Tcl Source Code

View Ticket
Login
Ticket UUID: 027ba061a8aac869441267c2aef4c4bf9c705c10
Title: Standard channels do not exist on new threads on Windows
Type: Bug Created on: 2014-09-25 14:38:43
Submitter: anonymous Assigned to: jan.nijtmans
Subsystem: 69. Other Severity: Severe
Priority: 5 Medium Last modified: 2014-09-30 15:06:53
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 2.7.0
Description:
On Windows, the channels 'stdin', 'stdout' and 'stderr' do not exist on new
threads. On Linux they do. This inconsistency causes unexpected errors, for example when trying to write to those channels.

On Linux:
~~~
::thread::send $tid { ::chan names; };
% package require Thread;
2.7.0
% ::chan names;
stdin stdout stderr
% set tid [::thread::create { ::thread::wait; }];
tid0x7f555da5e700
% ::thread::send $tid { ::chan names; };
stdin stdout stderr
% 
~~~

On Windows:
~~~
% package require Thread;
2.7.0
% ::chan names;
stdout stderr stdin
% set tid [::thread::create { ::thread::wait; }];
tid0000014C
% ::thread::send $tid { ::chan names; };
% 
~~~
User Comments:
aku added on 2014-09-25 17:27:44:
Hi. Big question:

  What version of the Tcl core are you seeing this issue with ?

I just tried to reproduce things using
    Tcl 8.6.2,
    Thread 2.6.7,
 and my Windows XP was fine.

My session:

F:\Documents and Settings\andreask>tclsh
% info patch
8.6.2
% file channels
stderr stdin stdout
% pack req Thread
2.6.7
% set t [thread::create]
tid00001584
% thread::send $t {file channels}
stderr stdout stdin
%

dgp added on 2014-09-25 18:20:25:
Please check the current release of Thread: 2.7.1

anonymous added on 2014-09-26 06:27:10:
Aku, I am not able to reproduce with tclsh.exe, the behaviour appears only with wish.exe, either introducing the commands in the console or sourcing a script. The runs in the original ticket was from Wish 8.6.1 and Thread 2.7.0. The runs on this comment uses Wish 8.5.14 and Thread 2.6.7.

Dgp, I will be able to try it with Tcl 8.6.2 and Thread 2.7.1 in the next few days.

Thank you for your answers.

~~~
(tmp) 1 % puts $::tcl_patchLevel;
8.5.14
(tmp) 2 % parray ::tcl_platform;
::tcl_platform(byteOrder)   = littleEndian
::tcl_platform(machine)     = intel
::tcl_platform(os)          = Windows NT
::tcl_platform(osVersion)   = 6.1
::tcl_platform(platform)    = windows
::tcl_platform(pointerSize) = 4
::tcl_platform(threaded)    = 1
::tcl_platform(user)        = prs
::tcl_platform(wordSize)    = 4
(tmp) 3 % package require Thread;
2.6.7
(tmp) 4 % ::chan names;
stdout stderr stdin
(tmp) 5 % set tid [::thread::create { ::thread::wait; }];
tid000016F4
(tmp) 6 % ::thread::send $tid { ::chan names; };
(tmp) 7 % 
~~~

Follows a script for non-interactive sourcing:
~~~
package require Thread;
set tid [::thread::create { ::thread::wait; }];
::pack [::ttk::label .l[incr i] -text "Patchlevel: $::tcl_patchLevel"];
::pack [::ttk::label .l[incr i] -text "Platform: [array get ::tcl_platform]"];
::pack [::ttk::label .l[incr i] -text "Main thread chans: [::chan names]"];
::pack [::ttk::label .l[incr i] -text "Other thread chan names: [::thread::send $tid { ::chan names; }]"];
::pack [::ttk::label .l[incr i] -text "Other thread file chans: [::thread::send $tid { ::file channels; }]"];
~~~

anonymous added on 2014-09-26 06:28:17:
Aku, I am not able to reproduce with tclsh.exe, the behaviour appears only with wish.exe, either introducing the commands in the console or sourcing a script. The runs in the original ticket was from Wish 8.6.1 and Thread 2.7.0. The runs on this comment uses Wish 8.5.14 and Thread 2.6.7.

