Overview
| Artifact ID: | a861dcdf034fff2402f5189a57353632ff416abec3d06eb0f67cec0f754076e4 |
|---|---|
| Ticket: | 91c9bc1c457fda269ae18595944fc3c2b54d961d
Standard channels (stdin, stdout, stderr) on Windows may get broken by dup2() |
| User & Date: | abv on 2018-11-12 19:45:37 |
Changes
- icomment:
@sebres, thanks for commenting! I agree the situation is rare bacause (a) it is Windows-specific, (b) redirection of stdin at runtime is rarely used, and (c) the problem manifests once per several thousand calls. In my scenario, the call to dup2() occurs after initialization of the Tcl channels. Note that this can happen in the C code that does not know anything about Tcl at all. In this case the handle stored by Tcl channel might become invalid (and it happens in my case), and Tcl has no means to know about that. In my case the place where dup2() is called is aware of Tcl. Calling Tcl_SetStdChannel() was the first thing I considered, but the problem is how to release the existing Tcl channel that uses invalidated handle. That channel has no any flag that would indicate that this handle was not properly allocated, and it will try deleting that handle on destruction. This leads to error indeed. Another point is that calling Tcl_SetStdChannel() would be not sufficient: by that time, Tcl intepretor is already created and has its own copy of the same channel, thus it should be necessary to update channels stored in the interpretor as well (in general case - all existing interpretors). The proposed solution does solve the problem since it creates the duplicate of the system handle which is then owned by the channel so it can be released safely, regardless of whether the standard stream was redefined or not. Duplication of the handle does not cause the error because the device (console in my case, but it can be different) is not closed, just the handle to that device set as "standard handle" in the system gets closed and opened again (sometimes with different value). Besides, you can grep the code for 'GetStdHandle' to see several workarounds against closing of the standard handle when Tcl channel is closed. These workarounds could perhaps be removed after the fix. I have not touch them though because they may still be needed in other scenarios.
- login: "abv"
- mimetype: "text/x-fossil-wiki"