Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 01394d173744bf722035f1a4c9d32251510d011b
Ticket: 2927851fffffffffffffffffffffffffffffffff
Tcl dies when STDIN is closed
User & Date: mohanchous on 2010-01-07 22:00:03
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    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.
    
    
    
  4. foundin changed to: "obsolete: 8.5.8"
  5. is_private changed to: "0"
  6. priority changed to: "5 Medium"
  7. resolution changed to: "None"
  8. status changed to: "Open"
  9. submitter changed to: "mohanchous"
  10. subsystem changed to: "None"
  11. title changed to: "Tcl dies when STDIN is closed"
  12. type changed to: "Bug"