Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: f6d496e261875110e7f791b7ffb3acb0344eeb968c9ffa0bd23a7fd1e251b262
Ticket: de232b49f26da1c18e07513d4c7caa203cd27910
write-only nonblocking refchan and Tcl internal buffers
User & Date: apnadkarni on 2024-03-30 12:02:18
Changes

  1. icomment:
    A channel writable event is being generated by a piece of code on a periodic basis with no knowledge of the channel state and whether it is actually writable or not. This is so broken as to be self-evident in my opinion and should need no debate but to answer Nathan's points below.
    
    The failure of socket-14.11.0, marking a connection when it is not yet open, or  is not a trivial matter. That specific scenario is exactly one that might be used to implement a port scanner (which always operate asynchronously for obvious reasons). The current trunk implementation will show all ports open even when they are not! And note the script is not aware the channel is broken as you seem to think!
    
    Consider the complementary case where they are open. The event fires BEFORE the TCP connection actually completes, the write handler runs and writes connection details to syslog. Not an uncommon scenario but the fconfigure that retrieves address/port information returns empty strings because the connection has not completed making the logging useless.
    
    A third example - admittedly a code inspection based conjecture; I have not tried this as I don't have a multihomed machine. Tcl's socket implementation is smart enough that if the destination hosts resolves to multiple addresses, it will try each in turn until one succeeds. The write event will trigger on the first *attempted* connection which may actually fail when a subsequent candidate succeeds.
    
    Now with respect to the `-async` option, let's ignore twapi because it simply clones Tcl socket semantics. The two illustrative fragments you gave are very different semantics. When the -async option is specified, the command returns without waiting for the TCP connect to be completed and connection open. The application can continue other work until the connection is open. Without the -async option, the application is blocked from any other work until the connection is open, which may be several or even tens of milliseconds. Changing socket with fconfigure -blocking 0 cannot change this (in the second example!). -async does not make the socket non-blocking, and -blocking 0 does not make the connection asynchronous (how can it when it can only be done afterward!). So I completely fail to understand your comment of redundancy.
    
    The fundamental problem here is that the reflected channel framework has incomplete support when it comes to event driven I/O. All channel drivers I have written or seen have two components. The first is the channel driver itself - in this case the tclrchannel driver. The second component of the driver hooks into Tcl event processing via the Tcl_EventSetupProc / Tcl_EventCheckProc callbacks. These are where the channel implementations generate notification events but the refchan framework has equivalent. The correct fix would be to add the equivalent facilities to refchan, or alternatively enhance the channel driver API (and refchan) to be able to exchange state information that would let the common channel code make proper decisions that depend on channel state.
    
    Both are likely to be non-trivial and tricky to get right, possibly why they were never implemented, and probably not feasible in the 9.0 time frame.
    
    As an alternative solution, I propose turning off buffering for refchans (either via an internal -buffering none or a -buffersize 1). This forces calls to refchans always to be passed down to the refchan script which is then responsible for generating events exactly as Nathan's test code does. This experiment is in branch [apn-bug-de232b49f2](https://core.tcl-lang.org/tcl/timeline?r=apn-bug-de232b49f2). There are some side effects that I will detail later but for now, all tests pass (both socket-14.11.0 as well as io-44.6 which tests Nathan's coroutine based driver).
    
  2. login: "apnadkarni"
  3. mimetype: "text/x-markdown"