Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2024-08-27
08:10 • Closed ticket [9b67dc9618]: socket-14.11.* write before being notified that the ... artifact: 5defcc09f7 user: sebres
08:10 • Ticket [9b67dc9618]: 3 changes artifact: 97522a6326 user: sebres
2024-08-26
18:53 • Ticket [9b67dc9618]: 3 changes artifact: cfdda73319 user: pooryorick
17:36 • Ticket [9b67dc9618]: 4 changes artifact: 52ff516b3a user: oehhar
16:13 • New ticket [9b67dc9618]. artifact: 19f032ff3b user: pooryorick

socket-14.11.* write before being notified that the channel is ready

Closed / Invalid9b67dc961892c8494218ee7eef4b30b08ccef865Created 2 years agoUpdated 2 years ago
Descriptionwiki2024-08-26 16:13:09
In the following excerpt socket.test/socket-14.11.0, puts is called before any notification is received that the channel is ready for writing:

set sock [socket -async localhost [randport]]                                                                                                                                                                                                                                                                          
fconfigure $sock -blocking 0                                                                                                                                                                                                                                                                                           
puts $sock ok

This is contraindicated in the documentation: "The application must use the Tcl event loop for non-blocking output to work; otherwise Tcl never finds out that the file or device is ready for more output data. It is possible for an arbitrarily large amount of data to be buffered for a channel in non-blocking mode, which could consume a large amount of memory. To avoid wasting memory, non-blocking I/O should normally be used in an event-driven fashion with the chan event command (do not invoke chan puts unless you have recently been notified via a file event that the channel is ready for more output data)."

Therefore, socket-14.11.0 and 14.11.1 are testing undefined behaviour.

oehharwiki2024-08-26 17:36:31

Thank you for this ticket. Please allow me to give an insight of the programmer perspective.

I use that to optimize my code. I know, that I have to send init data after connection once. Due to that, I use the upper construct quite verbatim.

Thank you for all, Harald

pooryorickwiki2024-08-26 18:53:09

Harald, you should fix your code!

sebreswiki2024-08-27 08:10:33

This tests socket-14.11.* cover the failing flush by close, nothing else. However regarding the asynchronous write outside of writeable event...

>It is possible for an arbitrarily large amount of data to be buffered for a channel in non-blocking mode, which could consume a large amount of memory.

String "ok" is surely not large amount of data... No wasting or consumption of memory is expected by such small writes. Even a write of 100K outside or inside of writeable event is legitimate and completely fine, moreover does not differ from each other if reading side can read 1 byte only. Just because the notification about ready to read, doesn't tell us how many data it can read. By single, controlled write attempt, especially with small amount of data, it simply doesn't matter where and when exactly one'd do it (and whether the opposite side was ready to read or not).

> Therefore, socket-14.11.0 and 14.11.1 are testing undefined behaviour.

I already wrote what the tests doing, but...
Neither it is an UB, nor it contradicts the documentation (but if so, the docu must be fixed), nor it is somehow wrong to do asynchronous write not from writable callback. It is fully legitimate to use such code snippets for known small data internally buffering in channel subsystem. Just consider it as simplest asynchronous write procedure - aka don't want/need to wait for the write.

sebres Closed ticket [9b67dc9618]. [History diff]2024-08-27 08:10:48
Log in (anonymous is fine) to add a comment.