Tcl Source Code

View Ticket
Login
Ticket UUID: 2981154
Title: async.test segfault
Type: Bug Created on: 2010-04-02 15:11:17
Submitter: dgp Assigned to: andreas_kupries
Subsystem: 49. Threading Severity:
Priority: 9 Immediate Last modified: 2011-08-19 22:25:22
Status: Closed Closed by: ferrieux
Resolution: Fixed Closed on: 2011-08-19 14:04:34
Version: obsolete: 8.5.8
Description:
Using the 8.5.8 sources (so I rule out more recent commits)
and with a --enable-threads --enable-symbols build, I
sometimes see a segfault at the end of async.test.

I managed to catch it in gdb:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x40646b90 (LWP 825)]
0x0814bcfb in Tcl_MutexLock (mutexPtr=0x10)
    at /home/dgp/cvs/tcl8.5/unix/../unix/tclUnixThrd.c:519
519         if (*mutexPtr == NULL) {


(gdb) bt
#0  0x0814bcfb in Tcl_MutexLock (mutexPtr=0x10)
    at /home/dgp/cvs/tcl8.5/unix/../unix/tclUnixThrd.c:519
#1  0x08067c88 in Tcl_AsyncMark (async=0x8e0b274)
    at /home/dgp/cvs/tcl8.5/unix/../generic/tclAsync.c:167
#2  0x08054baf in AsyncThreadProc (clientData=0x8e16c18)
    at /home/dgp/cvs/tcl8.5/unix/../generic/tclTest.c:936
#3  0x080c1fbd in NewThreadProc (clientData=0xbbe12f0)
    at /home/dgp/cvs/tcl8.5/unix/../generic/tclEvent.c:1487
#4  0x0088773b in start_thread () from /lib/libpthread.so.0
#5  0x007dccfe in clone () from /lib/libc.so.6


Don't know whether this is another symptom of 
Tcl Bug 2869384.

I believe that when the segfault happens, it is
always after test async-4.3 has failed
(Tcl Bug 1774689).  However, async-4.3
can also fail without a segfault following.
User Comments:
dgp added on 2011-04-14 22:39:52:
FWIW, I just saw this one again on trunk tip.

ferrieux added on 2011-08-19 04:44:01:
Quoting mistachkin in 1774689:

   To see this test fail reliably, simply change the line in proc hang3
   from: [string repeat {;incr i;} 1500000] to: [string repeat {;incr i;}
   150]

doing this I get it reliably on trunk tip.

ferrieux added on 2011-08-19 05:14:34:
I also get non-systematic segfault, but there're news: the non-segfaulting is correlated to Tcl_AsyncMark *not* being called. It's an iff. So there's a race condition that hides the segfault by not letting the thread go far enough.

ferrieux added on 2011-08-19 05:51:33:
Probability of segfault gets close to 1 with 15 instead of 150 in the modified loop.
Theory: the asyncmark may happen:
  (a) during the loopless stretch of BC 
  (b) during the main interp's teardown
  (c) not at all, since the process has exited
Clearly only the gray area (b) segfaults. I guess during teardown things are not as airtight as during normal operation.

ferrieux added on 2011-08-19 05:59:27:
Come to think of it, as mistachkin noted, the whole test setup is geared towards (a), ie there's absolutely no effort to be teardown-proof. So playing with too small stretches of BC is completely out of the intended range.
Hence, I believe the proper fix is to increase the size of the loopless BC stretch in that test.

ferrieux added on 2011-08-19 13:56:14:
To clarify: in the comments below, "teardown" is the test's cleanup part which does Tcl_AsyncDelete.
So maybe there's a race condition between Tcl_AsyncDelete and the real invocation of an already Tcl_AsyncMark'ed handler. In case this is true, it ought to be fixed generically rather than pulled under the rug.

ferrieux added on 2011-08-19 13:59:59:
OK, the previous hypothesis is proven wrong: the bug is really tclTest.c's lack of protection of its interthread data access. Will add a mutex.

ferrieux added on 2011-08-19 21:04:34:
Fixed in HEAD, by (1) mutex-protecting the process-wide static holding the list of TestAsyncHandlers (along with the unique id counter ;-), and (2) moving from a pointer-to-struct to an integer-id API in async handlers. The mutex-protected lookup by id is done twice: once in the alien thread, as a prerequisite to calling AsyncMark with the proper token; and once in the async handler, since the handler may have been deleted between the two.
Passes test suite. Verified that even wit the 1500000->15 tweak, even though the test consistently fails, it never segfaults.

dgp added on 2011-08-19 21:35:40:
Is the patch suitable for backport
to 8.5.11 ?

ferrieux added on 2011-08-19 22:25:22:
Backported.