Skip to content
Tcl Source Code View Ticket
Login

View Ticket

segfault in exit() due to unloaded shared library

Closed / Out of Date1011715fffffffffffffffffffffffffffffffffCreated 22 years agoUpdated 19 years ago
Descriptionplain2004-08-18 19:38:33
platform: linux, gcc 2.95.3

The c-library function exit() cannot reach
C++-destructors which
has been registered via atexit() . This happens,
because tcl8.5.xxx
unloads any shared library associated with a registered
package.

example:

----------- t.cpp --------------------------
#include <tcl.h>

class X { public: ~X() {}; };

extern "C" int
T_Init( Tcl_Interp * interp )
{
    static X x;
    return TCL_OK;
}
---------------------------------------------
make a shared library:

g++ -o libt.so -shared -fPIC -I../generic t.cpp

Then, in tclsh: "load libt.so; exit" ends up with a
core dump.

(1) load libt.so load the dll and calls T_Init()
(2) g++ registers an exit-handler calling atexit() after
     constructing the static object x of class X.
(3) at end: TclpUnloadFile() calls dlclose() for this
     package
(4) Tcl calls exit() 
(5) exit() calls the exit-handler registered in step (2),
     which unfortunately has been unloaded in step (3)

The unload behavior is controlled by the macro
TCL_UNLOAD_DLLS.

The current snapshot's Makefile.in defines this macro
as -DTCL_UNLOAD_DLLS=1

Having -DTCL_UNLOAD_DLLS=0 as default would 
ensure compatibility with previous versions 
(no implicit unload).

-- [email protected]
kennykbplain2004-08-20 02:39:23
Logged In: YES 
user_id=99768

Donal,  this appears to be TIP#100 stuff.
It appears that the stanza of code beginning at line 1155
of tclLoad.c is unloading all packages, irrespective of
whether they support a Foo_Unload entry point, and
therefore unloading packages that aren't designed to
be unloaded.

Should the test for pkgPtr->fileName[0] perhaps
also include a test that pkgPtr->unloadProc is non-NULL,
to indicate that the package expects to be unloaded
early?
dkfplain2004-08-20 03:22:51
Logged In: YES 
user_id=79902

TIP#100 is silent on the exact topic.  :^(
kennykbplain2004-09-08 04:55:52
Logged In: YES 
user_id=99768

Joe, do you think it'll take a TIP to fix this?  We don't define
a Foo_Unload for process exit, and the interpreter is no
longer available.  Any thoughts?
dkfplain2004-09-08 05:25:12
Logged In: YES 
user_id=79902

Deeper consideration: It would be easy to either not unload
DLLs at all on Finalize or only unload the DLLs on finalize
if they support unloading at other times. But we need input
from someone like Joe Mistachkin on this; IIRC he's one of
the (potential?) users of unloading, and might want the
facility for non-Tcl-aware libs too...

I've insufficient experience to help here (I prefer to
consider C++ as part of the problem space, a bit like PL/1
was reputed to be.)
dkf Ticket [1011715fff]: 1 change [History diff]2004-09-08 15:19:26
mistachkinplain2004-10-10 02:38:01
Logged In: YES 
user_id=113501

It would seem from reading the description of the problem
above that gcc expects the atexit() handlers to be called
prior to the DLL being unloaded?  Is there any way we can
figure out those handlers and call them ourselves?

Perhaps we can "hook" atexit(), figure out which DLL invoked
it, and simply call the handlers ourselves prior to
unloading the DLL in question.
dkfplain2004-12-23 23:43:36
Logged In: YES 
user_id=79902

xref Tcl bug 1082283 maybe?
dgpplain2005-11-02 00:22:14
Logged In: YES 
user_id=80530


Have finalization fixes since the
latest comments resolved this?
dgp Ticket [1011715fff]: 1 change [History diff]2006-03-10 13:43:47
dgpplain2007-10-25 22:29:56
Logged In: YES 
user_id=80530
Originator: NO

status?
dgpplain2007-12-04 01:47:02
Logged In: YES 
user_id=80530
Originator: NO


Is there still an 8.5.0-blocking
issue here?
dgpplain2007-12-07 02:33:05
Logged In: YES 
user_id=80530
Originator: NO


I can't block for a bug, no matter
how serious, if there's apparently
no one working on a solution.
georgepsplain2007-12-18 01:33:59
Logged In: YES 
user_id=585068
Originator: NO

For what it's worth I can't duplicate this in Ubuntu 7.x with gcc 4.1.2 and  /usr/lib/libstdc++.so.6 -> libstdc++.so.6.0.8

I suspect based on some searching, that this issue was fixed in libstdc++ by using cxa_finalize when a library is unloaded/dlclose()

Also, a minor clarification; the code generated by g++ is calling cxa_atexit, and not atexit().

So, I suggest unless there is further input from the submitter, that this bug be closed.
dgp Pending ticket [1011715fff]. [History diff]2007-12-18 01:59:36
sf-robotplain2008-01-01 10:20:07
Logged In: YES 
user_id=1312539
Originator: NO

This Tracker item was closed automatically by the system. It was
previously set to a Pending status, and the original submitter
did not respond within 14 days (the time period specified by
the administrator of this Tracker).
Log in (anonymous is fine) to add a comment.