Dgp, I will be able to try it with Tcl 8.6.2 and Thread 2.7.1 in the next few days.

Thank you for your answers.

~~~
(tmp) 1 % puts $::tcl_patchLevel;
8.5.14
(tmp) 2 % parray ::tcl_platform;
::tcl_platform(byteOrder)   = littleEndian
::tcl_platform(machine)     = intel
::tcl_platform(os)          = Windows NT
::tcl_platform(osVersion)   = 6.1
::tcl_platform(platform)    = windows
::tcl_platform(pointerSize) = 4
::tcl_platform(threaded)    = 1
::tcl_platform(user)        = prs
::tcl_platform(wordSize)    = 4
(tmp) 3 % package require Thread;
2.6.7
(tmp) 4 % ::chan names;
stdout stderr stdin
(tmp) 5 % set tid [::thread::create { ::thread::wait; }];
tid000016F4
(tmp) 6 % ::thread::send $tid { ::chan names; };
(tmp) 7 % 
~~~

Follows a script for non-interactive sourcing:
~~~
package require Thread;
set tid [::thread::create { ::thread::wait; }];
::pack [::ttk::label .l[incr i] -text "Patchlevel: $::tcl_patchLevel"];
::pack [::ttk::label .l[incr i] -text "Platform: [array get ::tcl_platform]"];
::pack [::ttk::label .l[incr i] -text "Main thread chans: [::chan names]"];
::pack [::ttk::label .l[incr i] -text "Other thread chan names: [::thread::send $tid { ::chan names; }]"];
::pack [::ttk::label .l[incr i] -text "Other thread file chans: [::thread::send $tid { ::file channels; }]"];
~~~

anonymous added on 2014-09-26 07:02:58:
Please, note that the issue does not appear when evaluating with Tclsh and issuing [package require Tk]; only when the script is evaluated with Wish itself.

Follows the modifyied script:
~~~
package require Tk;
package require Thread;
set tid [::thread::create { ::thread::wait; }];
::pack [::ttk::label .l[incr i] -text "Patchlevel: $::tcl_patchLevel"];
::pack [::ttk::label .l[incr i] -text "Platform: [array get ::tcl_platform]"];
::pack [::ttk::label .l[incr i] -text "Main thread chans: [::chan names]"];
::pack [::ttk::label .l[incr i] -text "Other thread chan names: [::thread::send $tid { ::chan names; }]"];
::pack [::ttk::label .l[incr i] -text "Other thread file chans: [::thread::send $tid { ::file channels; }]"];
~~~

anonymous added on 2014-09-30 10:50:09:
Dgp, I have tested with Tcl 8.6.2 and Thread 2.7.1. The results are the same: no standard channels on new threads when using Wish as an interpreter (standard channels are there if using Tclsh). Please, see the typescript:

~~~
(ghttpd) 1 % parray tcl_platform;
tcl_platform(byteOrder)     = littleEndian
tcl_platform(machine)       = intel
tcl_platform(os)            = Windows NT
tcl_platform(osVersion)     = 6.1
tcl_platform(pathSeparator) = ;
tcl_platform(platform)      = windows
tcl_platform(pointerSize)   = 4
tcl_platform(threaded)      = 1
tcl_platform(user)          = prs
tcl_platform(wordSize)      = 4
(ghttpd) 2 % package require Thread;
2.7.1
(ghttpd) 3 % ::chan names;
stdout stderr stdin
(ghttpd) 4 % set tid [::thread::create { ::thread::wait; }];
tid00000FC4
(ghttpd) 5 % ::thread::send $tid { ::chan names; };
(ghttpd) 6 % 
~~~

dgp added on 2014-09-30 11:25:35:
Passing on to the best person I can think of
to handle the intersection of Windows, Tk,
and Thread.

If there's a timeline to when this problem arose,
that might help in tracking it down.

