Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2014-09-30
11:27 • Closed ticket [7d83d2193d]: Standard channel restrictions apply to first channel... artifact: 7baed9681c user: dgp
10:56 • Ticket [7d83d2193d]: 4 changes artifact: 1b34fbfb6b user: anonymous
2014-09-25
14:46 • New ticket [7d83d2193d]. artifact: 326bbff738 user: anonymous

Standard channel restrictions apply to first channels opened in a new thread on Windows

Closed / Invalid7d83d2193d6da63f5ba269bb9e3132074ef8212cCreated 12 years agoUpdated 12 years ago
Descriptionplain2014-09-25 14:46:19
The absence of standard channels (see ticket
027ba061a8aac869441267c2aef4c4bf9c705c10) on new threads on Windows makes the
tree first opened channels to be taken by the core as the standard ones,
provoking spurious 'channel is shared' errors on [::thread::detach].

The context in which this issue appeared is a server where the server socket
is started in a different thread. Upon connection, connection sockets are
transferred to their own threads as well.

The following script shows the issue:
~~~
package require Thread;

# Print to standard error.
proc putsargs {args} {
    puts stderr $args;
};
# Set as error handler.
::thread::errorproc putsargs;

# Store main thread id.
::tsv::set threads main [::thread::id];

# Create thread for accepting connections.
::tsv::set threads accept [::thread::create {
    # Forward logging to main thread.
    proc putsargs {args} {
	::thread::send -async \
	    [::tsv::get threads main] \
	    [list ::putsargs {*}$args];
    }
    interp bgerror {} ::putsargs;

    # Connection handlers.
    proc _Accept {sock ip port} {
	putsargs [info level 0];
	::after 0 [list Accept $sock $ip $port];
    }
    proc Accept {sock ip port} {
	putsargs [info level 0];
	::thread::detach $sock;
    }

    set ssock [::socket -server _Accept 54321];
    putsargs "Accepting connection on port 54321 (socket=${ssock})"

    ::thread::wait;
}];
::vwait forever;
~~~

As a workaround, the following snippet should be evaluated when spawning a new
thread, so that subsequently opened channels are not taken to be the standard
ones.

~~~
    ::open nul r; # stdin
    ::open nul w; # stdout
    ::open nul w; # stderr
~~~
anonymousplain2014-09-30 10:56:10
This issue is present in Tcl 8.6.2 and Thread 2.7.1.
dgpplain2014-09-30 11:27:36
This is not a bug.  The other ticket is the bug.
Log in (anonymous is fine) to add a comment.