Overview
| Artifact ID: | f90d5e2b8a81f2194bc2c442e0730a457bf1421735728ffb32f6549560c46ebd |
|---|---|
| Ticket: | f739b95fdb1e8277f40fb42936d52b2116464ba2
Why does FileCloseProc preserve standard descriptors during thread exit, but TcpCloseProc does not? |
| User & Date: | arvind29 on 2026-10-28 10:28:20 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/x-fossil-plain"
- comment changed to:
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 - is_private changed to: "0"
- login: "arvind29"
- priority changed to: "5 Medium"
- private_contact changed to: "24b6332679e5f96732dab3b9d46f1f0d512b2551"
- resolution changed to: "None"
- severity changed to: "Critical"
- status changed to: "Open"
- submitter changed to: "arvind29"
- subsystem changed to: "27. Channel Types"
- title changed to:
Why does FileCloseProc preserve standard descriptors during thread exit, but TcpCloseProc does not?
- type changed to: "Bug"