Tcl Source Code

View Ticket
Login
Ticket UUID: 2927851
Title: Tcl dies when STDIN is closed
Type: Bug Created on: 2010-01-07 22:00:03
Submitter: mohanchous Assigned to: dgp
Subsystem: 50. Embedding Support Severity:
Priority: 5 Medium Last modified: 2010-01-08 22:08:09
Status: Open Closed by:
Resolution: None Closed on:
Version: obsolete: 8.5.8
Description:
Tcl Version: 5.8.6 (but likely in 5.8.7 as well)
Os Platform and Version: Windows 2003 Server

Before loading the Tcl library, our calling program closed STDIN, which it does not read. When the Tcl library was loaded, it eventually called TclpGetDefaultStdChannel() to create the Tcl STDIN channel. This in turn eventually called the Windows system GetStdHandle() to get the Windows STDIN file descriptor.

Apparently, even though STDIN had was closed, GetStdHandle() returned a nonzero (bogus) file descriptor, presumably the original STDIN file descriptor that had been closed. I  do not know if this is a GetStdHandle() bug or feature, nor do I know if it is specific to Windows Server 2003.

Subsequent Tcl code assumed the bogus STDIN file descriptor was valid and created its Tcl STDIN channel on it. The Tcl STDIN channel was given interpreter reference count refCount = 2 instead of the normal refCount = 1. I presume this is done to prevent a Tcl interpreter thread from closing the calling program's STDIN. So now we have a Tcl STDIN channel with refCount = 2 on a bogus closed STDIN file descriptor.

After loading the threaded Tcl library, the calling program then proceeded to interpret several Tcl scripts. This ultimately ended up calling Tcl FSEvalFileEx() in win/tclIOUtil.c, which, several layers down, calls the Windows system CreateFileA() or CreateFileW() to obtain a Windows file descriptor for the file.

However, since STDIN is closed, Windows eventually recycles the STDIN file descriptor, and possibly after many calls, CreateFileA() or CreateFileW() returns the original STDIN file descriptor. When this happens, Tcl identifies the file descriptor with its bogus STDIN channel and increments the channel's refCount to 3, instead of creating a new file channel as it should. It reads from its STDIN channel, which succeeds because the defunct file descriptor is now open on the new file. Finally, Tcl_FSEvalFileEx() calls Tcl_Close() on the file descriptor to close the file. This causes Tcl_Close() to try to close the STDIN channel. It notices refCount > 0 on the channel, decides it is closing some other interpreter's channel, and aborts with the message

called Tcl_Close on channel with refCount > 0

taking the calling program with it.

We were able to work around this in our case by leaving STDIN open on the calling program.
User Comments:
dkf added on 2010-01-08 18:00:32:
My feeling is that it might be best to deal with a closed standard channel by opening /dev/null (or other platform equivalent) during initialization of the library. Or possibly during Tcl_Main. (Definitely before we source the application script though.)

Assigning to someone who might have a well-informed opinion...

dgp added on 2010-01-08 22:08:09:
It will take me a while to find the time
to analyze this fully.  (The standard
channel support is a god-awful mess).

For embedding programs with special
needs like this, though, I would expect
them to need to make their own calls
to Tcl_SetStdChannel() to make Tcl
customize as needed.  Explicitly setting
Tcl's stdin channel to a 'null' channel
works, though it may require you code
a null channel for complete portability.