Overview
| Artifact ID: | d90e46e1cbe28b9e32a5c91f394e204585e9441e2a8e3f9a7c8be2a0f28a202b |
|---|---|
| Ticket: | 91c9bc1c457fda269ae18595944fc3c2b54d961d
Standard channels (stdin, stdout, stderr) on Windows may get broken by dup2() |
| User & Date: | abv on 2018-11-09 21:51:41 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/x-fossil-wiki"
- comment changed to:
1. The problem (Tcl level, root cause) On Windows, standard channels (stdin, stdout, stderr) in Tcl are initialized by OS handles returned by WinAPI function GetStdHandle() (see TclpGetDefaultStdChannel() defined in win/tclWinChan.c). These handles may be invalidated (closed, reopened, reassigned to different kind of object) by C/C++ code. In particular, function _dup2() of standard C library (MSVC), when called for the standard file number (0, 1, 2) as second argument, closes the OS handle associated with the standard stream, then creates the new handle and sets is as standard one by call to SetStdHandle(). In most cases the old and new handles are the same (apparently due to reuse), thus there are no immediate consequences. However, sometimes (in my experiments about once per several thousand calls), the new standard handle assigned by the system is different from the old one. Yet Tcl channel still keeps the old handle and when trying to use that channel (e.g. use puts to write to stdout), error occurs. 2. The problem (application level, reproducer) We are using Tcl as command-line tool to organize testing of a C++ library. When the test is executed interactively, the test system intercepts the output to stdout and stderr streams (from C code) so that it can be analysed. In this context, sometimes execution of a test script ends up with Tcl reporting "error writing "stdout": bad file number" Alas I have no isolated reproducer for the problem (no sufficient expertise with Tcl). However it can be reproduced within a test system (the software is open source), as follows: a) Install OCCT either from https://www.opencascade.com/content/latest-release or build it from sources (download link: http://git.dev.opencascade.org/gitweb/?p=occt.git;a=snapshot;h=refs/tags/V7_3_0;sf=zip) b) Run draw.bat c) Type "test perf bop boxholes" The problem has been reproducible for years, with Tcl 8.5 - 8.6. For debugging the problem I have used MSVC 2017 (15.8.4) Community Edition, working on Windows 10 64-bit. The last Tcl version tried was GitHub Tcl repository master as of Nov 06 3. The proposed solution The problem can be solved by duplicating the standard handle returned by GetStdHandle() (in TclpGetDefaultStdChannel()) and using the duplicate for initialization of the Tcl channel. Here is a diff: <pre> win/tclWinChan.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/win/tclWinChan.c b/win/tclWinChan.c index 8c47be646..480b387f4 100644 --- a/win/tclWinChan.c +++ b/win/tclWinChan.c @@ -1302,6 +1302,17 @@ TclpGetDefaultStdChannel( return (Tcl_Channel) NULL; } + /* + * Make duplicate of the standard handle as it may be altered + * (closed, reopened with another type of the object etc.) by + * the system or a user code at any time, e.g. by call to _dup2() + */ + if (! DuplicateHandle (GetCurrentProcess(), handle, + GetCurrentProcess(), &handle, + 0, FALSE, DUPLICATE_SAME_ACCESS)) { + return (Tcl_Channel) NULL; + } + channel = Tcl_MakeFileChannel(handle, mode); if (channel == NULL) { </pre> The only drawback that I foresee is that when standard stream is redirected on a C library level, Tcl will keep working with initial stream. Though this should be quite logical taken that Tcl has channels bound to WinAPI and not standard C library, which is apparently intentional. - foundin changed to: "8.6.8"
- is_private changed to: "0"
- login: "abv"
- priority changed to: "5 Medium"
- private_contact changed to: "10ba58e00d474c6b8c6003e8e4dbe18be020bb7d"
- resolution changed to: "None"
- severity changed to: "Important"
- status changed to: "Open"
- submitter changed to: "abv"
- subsystem changed to: "25. Channel System"
- title changed to:
Standard channels (stdin, stdout, stderr) on Windows may get broken by dup2()
- type changed to: "Bug"