Skip to content

Commit f264a79

Browse files
committed
merge 2.9 (fixes segfault [d4ba38d00d06ebba])
2 parents b044819 + 677a934 commit f264a79

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

generic/threadCmd.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,17 @@ Init(
571571
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
572572

573573
if (tsdPtr->interp == NULL) {
574+
Tcl_Interp *tmpInterp, *mainInterp = interp;
574575
memset(tsdPtr, 0, sizeof(ThreadSpecificData));
575-
tsdPtr->interp = interp;
576+
/*
577+
* Retrieve main interpreter of the thread, only
578+
* main interpreter used as default thread-interpreter,
579+
* so no childs here, see bug [d4ba38d00d06ebba]
580+
*/
581+
while (mainInterp && (tmpInterp = Tcl_GetMaster(mainInterp))) {
582+
mainInterp = tmpInterp;
583+
}
584+
tsdPtr->interp = mainInterp;
576585
ListUpdate(tsdPtr);
577586
Tcl_CreateThreadExitHandler(ThreadExitProc,
578587
threadEmptyResult);

tests/thread.test

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,37 @@ proc ThreadReap {} {
3333
llength [thread::names]
3434
}
3535

36+
test thread-1.11 {no segfault on 2nd interpreter, bug [d4ba38d00d06ebba]} -body {
37+
# This behavior needs to be covered in a separate shell, because it doesn't expect
38+
# any other thread invocation before 2nd interpreter gets the thread::id (TSD),
39+
# but test-suite calls thread::id command (tcltest, all.tcl and thread.test):
40+
set fd [open [list |[info nameofexecutable] << [string map [list {$$load} [tcltest::loadScript]] {
41+
$$load; package require thread
42+
interp create ci
43+
set l {}
44+
ci eval {$$load; package require thread; thread::id}
45+
thread::send -async [thread::id] {lappend l ev-1}; update
46+
interp delete ci
47+
thread::send -async [thread::id] {lappend l ev-2}; update; # no SF here
48+
puts $l
49+
}] 2>@stderr] r]
50+
gets $fd
51+
} -cleanup {
52+
catch { close $fd }
53+
} -result {ev-1 ev-2}
54+
55+
test thread-1.12 {no events in 2nd interpreter, bug [d4ba38d00d06ebba]} -setup {
56+
interp create ci
57+
} -body {
58+
set l {}
59+
thread::send -async [thread::id] {lappend l ev-1}; update
60+
ci eval {package require tcltest; tcltest::loadTestedCommands; package require thread}
61+
thread::send -async [thread::id] {lappend l ev-2}; update
62+
set l
63+
} -cleanup {
64+
interp delete ci
65+
} -result {ev-1 ev-2}
66+
3667
test thread-2.0 {no global thread command} {
3768
info commands thread
3869
} {}

0 commit comments

Comments
 (0)