Skip to content
Tcl Source Code View Ticket
Login

View Ticket

exec hangs, iff stdin & stdout both were previously closed.

Open21dbc600ae0a005a7f879a05580550a8da257b14Created 4 years agoUpdated 43 days ago
Descriptionplain2022-12-06 11:45:49
simplest test script:
   close stdin; close stdout; exec true

Workaround (assuming that original stdin & -out just need to be closed):
   close stdin; close stdout; open /dev/null r; exec true

In either case: exec first creates a pipe, and while the child runs,
the parent waits on that pipe for EOF (probably to be initiated
from another thread).

With at least one of stdin or stdout NOT closed (or re-opened afterwards),
the EOF on that pipe is seen, but with BOTH stdin/-out closed, nothing
ever closes the write-end of the pipe.

PS: that pipe is NOT the one by which the main process communicates to the child.
Referenced by3
  • d2c4f74bacFix [21dbc600ae]: exec hangs, iff stdin & stdout both were previously closed check-in2026-10-21 06:30:44
  • 32b22bfeb5Fix [21dbc600ae]: exec hangs, iff stdin & stdout both were previously closed check-in2026-10-21 06:28:49
  • c6f3b26c02Fix [21dbc600ae]: [exec] hung if stdin and stdout were closed: its pipe got descriptors 0 and 1, which TclpCloseFile() never closes, so the parent kept the write end open. Move descriptors created for a pipeline above 2. check-in2026-10-13 18:33:34
avl42 Ticket [21dbc600ae]: 3 changes [History diff]2022-12-06 11:47:24
avl42 Ticket [21dbc600ae]: 3 changes [History diff]2022-12-06 11:56:25
avl42 Ticket [21dbc600ae]: 3 changes [History diff]2022-12-06 12:06:26
serhiy.storchakawiki2026-10-13 18:34:38

Still hangs with 8.6.17, 9.0 and 9.1. With stdin and stdout closed, the pipe for the output of the child gets descriptors 0 and 1, and TclpCloseFile() never closes 0, 1 and 2, so the parent keeps the write end open and never sees the end of the output.

Proposed fix in branch exec-closed-stdio: move pipes and files created for a pipeline above descriptor 2. Test exec-23.1.

jan.nijtmanswiki2026-10-20 20:53:48

I'm seeing this compiler warning, even though it appears to work fine:

unix/tclUnixPipe.c:469:13: warning: variable ‘errPipeIn’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Wclobbered]
  469 |     TclFile errPipeIn, errPipeOut;
      |             ^~~~~~~~~
unix/tclUnixPipe.c:469:24: warning: variable ‘errPipeOut’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Wclobbered]
  469 |     TclFile errPipeIn, errPipeOut;
      |                        ^~~~~~~~~~

jan.nijtmanswiki2026-10-20 21:03:20

This warning is caused by the line

pid = fork();

in line 602

serhiy.storchakawiki2026-10-20 21:28:10

The warning is not new in this branch: TclpCreateProcess() is unchanged, but unix/tclUnixPipe.c has #define fork vfork when HAVE_VFORK is set, so pid = fork(); is really vfork(), after which gcc warns about locals that may be clobbered. dsArray and newArgv there were already volatile for this reason, errPipeIn/errPipeOut were not.

Fixed in [4af19e7744] on the branch: they are volatile now (TclpCreatePipe() fills plain temporaries). My gcc 15 never showed the warning, so please check that it is gone with yours.

Log in (anonymous is fine) to add a comment.