anonymous added on 2014-09-30 11:37:40:
We found it only last week. As said in other comments, present in Tcl 8.5.14-8.6.2, Thread 2.6.7-2.7.1.

Thank you.

dgp added on 2014-09-30 11:53:27:
Ok, but to further clarify, were there earlier releases
of Tcl and Thread where this did work?  Is this a regression,
or something that never worked in the first place?

It's an inconsistency between platforms, so we ought to fix
it regardless, but regressions have causes, and finding them
can lead to faster fixes.

anonymous added on 2014-09-30 12:08:47:
This came up in a new project, we don't know whether it has ever worked before.

Thank you.

dgp added on 2014-09-30 12:44:27:
Since the problem is isolated to wish, I suspect
there's something wrong with the [console].  (That
is, the bug is really over in Tk.)  Will let
nijtmans decide if he wants to move the ticket.

dgp added on 2014-09-30 12:52:57:
arjen	Hm, so new threads have no connection to stdin etc. But via wish there is no connection at all, is there?
dgp	wish ought to have std channels managed by the [console].
dgp	perhaps there's nothing in place for wish to notice new threads.
arjen	Okay, let me check
arjen	Yes, that is correct, but they are only there I presume if there is also a console
dgp	so we need some hook to get Tk_CreateConsoleWindow() called on the main interp of each new thread that [thread::create] makes?
arjen	That sounds correct
arjen	Of course that needs to be verified
dgp	Is Tk even in the [thread::create]d main interps?

jan.nijtmans added on 2014-09-30 13:39:06:

Actually, wish doesn't have stdin/stdout/stderr on windows at all, neither in the main thread neither in additional threads. But when wish is ran interactively on windows, a separate console is created which has "artificial" stdin/stdout/stderr. The code for this is - indeed - in tkConsole.c.

So, the observed difference is only noticed when wish is ran interactively, in real applications there will be no stdin/stdout/stderr at all, not in any thread. Yes, that's a difference between Linux and Windows, but it is due to the fact that Windows GUI application don't have stdin/stdout/stderr by (Microsoft's) design.

I'm not sure something should be done about this. At most some additional wrapper-code which creates the 3 channels, and maps them through to the 3 (artifical) channels in the main thread. And that should only be done when wish is in interactive mode.


anonymous added on 2014-09-30 14:10:44:
The behaviour can be replicated interactively and non-interactively, see my comment on 2014-09-26 06:28:17. The procedure: paste the code (which I include at the end of this message) in file "t.tcl", then run with "wish86.exe t.tcl".

* * *

So, the issue is three-fold:
 - On one hand there is a fundamental inconsistency between platforms (there is no std* on Windows). Not an issue, except for:
 - Tk emulates std* interactively *and non-interactively* (the latter not intended), but only on the first thread.
 - Tcl assumes the first three channels are special (the channel names even appear as "stdin", etc. when debugging). This causes issues such as those described in ticket 7d83d2193d6da63f5ba269bb9e3132074ef8212c.

The latter two points seem to me bugs in Tk and Tcl respectively, and need action to be taken. In my view, either of the following seem reasonable: a) Tk does not create any channel at all on Windows (as presumed) and Tcl does not treat first three created channels as special, or b) Tk always creates three channels (although dummy) and Tcl can keep its assumptions. The latter is probably the most platform-consistent, although there might be drawbacks I am missing ATM.

Thank you all,
Adrián.

~~~
package require Thread;
set tid [::thread::create { ::thread::wait; }];
::pack [::ttk::label .l[incr i] -text "Patchlevel: $::tcl_patchLevel"];
::pack [::ttk::label .l[incr i] -text "Platform: [array get ::tcl_platform]"];
::pack [::ttk::label .l[incr i] -text "Main thread chans: [::chan names]"];
::pack [::ttk::label .l[incr i] -text "Other thread chan names: [::thread::send $tid { ::chan names; }]"];
::pack [::ttk::label .l[incr i] -text "Other thread file chans: [::thread::send $tid { ::file channels; }]"];
~~~

dgp added on 2014-09-30 15:06:53:
Please note that it is *wish* and not Tk that supplies
the [console].