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

Changes To Rationale for rollback of refchan event generation in core

Changes to "Rationale for rollback of refchan event generation in core" between 2024-06-06 01:47:40 and 2024-11-04 22:07:31

︙
299
300
301
302
303
304
305











































































































299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
> documented as asynchronous,

I see no such documentation in the `chan` manpage, nor in the TIP 219.
Please provide a reference to the docs you believe made the async
claim.

```

## Poor Yorick's response

```Andreas, thank you for the analysis.  In order to answer the question of
whether [chan postevent] was intended to be asynchronous, you had to
look decades back into the past and reconstruct your memories in light
of the current debate, and you have persuaded yourself that [chan
postevent] was intended to be synchronous.  I'll now attempt to persuade
you of the opposite.

In the small, Tcl_NotifyChannel() appears to be synchronous, but when
one steps back and looks at the larger picture, it is part of an
asynchronous operation.  It's true that Tcl_NotifyChannel() does not
queue up an event, but that's because Tcl_NotifyChannel() is itself an
event handler.  In tclUnixChan.c, which is the implementation of the
*nix channel driver for files, FileWatchProc() passes
Tcl_NotifyChannel() to Tcl_CreateFileHandler(), and the notifier then
calls Tcl_NotifyChannel() later when it detects an event on the channel.
This effectively makes Tcl_NotifyChannel() asynchronous, as its only
caller is the event loop itself.  When this also true for refchans, i.e.
when the only caller of a handler for an event on the refchan is the
event loop itself, then the operation of refchan is consistent with the
operation of other channels.  Unfortunately, due to the reversions
listed below, this is no-longer the case.

You named it [chan postevent] instead of [chan notify] because you
intuited that it was not at the same level as Tcl_NotifyChannel(), even
if you didn't articulate then, or even now.  [chan postevent] is not
Tcl_NotifyChannel().  It operates at an even lower layer:  It is the
mechanism that informs the notifier that there is an event on the
channel.  File event detection is not part of the channel driver, but
rather is a part of the notifier:  select() is called in tclUnixNotfy.c,
not in tclUnixChan.c.  In contrast with other event sources that are
surfaced through select(), which interacts with external systems during
normal event loop operations, a refchan must have some way to make the
event known to Tcl.  [chan postevent] was provided for that purpose.  In
other words, [chan postevent] is the analogue of select(), not the
analogue of Tcl_NotifyChannel().  As such, it should return something to
the notifier, like select() does during normal event loop processing.
The way to have a refchan do that is for [chan postevent] to place an
event on the queue, just as its name implies.  So you got the name
right, even if you weren't sure exactly why.

Requiring every refchan to use [after] to call [chan postevent] is a
completely unnecessary wart.  It's nothing more than leakage of poor
implementation into the script level.  Inspection of the design proves
that it is unnecessary, and not one example has been brought forward
demonstrating any reason a refchan might need to call [chan postevent]
synchronously when a channel is nonblocking.  No one out there is doing
it either, because it would result in sporadic deadlocks.  This is
simply hand-wringing over a fictitious situation.  Since a refchan must
use [after] to call [chan postevent] if it wants to work properly,
taking care of that part at the implementation level is a much better
strategy.

For the last five years, the following bugs have been fixed on
core-8-branch and in trunk

    https://core.tcl-lang.org/tcl/tktview/de232b49f26da1c18e07513d4c7caa203cd27910

    https://core.tcl-lang.org/tcl/tktview/67a5eabbd3d195915d3ccc13246bdecf10e20726

Throughout five years of development on those branches, the fix has
never been called into question, and has never lead to another valid bug
report (at least not one that wasn't quickly fixed).  Before coroutines,
calling [chan postevent] without a timer "only" lead to deadlocks.
Nowadays, when a refchan is implemented as a coroutine, it is an error
to call [chan postevent] synchronously.  I'll repeat that:

    It is an error to call [chan postevent] synchronously.

Coroutine implementations of refchans make it manifestly clear that
[chan postevent] must be asynchronous.  Therefore, there is no point in
trying to pretend that [chan postevent] is synchronous.  It must go
through the event queue, and the only way currently to do that is to use
[after].  Although it is coroutine implementations of refchans that make
this obvious, it is and always has been true for all other
implementations as well.  It's just that with other implementations, the
error can be more camouflaged, with deadlocks happening at unpredictable
times.

But now, via the following commits, the fix for the bugs mentioned
above has been undone.

    core-8-branch

        https://core.tcl-lang.org/tcl/info/8d4d978bd3ca580d

    trunk

        https://core.tcl-lang.org/tcl/info/1ec9927351b255fb

This work was tedious and painstaking, it solved the bugs referenced
above, and it made the operation of refchans more consistent with other
types of channels. Now Tcl has lost this contribution, and all for a
"bug" report that doesn't actually demonstrate any reproducible bug, and
only because someone has some ideas but no code to stand those ideas on.
It's a mistake to undo this work, and it's a mistake to rationalize
undoing it by documenting [chan postevent] as synchronous.  Let's bring
back consistency with the design of the IO subsystem, ditch this
brain-dead idea that [chan postevent] should do anything other than post
a notification for the notifier, and revert the reversions of the code
that fixed the bugs above.

-- 
Yorick
```