Tcl Source Code

View Ticket
Login
Ticket UUID: 597575
Title: [exit] in sub-thread may crash.
Type: Bug Version: obsolete: 8.4.4
Submitter: andreas_kupries Created on: 2002-08-20 02:47:32
Subsystem: 49. Threading Assigned To: ferrieux
Priority: 8 Severity:
Status: Closed Last Modified: 2009-06-18 15:18:23
Resolution: Fixed Closed By: dkf
    Closed on: 2009-06-18 08:18:23
Description:
When executing the script below the tcl interpreter may
crash every once in a while (I usually have to execute
it once or twice for the problem to happen).

-----------------
package require Thread

puts [info loaded]
puts [pwd]

set t [::thread::create]
after 5000 "::thread::send $t exit"
-----------------

Basically the interpreter is shutdown from inside the
created thread, and not the main-thread, and the main
thread is running an after. Timing-depending, it seems
that the exit killed the Tcl_Release-data structure
before the after in the main thread is completely done
with its interpreter, causing it to try to release the
structure with nothing being there anymore.

Stack trace: attached, to prevent it from taking up
space in the notification mails.
User Comments: dkf added on 2009-06-18 15:18:23:

allow_comments - 1

Works with HEAD when I test with repeated runs of this line...
echo 'load /System/Library/Tcl/thread2.6/libthread2.6.dylib;set t [::thread::create]; puts [info patch],[info loaded],[package require Thread],$t;after 2000 "puts sending...;thread::send $t exit"; vwait forever' | make -C unix shell

Looks to me like you and GPS have nailed it. Great!

ferrieux added on 2009-06-18 03:02:59:
Since the patch in 2001201 has been applied, please test.

ferrieux added on 2009-06-16 04:54:42:
I strongly suspect this is a dup of 2001201 indeed.
Please review George's updated patch there.

georgeps added on 2008-08-15 10:23:03:
Logged In: YES 
user_id=585068
Originator: NO

bug report 2001201 has a patch to fix this.

"We ought to change the way the core is finalizing various subsytems. We'd need to introduce refcounts on those subsystems and do the tear down on the last-thread exit."

Why would we need that to fix the Tcl_Exit problem?  Why not just remove the full Tcl_Finalize from Tcl_Exit, as the patch associated with 2001201 does?

I agree we need the reference count solution if we want Tcl_Finalize to work in the multiple-Tcl-thread case, which it doesn't currently, but I don't know if that's reasonable or doable when Tcl_Finalize cleans up global state for all Tcl threads, and 1 or more of the threads that are not calling Tcl_Finalize may be in unknown states.  It seems simpler and more maintainable to just have threads join/exit and cleanup via Tcl_Finalize when all other threads have exited.

vasiljevic added on 2006-03-10 20:03:48:
Logged In: YES 
user_id=95086

No chances. 
Please look at the comment I just added to Bug #486399.

dgp added on 2006-03-10 11:37:26:
Logged In: YES 
user_id=80530


There's been a lot of changes
in Tcl's finalization sequence
in the last 3 years.

Any chance some of those changes
fixed this bug?

vasiljevic added on 2003-02-22 15:59:02:
Logged In: YES 
user_id=95086

Pending. As AK (se below) correctly stated, this one is 
pretty difficult to solve. We ought to change the way the 
core is finalizing various subsytems. We'd need to  
introduce refcounts on those subsystems and do the tear 
down on the last-thread exit. Now, they are finalized on the 
main-thread exit which obviously affects other thread in the 
process still using the Tcl library. 
 
I'd say, that this should still be left open, as a reminder.  
I also think that in the process of the Tcl Thread API revamp 
(which we (I) should do) this issue might be addressed.

hobbs added on 2003-02-22 01:57:00:
Logged In: YES 
user_id=72656

Zoran - what is the status of this one?

dgp added on 2002-08-21 00:02:46:
Logged In: YES 
user_id=80530


I don't know anything about the details involved
here, but the issue seems similar in spirit to me
to the problem of [exit] in a slave interp

    http://mini.net/tcl/894

The solution that feels right to me is that [exit]
should have appropriate behavior for the context
it is in.  In a master interp, ok exit the application,
but in a slave, just destroy that one interp.

andreas_kupries added on 2002-08-21 00:00:02:
Logged In: YES 
user_id=75003

Ah. Thanks. I was wondering if we had that encountered 
before, but was not sure. I guess we can close this as a 
duplicate then.

vasiljevic added on 2002-08-20 23:55:48:
Logged In: YES 
user_id=95086

Please visit the:
Bug ID: 486399
Category 80. Thread package

We did already discussed this matter.
but never really got to a solution.
As AK says, if properly documented,
one should be able to live with this.
Otherwise, we must revise the finalization
stuff and add some reference counting
and things like that. 

Zoran

dgp added on 2002-08-20 21:48:29:
Logged In: YES 
user_id=80530


Would it a reasonable solution to 
not define [exit] in interps that
are in threads other than the main
one?  Or perhaps to define [exit]
in those threads to something
that deletes the interp, and/or exits the
thread?

andreas_kupries added on 2002-08-20 10:00:55:
Logged In: YES 
user_id=75003

Guess I should also note that I consider this problem ...
diificult ... to fix, expecially with the current system for
finalizing things. Because of this I am absolutely willing
to live with the problem, if this is properly documented,
like below, or similar.

In a threaded environment the main thread of a script, i.e.
the thread created together with the tcl process is special.
Do not exit the application from one of the threads created
by the script. alsways use the main thread for this, and
only after all sub-threads have exited. Otherwise crashes
may occur during process tear-down.

andreas_kupries added on 2002-08-20 09:51:05:
Logged In: YES 
user_id=75003

I forgot that the script contains a 'vwait forever' to wait
for the after.

andreas_kupries added on 2002-08-20 09:47:33:

File Added - 29400: st

Attachments: