Overview
| Artifact ID: | f54756de6b6805694e94fe240c92cbf95494a719d03703012b6be12aac46e208 |
|---|---|
| Ticket: | 080f846fd58958efeaa5e2247622dc0ee7fa1ff7
Channel system generating writable events BEFORE channel is open (refchan) |
| User & Date: | apnadkarni on 2024-03-28 11:44:21 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/x-markdown"
- comment changed to:
A script may open a non-blocking channel and register an "writable" event handler to be called when the channel is open. This worked for refchans as well in 8.6 but in 9.0, the handler is invoked BEFORE the channel is actually open. The culprit is the [following](https://core.tcl-lang.org/tcl/file?ci=692ac81a3a01d582&name=generic/tclIO.c&ln=8761-8770) code in tclIO.c ``` if (statePtr->interestMask & TCL_WRITABLE && GotFlag(statePtr, CHANNEL_NONBLOCKING) && !GotFlag(statePtr, BG_FLUSH_SCHEDULED)) { /* * Restart the timer in case a channel handler reenters the event loop * before UpdateInterest gets called by Tcl_NotifyChannel. */ statePtr->timer = Tcl_CreateTimerHandler(SYNTHETIC_EVENT_TIME, ChannelTimerProc,chanPtr); Tcl_NotifyChannel((Tcl_Channel) chanPtr, TCL_WRITABLE); ``` The above was added post-8.6 to `ChannelTimerProc` in [d0dd6d19a4](https://core.tcl-lang.org/tcl/info/d0dd6d19a4) as a fix for a [ticket](https://core.tcl-lang.org/tcl/tktview/de232b49f2). No matter the motivation, this fragment seems incorrect or incomplete to me. Under no circumstances should a file event writable callback be invoked unless the channel is actually open (writable) and the above does not make that check. The impacted case described below involves refchans because the refchan infrastructure uses timers to generate events but I suspect it could also affect ordinary channels that register timers to detect (for example) connection time outs. I ran into the above porting twapi to 9.0. Twapi's TLS sockets are implemented as refchans wrapping Tcl sockets. The issue I'm seeing is the following test case: ``` set so [twapi::tls_socket -async www.example.com 443] fconfigure $so -blocking 0 fileevent $so writable [list apply {{chan} { ...chan is not actually open when this is called since connection has not completed }} $so] vwait ... ``` `tls_socket` is implemented as a reflected channel. In 8.6, there was no "automatic" timer based write event generation and thus the callback was invoked only on connection completion. With the above change, the callback is invoked from the timer code without any checks that the reflected channel is actually open. A simple test case is not easy to create since it involves timing (the connection should not complete before the timer) and implementation of a virtual channel but the above code seems obviously wrong to me in that it does not check whether channel is actually open. - foundin changed to: "9.0"
- is_private changed to: "0"
- login: "apnadkarni"
- priority changed to: "5 Medium"
- private_contact changed to: "9546e9877f1a8899e25a89082561ee694e7fb402"
- resolution changed to: "None"
- severity changed to: "Important"
- status changed to: "Open"
- submitter changed to: "apnadkarni"
- subsystem changed to: "25. Channel System"
- title changed to:
Channel system generating writable events BEFORE channel is open (refchan)
- type changed to: "Bug"