Overview
| Artifact ID: | 838d4d73fe9ec3243053ede1497ec5e1498d7759bb8d91592e709dc74c4226f3 |
|---|---|
| Ticket: | de232b49f26da1c18e07513d4c7caa203cd27910
write-only nonblocking refchan and Tcl internal buffers |
| User & Date: | pooryorick on 2024-03-31 01:12:56 |
Changes
- icomment:
Yes, I incorrectly claimed that [chan configure ... -blocking] was redundant with [socket -async]. I forgot that [socket -async] returned a blocking channel. However, the fact that [socket -async] returns a blocking channel provides the solution to [080f846fd58958ef]: Don't immediately configure the channel to be nonblocking, and instead do this: <code><verbatim> set so [twapi::tls_socket -async www.example.com 443] fileevent $so writable [list apply {chan { #chan is now connected fconfigure $so -blocking 0 fileevent $so writable [list apply {chan { #do stuff with connected channel } $so] }} $so] vwait ... </verbatim></code> Problem solved: The event will be triggered only when the channel is connected. Now, on to the issue of "writable" events on non-blocking channels. If a channel exists and is nonblocking, it's writable. Any other state of the channel, whether it is error state or connection state is not a factor. That's how nonblocking channels have always functioned in Tcl, and if you think about it, that's the way it must be: If something is blocking the writability of a channel, then it's no-longer nonblocking. That's why it has always been the case that for a nonblocking channel [puts] never fails, and also why "Tcl continues to buffer the data and writes it in the background as fast as the underlying file or device can accept it." ([puts] documentation), and why [close] is the operation that reports the error, if there was one: There's no other strategy that is practical. Here's another example: <code><verbatim> set chan [socket -async <some address>] chan configure $chan -blocking 0 puts $chan hello </verbatim></code> Here, it's entirely possible that the socket will not be connected yet, but when [puts] executes it will never fail, nor should it. That's the point of configuring a channel to be nonblocking: It doesn't ever block. By definition, if a channel never blocks, it is always writable. Over in [080f846fd58958ef] Ashok wrote, "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." As we see in the example above, that's wrong: [puts] doesn't fail, and if it doesn't fail, then the channel was writable. If the channel is writable, there's no problem triggering a corresponding event on the the channel. Not only that, but the fire-and-forget aspect of nonblocking channels is very convenient, particularly at the script level. I incorrectly claimed that the test 14.11.0 failure we were talking about was the case where it produces an error. The actual situation is that the test fails if an error does not occur, but I believe it is by design that sometimes there is no error. The documentation for [close] states, "If the channel is nonblocking and there is unflushed output, the channel remains open and the command returns immediately; output will be flushed in the background and the channel will be closed when all the flushing is complete." That means that for systems that take their sweet time figuring that a connection can't be completed, [close] will in the meantime return without error, which in turn means that test 14.11.0 is only testing circumstance rather than anything meaningful. Perhaps that test should be deleted, or perhaps a pause should be introduced to let the system finish meditating on the state of the connection. I believe that the existing fix for this issue is theoretically correct, and should remain in place. A few years ago I submitted issue [67a5eabbd3] and fixed it. Prior to this, refchans were thoroughly broken in an subtle way, and I believe this caused refchan programmers over the years to throw up their hands and just walk away from refchans, or to invent twisted cargo-cult approaches to keeping their refchans responsive. That issue is related to this one. Not taking the approach that has now been established for triggering writable events on asynchronous channels risks making them unusable again in ways that are too subtle for most programmers to ascertain, and in my experience means that we'll just keep playing whack-a-mole with issues related to handlers not ever getting triggered even though the should be. I'm not in favour of changing the default buffering for refchans. That would be a knee-jerk reaction to a particular problem rather than a well-considered design, and in issue [080f846fd58958ef] there is so far no actual example of a broken script that would warrant such a change. - login: "pooryorick"
- mimetype: "text/x-fossil-wiki"