Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-22
17:19 • Ticket [653218ffff] Channels CloseProc() not called on close status still Cl... artifact: 20bd9150f8 user: serhiy.storchaka
2026-10-16
21:49 • Closed ticket [653218ffff]. artifact: 005ad727f2 user: serhiy.storchaka
2003-10-16
20:59 • Ticket [653218ffff]: 1 change artifact: 6753ec15a4 user: dkf
2003-01-08
13:52 • Ticket [653218ffff]: 4 changes artifact: 643fb62ba9 user: schroedter
2003-01-07
17:59 • Ticket [653218ffff]: 4 changes artifact: b7d2e6fea8 user: dkf
15:28 • Ticket [653218ffff]: 4 changes artifact: 1ab2d5312b user: schroedter
05:52 • Ticket [653218ffff]: 4 changes artifact: fd94c4c9e8 user: andreas_kupries
2002-12-13
21:14 • Ticket [653218ffff]: 4 changes artifact: b0d95fe8d8 user: schroedter
20:51 • Ticket [653218ffff]: 4 changes artifact: 65cb6e2bfc user: dgp
20:17 • Ticket [653218ffff]: 4 changes artifact: ecda7f38da user: dkf
18:58 • Ticket [653218ffff]: 4 changes artifact: 46695c69c6 user: schroedter
11:54 • New ticket [653218ffff]. artifact: 4333706460 user: schroedter

Channels CloseProc() not called on close

Closed / Invalid653218ffffffffffffffffffffffffffffffffffCreated 24 years agoUpdated 41 days ago
Descriptionplain2002-12-13 11:54:58
If there is pending BG output for a non-blocking channel 
then Tcl_DriverCloseProc() is not called when the 'close' 
command is invoked.

That can be seen from generic/tclIO.c : 
Tcl_Unregister_Channel():
    if (!(statePtr->flags & BG_FLUSH_SCHEDULED)) {
        ...

This can be very bad if channel's output is stalled, e.g. a 
serial port blocked by handshake:

% puts "Tcl $tcl_patchLevel"
Tcl 8.4b1
% set chan [open com1: r+]
fileaaf550
% fconfigure $chan -buffering none -blocking 0
% fconfigure $chan -mode 1200,n,8,1 -handshake rtscts
% ### 2 outputs: "hello world" \x0D\x0A, BG_FLUSH
% puts $chan "hello world"
% puts "Queue: [fconfigure $chan -queue]"
Queue: 0 13
% after 1000
% puts "Queue: [fconfigure $chan -queue]"
Queue: 0 13
% close $chan
% set chan [open com1: r+]
couldn't open "com1:": permission denied
% 

This problem occurs also for slow output without 
handshake:

% puts "Tcl $tcl_patchLevel"
Tcl 8.4b1
% set chan [open com1: r+]
fileaaf768
% fconfigure $chan -buffering none -blocking 0
% # Explicitely reset handshake=none
% fconfigure $chan -mode 1200,n,8,1 -handshake none
% puts $chan "hello world"
% puts $chan "hello world"
% close $chan
% set chan [open com1: r+]
couldn't open "com1:": permission denied


For Tcl < 8.4. this has never been observed for serial 
ports, because serial output has already been blocking.
schroedterplain2002-12-13 18:58:29
Logged In: YES 
user_id=99573

This bahaviour may be interpretet not as a bug,
but as a feature of the channel system.

But then we need an additional option 
        [fconfigure $chan -flushonclose 0]
(default=1) for channels like serial ports.
dkfplain2002-12-13 20:17:39
Logged In: YES 
user_id=79902

When I have time (maybe, just maybe, over Xmas) I'll be
advancing a TIP on behaviour-on-close of serial channels.
dgpplain2002-12-13 20:51:55
Logged In: YES 
user_id=80530

Is this related to FR 525783 ?
schroedterplain2002-12-13 21:14:31
Logged In: YES 
user_id=99573

IMO not. [ 526163 ] tty not reset on exit 
is related to the (requested) change made by TIP#35, not to
restore the original TTY settings on close, so that tcl can
be used as a simple 
'stty' substitute.
This stuff is not yet finally removed - see the comment in
tclUnixChan.c, ttyCloseProc().
andreas_kupriesplain2003-01-07 05:52:45
Logged In: YES 
user_id=75003

Note: If BG_FLUSH_SCHEDULED is set the channel cannot 
be closed completely. There is a BackGround-FLUSH of data 
active. The channel won't be visible at the script level 
anymore, but the data structures remain until the flush is 
completed. After that happend the close proc is called and 
the structures are cleaned up completely. This is only for a 
non-blocking channel.

To get 'flushonclose' behaviour just set the channel to -
blocking 1. Of course, this may stall the [close] at the tcl 
level if the serial port itself is stalled.
schroedterplain2003-01-07 15:28:10
Logged In: YES 
user_id=99573

Yes, what you say emphasizes that Tcl needs a mechanism  
to close a stalled serial channel by discarding all currently 
pending output.
dkfplain2003-01-07 17:59:04
Logged In: YES 
user_id=79902

There are several possible desired behaviours for what
should happen on closing of a serial channel, but I think
these are the interesting cases:
  wait until all OS buffers are empty and reset to initial
state
  discard all pending output (including OS level) and reset
to init.
  leave channel in *current* state
(I'll not specify what happens to the OS buffers in that
last case as I believe that typically in that case, there
won't be much data in the buffers anyway!)

IIRC, right now we just support the first of those, and we
know that selection of the right behaviour can't be done
automatically (we don't want to discard debug info!) so that
calls for a suitable [fconfigure] option. Which means a TIP.
Which I've not had time to write.  (And I'm not certain how
-blocking interacts with this; the buffers in question are
in the OS, not Tcl.)
schroedterplain2003-01-08 13:52:51
Logged In: YES 
user_id=99573

For blocking channels no changes are necessary.
For non-blocking channels the current behaviour is to put the 
channel to *blocking* mode, wait until all Tcl output buffers 
are done and only then call Tcl_DriverCloseProc(). 

If there would be a non-default [fconfigure -discardonclose 1] 
option the channel system should
1. *Not* make the channel blocking.
2. Discard all pending output in Tcl buffers.
3. Immediately call Tcl_DriverCloseProc().

The serial SerialCloseProc() is responsive for the OS-level 
output. So SerialCloseProc() should also check for the 
[fconfigure -discardonclose 1] flag and discard OS-level output.

I could write the TIP and implement the the changes in the 
serial drivers SerialCloseProc() for Windows & Posix. But I do 
not feel skilled for the changes in the general channel system.
dkf Ticket [653218ffff]: 1 change [History diff]2003-10-16 20:59:24
serhiy.storchakawiki2026-10-16 21:49:57

The deferred close is by design (see close(n)). Testing this found that on Linux in 8.7 and later a background flush to a pipe whose reader has exited never ends: [7aa8fd34c4].

serhiy.storchaka Ticket [653218ffff]: 3 changes [History diff]2026-10-22 17:19:27
Log in (anonymous is fine) to add a comment.