Skip to content
Tcl Source Code Update of ”Rationale for rollback of refchan event generation in core”
Login

Update of ”Rationale for rollback of refchan event generation in core”

Overview

Artifact ID: d7cd2603384e05e237c648ec0ef42175ee2bb98845fa9d3ff2e31b9c3b57f9cc
Page Name:Rationale for rollback of refchan event generation in core
Date: 2024-05-26 03:54:47
Original User: apnadkarni
Mimetype:text/x-markdown
Next 740a8187fbb974f8d1b2c92cc240919fd964c4dea2ef70b78d9053db6adb08ca
Content

All,

Below is my rationale for requesting a rollback of certain post-8.6 changes related to event generation for refchans. Thanks to Jan, the experimental branch bug-18f4a94d03 contains the rollback.

In summary, the changes

The below merely consolidates the discussion in various related tickets. In (roughly) chronological order:

Commit e066e24f

The commit changed the chan postevent from synchronous to asynchronous operation as a fix for ticket 67a5eabb.

TL;DR This change was unnecessary and if at all shown to be so, should have absolutely required a TIP.

For starters, the test case motivating the fix is itself flawed. The coroutine busy error was caused by the refchan implementation directly invoking chan postevent which results in a nested coroutine invocation. For these use cases, in particular where the refchan implementation is non-reentrant, it has always been the case that refchan implementations post events using the after idiom as described in Andreas' 2009 Tcl conference paper. As @aspect noted in the same ticket this is also true of the tcllib refchan implementations. It also is true of TWAPI's TLS implementation.

This change resulted in @bll logging ticket 18f4a9 illustrating a failure in previously working code. @pooryorick marked this as invalid because event loop was not running (as per my understanding). Without commenting on the merits either way, it is still the case that working refchan implementations that correctly used the after idiom will now have events passed through the event queue additional times for no good reason.

In any case, a change in a command behavior from synchronous or asynchronous should require a TIP. Paraphrasing @dgp's post, this results in (a) loss of the ability to choose synchronous and asynchronous operation, and (b) an incompatible loss of function in a public command.

Commit d0dd6d19

As a fix for de232b49, buffered coroutine write events not called, the commit introduced the continuous generation (roughly equivalent of after 0 but at the C level) of write events (when a write event handler is registered on a non-blocking channel) within the channel core implementation.

TL;DR Again, as in the previous commit, the change was unnecessary. It also resulted in new bugs.

As stated above, refchans are responsible of generation of their own events and the test example in the ticket is flawed in its implementation. It should have set up a timer itself at the script level as demonstrated in one of the attachments to that ticket. Instead, the commit changed the Tcl core itself to generate write events in a continuous 0-interval timer with no knowledge of the actual channel state. As a result,

  1. Test socket-14.11.0 started failing with this commit, sporadically on Unix as reported by @dgp, and consistently on Windows. Root cause was the fileevent handler being called before the connection was actually open.

  2. Ticket 080f846f, test failures in TWAPI TLS. Same root cause as above.

  3. The Tk UI freeze subsequently logged at ac7592e7 resulting from the continuous placement of writable events on the event queue.

Attempts were made to fix the above as summarized below but the bigger issue is that this is fundamentally broken design for more than one reason.

@pooryorick has proposed fixes for the specific bugs listed above.

In any case, aside from these misgivings about these fixes, more concerning is that these are point fixes and neither does anything to correct the fundamental point that continuous timer based generation of I/O events completely defeats the intent of event based I/O.

Rebuttals

@pooryorick has objected to reverting. See his comments and my responses in the various tickets linked above.

One additional point I want to make about fundamental motivation for the commits. Either

or