Tcl Source Code

View Ticket
Login
2026-11-12
14:55
Fix [be38540b32]: a child process created by fork() could deadlock on its first allocation check-in: 7e25a68671 user: jan.nijtmans tags: core-9-0-branch
2026-11-11
14:19 • Ticket [be38540b32] Deadlock during fork in TclpAlloc status still Open with... artifact: 3cc70373d2 user: jan.nijtmans
14:19
Fix [be38540b32]: a child process created by fork() could deadlock on its first allocation check-in: a36bc8a7b6 user: jan.nijtmans tags: trunk, main
2026-10-13
16:15 • Ticket [be38540b32] Deadlock during fork in TclpAlloc status still Open with... artifact: 60272ca205 user: serhiy.storchaka
16:13 • Add attachment forkhang.c to ticket [be38540b32] artifact: f09c55f4cd user: serhiy.storchaka
16:08
Fix [be38540b32]: a child process created by fork() could deadlock on its first allocation, if anoth... closed check-in: ecb454f543 user: serhiy.storchaka tags: alloc-fork-deadlock
2020-08-18
09:40 • New ticket [be38540b32] Deadlock during fork in TclpAlloc. artifact: 1f17a6350d user: christian.nassau

Ticket UUID: be38540b321769e53e36e8d03b1a867a1df4675
Title: Deadlock during fork in TclpAlloc
Type: Bug Created on: 2020-08-18 09:40:15
Submitter: christian.nassau Assigned to: jan.nijtmans
Subsystem: 49. Threading Severity: Important
Priority: 5 Medium Last modified: 2026-11-11 14:19:34
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 9.1.0
Description:
We are seeing the following stack trace for a child process started via fork; it seems to be permanently stuck in the call to "Tcl_MutexLock(allocMutexPtr);" (implicitly triggered by TCL_TSD_INIT(&dataKey) when there is not yet thread specific data for the dataKey):

#0 0x00007f2c4e21a54d in __lll_lock_wait () from /lib64/libpthread.so.0
#1 0x00007f2c4e215e9b in _L_lock_883 () from /lib64/libpthread.so.0
#2 0x00007f2c4e215d68 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3 0x00007f2c49c59efc in TclpAlloc () from libtcl8.6.so
#4 0x00007f2c49b83088 in Tcl_Alloc () from libtcl8.6.so
#5 0x00007f2c49c59738 in Tcl_GetThreadData () from libtcl8.6.so
#6 0x00007f2c49c7a12c in AtForkChild () from libtcl8.6.so
#7 0x00007f2c48d44a9e in fork () from /lib64/libc.so.6

My guess is that the fork happened at a very unfortunate moment where the "allocMutexPtr" in the parent process was temporarily locked. Should there not be a call at the beginning of "AtForkChild" that forces "allocInit" back to zero?
User Comments:
serhiy.storchaka added on 2026-10-13 16:15:15:

Still happens with 9.1, now in GetCache() of the threaded allocator: if another thread holds a lock of the allocator when fork() is called, the child inherits the lock in the locked state, and its first allocation that needs it never returns. With the attached forkhang.c, where threads allocate and exit while another thread forks, 37 of 120000 children hung.

Proposed fix in branch alloc-fork-deadlock: hold all allocator locks across fork() with pthread_atfork().


Attachments: