Why does FileCloseProc preserve standard descriptors during thread exit, but TcpCloseProc does not?
Openf739b95fdb1e8277f40fb42936d52b2116464ba2Created 35 days agoUpdated 34 days ago
Descriptionplain2026-10-28 10:28:20
We are investigating an exit-time issue in a C++ application embedding Tcl 8.6.8.
Some C++ static destructors write to stderr after Tcl_Exit() starts application shutdown. The behavior depends on how the application is launched:
LSF bsub -I: stdin, stdout and stderr are inherited INET sockets. Tcl wraps stdin and stderr as TCP channels. During shutdown, TcpCloseProc closes fd 2 before the C++ static destructors run, causing their writes to stderr to fail with EBADF.
LSF bsub -Ip: the standard descriptors are connected to a pseudo-terminal. The corresponding channels use FileCloseProc, which preserves standard descriptors during thread exit. The destructor messages print successfully.
In the Tcl version we are using, FileCloseProc checks TclInThreadExit() and avoids closing descriptors 0, 1 and 2 during thread-exit cleanup. However, TcpCloseProc closes its socket descriptors without an equivalent check.
We confirmed the following shutdown path with a debugger:
Tcl_Exit
→ FinalizeThread
→ TclFinalizeIOSubsystem
→ ChanClose
→ TcpCloseProc
→ close(2)
At this point, TclInThreadExit() returns 1. Later, the C++ destructor’s write to fd 2 fails.
Could you please clarify:
1. Is there an intentional reason for preserving standard descriptors in FileCloseProc but not in TcpCloseProc?
2. Would applying the same thread-exit check to standard socket descriptors in TcpCloseProc be appropriate, or could that affect socket cleanup semantics?
3. Has this behavior been addressed in a newer Tcl release?
4. Is there a recommended approach for an embedding application that needs stderr to remain available to C++ static destructors during shutdown?
Our proposed change would preserve only descriptors 0, 1 and 2 during thread-exit cleanup. Other socket descriptors, and explicit channel closes outside thread exit, would retain their existing behavior.
Thank you for your guidance
arvind29 Ticket [f739b95fdb]: 3 changes
[History diff]2026-10-28 10:28:37
arvind29 Ticket [f739b95fdb]: 3 changes
[History diff]2026-10-28 14:43:26
apnadkarnimarkdown2026-10-30 06:22:45
Couple of comments, mostly so you don't feel ignored :-) But I'm afraid will not help you much.
I don't think the folks who originally wrote the code are involved in active core development any more so you are unlikely to get a definitive answer right away.
Someone, perhaps me, will eventually take a look, but it is simply not a high priority and engineering time is very limited.
Tcl 8.6.8 is way way old. Any fixes will not happen on 8.6 which is EOL so you should consider moving to 9.0 which has been out for a while now.
Sorry for the bad news.
arvind29plain2026-10-30 06:48:20
Thanks for the response. I checked Tcl 9.0.0 as suggested, but the same behavior is present there as well. Could you please confirm whether the difference between FileCloseProc and TcpCloseProc is intentional?
Log in (anonymous is fine) to add a comment.