Bug 1888660 - glibc: pthread_atfork handlers that call pthread_atfork deadlock
Summary: glibc: pthread_atfork handlers that call pthread_atfork deadlock
Keywords:
Status: CLOSED ERRATA
Alias: None
Deadline: 2022-07-04
Product: Red Hat Enterprise Linux 8
Classification: Red Hat
Component: glibc
Version: 8.2
Hardware: All
OS: Linux
unspecified
medium
Target Milestone: rc
: 8.0
Assignee: Arjun Shankar
QA Contact: Martin Coufal
Jacob Taylor Valdez
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2020-10-15 12:56 UTC by Paulo Andrade
Modified: 2022-11-08 12:17 UTC (History)
11 users (show)

Fixed In Version: glibc-2.28-206.el8
Doc Type: Bug Fix
Doc Text:
.Applications no longer deadlock when invoking `pthread_atfork` or `dclose` from fork handler callbacks Previously, applications invoked `pthread_atfork` handler callbacks while `glibc` had acquired an internal lock. As a result, registering fork handlers or calling `dclose` from a fork handler could deadlock applications. A different synchronization mechanism is now used to protect internal data structures while fork handlers are running. As a result, applications no longer deadlock when invoking `pthread_atfork` or `dclose` from fork handler callbacks.
Clone Of:
Environment:
Last Closed: 2022-11-08 10:43:11 UTC
Type: Bug
Target Upstream Version:


Attachments (Terms of Use)
hang.c (861 bytes, text/x-csrc)
2020-10-15 12:56 UTC, Paulo Andrade
no flags Details


Links
System ID Private Priority Status Summary Last Updated
Red Hat Product Errata RHBA-2022:7684 0 None None None 2022-11-08 10:43:27 UTC
Sourceware 27054 0 P2 NEW pthread_atfork handlers that call pthread_atfork deadlock 2022-05-25 11:41:36 UTC

Description Paulo Andrade 2020-10-15 12:56:46 UTC
Created attachment 1721818 [details]
hang.c

Previous versions of glibc used a different approach, now, if a thread calls
pthread_atfork from an atfork handler, it will dead lock, attempting to get
the lock, hold during run of the handler.

Looks similar, or same root cause, to issue reported at
https://sourceware.org/bugzilla/show_bug.cgi?id=24595
[[2.28 Regression]: Deadlock in atfork handler which calls dlclose]

The sample hang.c, compiled with:

$ gcc -pthread -o hang hang.c

will show the dead lock.

Comment 1 Florian Weimer 2020-10-15 14:17:17 UTC
The problem is this:

static void __attribute__((constructor)) init(void)
{
    pthread_atfork(NULL, NULL, init);
}

This recursive call into the fork subsystem now deadlocks. An upstream patch has been posted:

https://sourceware.org/bugzilla/show_bug.cgi?id=24595#c1

There were some concerns about this patch. The key point will be not to call any callback functions while implementation locks are held.

With glibc, re-registering fork handlers after fork (in the subprocess) is not necessary because they are automatically inherited. This is likely not POSIX-compliant, but is long-standing glibc behavior, and it cannot be changed. So as a workaround, you can change this:

static void __attribute__((constructor)) init(void)
{
    pthread_atfork(NULL, NULL, init);
}

to:

static void handler(void)
{
    // Real fork handler here.
}

static void __attribute__((constructor)) init(void)
{
    pthread_atfork(NULL, NULL, handler);
}

Comment 8 Carlos O'Donell 2022-05-13 17:38:01 UTC
Patches for this are now under review upstream:
https://patchwork.sourceware.org/project/glibc/patch/20220427134625.3759831-1-arjun@redhat.com/

Comment 23 errata-xmlrpc 2022-11-08 10:43:11 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (glibc bug fix and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2022:7684


Note You need to log in before you can comment on or make changes to this bug.