Tcl Source Code

View Ticket
Login
Ticket UUID: 35a839cba8a4e6bbdcd9567f958d56076fe5bd0
Title: Can not reinitialize Tcl after Tcl_Finalize()
Type: Bug Created on: 2014-04-14 16:38:04
Submitter: anonymous Assigned to: ferrieux
Subsystem: 50. Embedding Support Severity: Important
Priority: 5 Medium Last modified: 2015-05-14 04:47:23
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.6.1
Description:
Initialize Tcl and then call Tcl_Finalize().
Now try to re-initialize Tcl, with Tcl_FindExecutable()
This gives "TclInitSubsystems called while exiting" followed by an abort().

This used to work prior to Tcl/Tk 8.6.

Looking at the code, my guess is that this is related to the variable "inExit" in tclEvent.c (not in 8.5.15). Its comment says "This variable is set to 1 when Tcl_Exit is called" and it is never reset to zero.

In 8.5.15, there is a similar variable "inFinalize" (gone in 8.6.1) with the comment "This variable is set to 1 when Tcl_Finalize is called, and at the end of its work, it is reset to 0.".

Note the difference, the new variable "inExit" is never reset to 0.

The backtrace says:
0   libsystem_kernel.dylib        	0x00007fff91445ce2 __pthread_kill + 10
1   libsystem_c.dylib             	0x00007fff8ec8a7d2 pthread_kill + 95
2   libsystem_c.dylib             	0x00007fff8ec7ba7a abort + 143
3   Tcl                           	0x0000000107f88c92 Tcl_PanicVA + 449
4   Tcl                           	0x0000000107f88d34 Tcl_Panic + 162
5   Tcl                           	0x0000000107f50927 TclInitSubsystems + 30
6   Tcl                           	0x0000000107f47de3 Tcl_FindExecutable + 17
User Comments:
dgp added on 2014-07-23 13:50:59:
ferrieux, I think you were involved with the
finalization reforms of Tcl 8.6.  Can you give
this a look?

Could be a simple fix of a simple bug, or it could
be a chance to record here why things changed in
this way.

ferrieux added on 2014-08-25 22:19:58:
Naive one-liner below just resets the flag at the end of Tcl_Finalize.
Test suite ok even with TCL_FINALIZE_ON_EXIT=1, but that's hardly a
proof of anything, since I guess no test does this (finalize and start
over). Maybe you have the proper wrench at hand ?

Index: generic/tclEvent.c
==================================================================
--- generic/tclEvent.c
+++ generic/tclEvent.c
@@ -1245,10 +1245,15 @@

     TclFinalizeMemorySubsystem();

   alreadyFinalized:
     TclFinalizeLock();
+
+    /*
+     *  In case someone wants to start over, permit it.
+     */
+    inExit = 0;
 }


 /*
  *----------------------------------------------------------------------
  *

dgp added on 2014-08-26 02:24:42:
That suggests "safe" but doesn't demonstrate "effective".

I'd like a demo that fails before and passes after the patch,
(aka fails in 8.6 but works in 8.5) and so far I'm failing to get that.

dgp added on 2014-08-26 11:58:20:
In my own attempts to test, it appears that
reinitialize after finalization has been failing
since

http://core.tcl.tk/tcl/info/be83fe62c8

I don't see any evidence that it ever worked
in a stable Tcl 8.5.* release.

It would be great to fix this, but I don't
have high hopes.  My impression is that folks
have given up on getting Tcl_Finalize() right,
to the point where they have taken big steps to
avoid calling it at all in Tcl 8.6.

ferrieux added on 2014-08-26 13:23:57:
I do confirm that the net result of the "Bug 2001201 exit reform" is to avoid calling Finalize in the normal exit path; but the reason is more subtle than "it's broken and I can't be bothered to fix it". Rather, it is the sum of two things:

 - exit() is really the fastest way of getting out, because it implies (a) bulk memory freeing (the mappings just disappear, without  crawling whatever the userland thinks its heap contains) and (b) bulletproof release of kernel resources (like fds, mutexes, etc). The OOP habit of recursively calling the Destructor of every single living form in the universe is a suboptimal way of exiting when there is no compulsory app-level cleanup to do.

 - multithread finalization can lead to deadlocks, and there is no way to avoid them with certainty, short of exit() itself.

Note the "reform" only says that [exit] will shun Finalize() (unless the TCL_FINALIZE_ON_EXIT env var is set to 1); it says nothing about what embedders do. And it did not sabotage the Finalize() function itself ;)

bdrewery added on 2015-04-10 21:42:54:
Ping? I've hit this in a library I am working on that reinitializes TCL.

dgp added on 2015-05-11 19:00:49:
bdrewery, can you supply the requested demo then?

We don't want to just blindly patch.
We'd rather have a short demo program that fails before
and succeeds after the patch to demonstrate the bug and
the fix.

Also useful would be an identification of prior releases of
Tcl that were not broken in the way that is giving you
trouble.  So far, those claims have not been able to be
reproduced.

Thanks.

mistachkin added on 2015-05-14 04:47:23:
Having also worked on finalization in the past, I certainly think the inExit flag
needs to be reset.

In my testing with Eagle on Windows (i.e. when it repeatedly loads and unloads
the native Tcl library), I do not encounter any issues when only dealing with
Tcl itself (i.e. no Tk).  I *think* this is due to me completely unloading the
library from memory after calling Tcl_Finalize.