dead lock in forked child

Mowry, Robert Robert.Mowry at
Tue Oct 9 17:53:32 PDT 2012

I can understand the motivation behind wanting this to work, but when I
read the POSIX spec for threaded programs that call fork(), it seems
pretty clear that malloc() and free() etc are not defined to be safe to
call in the child process:

which says in part:

A process shall be created with a single thread. If a multi-threaded
process calls fork(), the new process shall contain a replica of the
calling thread and its entire address space, possibly including the states
of mutexes and other resources. Consequently, to avoid errors, the child
process may only execute async-signal-safe operations until such time as
one of the exec 
ons is called. Fork handlers may be established by means of the
tml> function in order to maintain application invariants across fork()

The list of async-signal-safe functions is defined here:

(it's a long way down the page)

Is is that people want to go beyond what the spec calls out - and hence
the request to fix issues like this?  Or are folks not aware they are
attempting to do something that's not really kosher?


On 10/9/12 6:48 PM, "Salvatore Sanfilippo" <antirez at> wrote:

>Sorry now I realize that we just always allocate something in main()
>before doing anything else, so there is no need to touch an RC for
>Looking forward to the next jemalloc release :-)
>On Wed, Oct 10, 2012 at 12:41 AM, Salvatore Sanfilippo
><antirez at> wrote:
>> Thank you Jason, I think we'll merge the fix in the Redis 2.6 branch
>> that is currently in release candidate.
>> Cheers,
>> Salvatore
>> On Wed, Oct 10, 2012 at 12:36 AM, Jason Evans <jasone at>
>>> On Jun 1, 2012, at 11:31 AM, Jason Evans wrote:
>>> On May 25, 2012, at 2:02 AM, Jokea wrote:
>>> I've found that a forked child runs into dead lock in a multithreaded
>>> application.
>>> jemalloc calls pthread_atfork() during initialization, but the test
>>> does no allocation in the main thread before forking, and it launches
>>> threads that race with it.  It appears that one of those threads gets
>>> way through allocator initialization before the fork occurs, which
>>> the allocator in an inconsistent state (init_lock locked, but
>>> incomplete).  The simple workaround is to allocate something before
>>> A general fix in jemalloc is messy at best.  The possibilities that
>>>come to
>>> mind are 1) intercepting pthread_create() (or all fork-like system
>>> much as the lazy locking code in mutex.c does and forcing allocator
>>> initialization, or 2) using a library initializer (function specified
>>> compiler attribute to be run during library load) to force allocator
>>> initialization.  Both of these approaches are somewhat fragile;
>>> dlsym(RTLD_NEXT, Š) can break if other libraries play similar games,
>>> library initializers don't run early enough to prevent all possible
>>> failures.  In any case, I'll make a note to experiment with (2).
>>> I just committed a fix for this bug:
>>> It turned out that there were also some mutex acquire/release calls
>>> in the prefork/postfork functions, but most applications would not have
>>> exercised the code necessary to cause related deadlocks.
>>> Jason
>>> _______________________________________________
>>> jemalloc-discuss mailing list
>>> jemalloc-discuss at
>> --
>> Salvatore 'antirez' Sanfilippo
>> open source developer - VMware
>> Beauty is more important in computing than anywhere else in technology
>> because software is so complicated. Beauty is the ultimate defence
>> against complexity.
>>        ‹ David Gelernter
>Salvatore 'antirez' Sanfilippo
>open source developer - VMware
>Beauty is more important in computing than anywhere else in technology
>because software is so complicated. Beauty is the ultimate defence
>against complexity.
>       ‹ David Gelernter
>jemalloc-discuss mailing list
>jemalloc-discuss at

More information about the jemalloc-discuss mailing list