.
D 2024-05-26T03:54:47.222
L Rationale\sfor\srollback\sof\srefchan\sevent\sgeneration\sin\score
N text/x-markdown
U apnadkarni
W 8665
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](https://core.tcl-lang.org/tcl/timeline?r=bug-18f4a94d03) contains
the rollback.
In summary, the changes
- were unnecessary as they were made in response to illustrative scripts that
were incorrect
- even if necessary, they changed the behavior of public commands and should
therefore have been TIP'ed
- completely nullify the purpose of event driven i/o
The below merely consolidates the discussion in various related tickets.
In (roughly) chronological order:
## Commit [e066e24f](https://core.tcl-lang.org/tcl/info/e066e24f36f67a8d)
The commit changed the `chan postevent` from synchronous to asynchronous operation as
a fix for ticket [67a5eabb](https://core.tcl-lang.org/tcl/info/67a5eabbd3d1).
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](https://core.tcl-lang.org/tcl/tktview/18f4a94d) 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](https://core.tcl-lang.org/tcl/info/cb779fc03defc0b8), 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](https://core.tcl-lang.org/tcl/info/d0dd6d19a41ae7af)
As a fix for [de232b49](https://core.tcl-lang.org/tcl/tktview/de232b49f2),
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](https://core.tcl-lang.org/tcl/tktview/080f846fd5), test
failures in TWAPI TLS. Same root cause as above.
3. The Tk UI [freeze](https://core.tcl-lang.org/tcl/info/40e85b6fd7ef453e)
subsequently logged at [ac7592e7](https://core.tcl-lang.org/tcl/tktview/ac7592e73c)
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.
- The events are generated with no knowledge of the device (refchan implementation)
state. Thus the writable event that a connection was open (bugs 1, 2 above)
would fire without checking the connection was open.
- The documentation for fileevent states *A channel is considered to be writable
if at least one byte of data can be written to the underlying file or device
without blocking...*. Notifying a channel is writable purely based on a timer is
in violation of that statement. As an aside, in the documentation of `chan event`,
the wording *underlying file or device* was removed in commit
[eb627bda](https://core.tcl-lang.org/tcl/info/eb627bda27968937). This might have
been just a side effect of an effort towards readability but it has semantic
consequences as without those semantics writable events are pretty much useless.
In any case, I restored the original wording as part of the `chan` manpage
consolidation.
- Generation of events on a continuous basis at regular intervals is no
different than polling. As elaborated in
[this post](https://core.tcl-lang.org/tcl/info/b11ef71db56e7fc8) it completely
defeats the purpose of an event driven I/O architecture. If this was an
acceptable solution, the entire event architecture for I/O in Tcl can be
jettisoned. Just have a timer that generate events for all registered channels.
No need to even check the channel state!
- Generation of events with a regular interval of **0** is even worse. This is
essentially continuous polling with all that implies! Bug 3 above (frozen UI) is
just one consequence. This is really such a fundamental design flaw, no other
justification for reverting should be needed.
@pooryorick has proposed fixes for the specific bugs listed above.
- [70a407c2](https://core.tcl-lang.org/tcl/info/70a407c2) is proposed to deal
with the premature connection open notification bug. While it may fix this
specific issue, it does so by essentially **guessing** connection state based on
buffer status. In doing so, it introduces dependencies between two unrelated and
conceptually independent modules - buffer management and state-based event generation. Any
future changes to the way the channel buffers are managed has the potential to
break the latter. This is fragile and in SE terms, coupling between
unrelated components is an anti-pattern for maintainability.
- [b2d083e9](https://core.tcl-lang.org/tcl/info/b2d083e9) is proposed as
as a fix for the idle event queue starvation by moving the events from
the event queue to the idle queue. My objections are noted at
[934bb3a6](https://core.tcl-lang.org/tcl/info/934bb3a69d721587) as
has @sebres in the comments to [ac7592e7](https://core.tcl-lang.org/tcl/tktview/ac7592e7).
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
- At the time of logging the "bugs" that motivated the changes, the author of
the scripts was not aware of the refchans being responsible for generation of
events themselves. The purported bugs in the core were really bugs in the script
and did not require these core changes. The scripts should have been fixed.
or
- The intent was as @pooryorick stated at a later time *"I think it's pretty clear that refchan
implementations shouldn't be burdened with the duty of maintaining timers"*. No,
it's not clear at all. Since it's only the refchan implementation, and not the
channel core, that knows the full channel state, it is the only component that
can correctly generate events based on state of the "underlying file or device".
The channel core cannot do this correctly until TIP 131 is implemented. And as
far as burden goes, to put it bluntly, folks not capable of scheduling `after`
timers have no business writing refchans. Ironically, @pooryorick in his
comments suggests how previously working code that was failing due to the
commits in question should be modified to work around the issues introduced, in
effect "burdening" users of the channel with the duty of working around the
introduced flaws!
Z 7c9cac3e46cd05b1fdef699e7fb18c76