Tcl Source Code

View Ticket
Login
Ticket UUID: 2913585
Title: [win32] serial channel blocks indefinitely on write
Type: Bug Created on: 2009-12-13 08:35:32
Submitter: a_kovalenko Assigned to: andreas_kupries
Subsystem: 25. Channel System Severity:
Priority: 5 Medium Last modified: 2010-06-18 12:26:38
Status: Open Closed by:
Resolution: None Closed on:
Version: obsolete: 8.6b1.1
Description:
1. set fd [ open COM5: r+]
    fconfigure $fd -blocking no
    puts $fd something
    close $fd
Hangs indefinitely in SerialBlockingWrite() when the port is not connected.
2. if we use -blocking yes instead, close $fd seems to succeed, but _leaves an open handle and a running SerialWriterThread_ forever: so we get "permission denied" when trying to reopen the port.

Write timeout is ignored on Windows (it's documented at the open man page, and also obvious from the code).

The error is fixable in many ways, of which I can't select the best one yet. The problem is that GetOverlappedResult() in SerialBlockingWrite() waits forever (an interesting thing is that the code below handles ERROR_COUNTER_TIMEOUT, though it can never really happen). Forwarding the write timeout to SetCommTimeouts() would at least give me an ability to close the port somehow. Ensuring that the handle is not open after successful [close $fd], whether  its mode is blocking or non-blocking, would also be a good practice (thus avoiding the awful non-blocking case described above, when the handle is inaccessible by TCL but continues to hold the device in busy state). 

Also, [chan cancel] for all pending IO would be a good addition, but it's more about long-term TCL development plans.

It may happen that there is an easy workaround for _usual_ (UART16550A PC) serial ports: putting them into -handshake none mode, when all pending writes should succeed immediately. However, it doesn't help with many other COMM devices, like com0com-forwarded channels, Bluetooth virtual serial ports and (maybe) some USB2RS232 adaptors. This difference explains why this error could live unnoticed for so long.
User Comments:
a_kovalenko added on 2009-12-13 15:38:10:
Errr, typo: please read "-blocking yes" above as "-blocking no" and vice versa.

a_kovalenko added on 2010-06-18 12:26:38:
It turned out that there is a workaround for this bug, namely, 

(#1) always wait for writable state, using [fileevent], before writing
anything to port,

(#2) _and_ don't set buffering to none (haven't yet tested what
happens if I wait for fileevent and write more than -buffersize; it
might as well be important always to write less).

I continue to regard this as a bug, though: (#1) that is
understandable and (#2) that is rather obscure must be _both_
respected, and the penalty of uclosable channel and unopenable port
(forever..err, until the application is restarted) seems cruel and
unusual anyway. Documenting requirements (#1) and (#2) would be
significantly better than nothing.