Skip to content

Fix multithreaded fork on OS X and add a test.#954

Merged
davidtgoldblatt merged 2 commits intojemalloc:devfrom
davidtgoldblatt:zone_fix
Jul 11, 2017
Merged

Fix multithreaded fork on OS X and add a test.#954
davidtgoldblatt merged 2 commits intojemalloc:devfrom
davidtgoldblatt:zone_fix

Conversation

@davidtgoldblatt
Copy link
Contributor

This resolves #895.

@davidtgoldblatt davidtgoldblatt requested a review from djwatson July 10, 2017 21:11
@davidtgoldblatt
Copy link
Contributor Author

@alexcrichton, could you confirm that this fixes things on your end?

src/zone.c Outdated
} else {
jemalloc_postfork_child();
}
zone_force_lock_pid = 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some reason this is 1 instead of -1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, typo. Thanks.

On OS X, we rely on the zone machinery to call our prefork and postfork
handlers.

In zone_force_unlock, we call jemalloc_postfork_child, reinitializing all our
mutexes regardless of state, since the mutex implementation will assert if the
tid of the unlocker is different from that of the locker.  This has the effect
of unlocking the mutexes, but also fails to wake any threads waiting on them in
the parent.

To fix this, we track whether or not we're the parent or child after the fork,
and unlock or reinit as appropriate.

This resolves jemalloc#895.
Copy link

@djwatson djwatson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm assuming it fixes the issue.

@alexcrichton
Copy link

Works for me 👍

Thanks @davidtgoldblatt!

#endif

static void
wait_for_child_exit(int pid) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like some of this needs more define guards

Forking a multithreaded process is dangerous but allowed, so long as the child
only executes async-signal-safe functions (e.g. exec).  Add a test to ensure
that we don't break this behavior.
@davidtgoldblatt davidtgoldblatt merged commit fb6787a into jemalloc:dev Jul 11, 2017
@davidtgoldblatt davidtgoldblatt deleted the zone_fix branch July 11, 2017 01:17
nicktrav added a commit to nicktrav/cockroach that referenced this pull request Dec 8, 2022
Update jemalloc to point to the upstream 5.3.0 release, hosted on our
internal fork. This removes two custom patches that are no longer
required:

- Fix deadlock in multithreaded fork in OS X - fix upstreamed in
  jemalloc/jemalloc#954.
- Fix JEMALLOC_MUTEX_INIT_CB to only be set if OSS_PINLOCK is false -
  spinlock support was removed upstream in jemalloc/jemalloc#1367

Touches cockroachdb#83289.

Epic: CRDB-20293.

Release note: None.
nicktrav added a commit to nicktrav/cockroach that referenced this pull request Jan 5, 2023
Update jemalloc to point to the upstream 5.3.0 release, hosted on our
internal fork. This removes two custom patches that are no longer
required:

- Fix deadlock in multithreaded fork in OS X - fix upstreamed in
  jemalloc/jemalloc#954.
- Fix JEMALLOC_MUTEX_INIT_CB to only be set if OSS_PINLOCK is false -
  spinlock support was removed upstream in jemalloc/jemalloc#1367

Touches cockroachdb#83289.

Epic: CRDB-20293.

Release note: None.
craig bot pushed a commit to cockroachdb/cockroach that referenced this pull request Jan 6, 2023
93045: c-deps: update jemalloc to 5.3.0 r=rickystewart a=nicktrav

Update jemalloc to point to the upstream 5.3.0 release, hosted on our internal fork. This removes two custom patches that are no longer required:

- Fix deadlock in multithreaded fork in OS X - fix upstreamed in jemalloc/jemalloc#954.
- Fix JEMALLOC_MUTEX_INIT_CB to only be set if OSS_PINLOCK is false - spinlock support was removed upstream in jemalloc/jemalloc#1367

Touches #83289.

Closes #17013. Closes #83289.

Epic: CRDB-20293.

Release note (performance improvement): The memory allocator, `jemalloc` was updated to the latest available upstream version, 5.3.0, from 4.5.0. This update pulls in a number of upstream improvements, including reduced memory fragmentation for memory allocated outside of the Go runtime (i.e. the Pebble block and table caches), resulting in better memory utilization.

Co-authored-by: Nick Travers <travers@cockroachlabs.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deadlock with multithreaded fork on OSX 10.12

3 participants