diff options
author | Andrew G. Morgan <morgan@kernel.org> | 2022-04-10 15:39:14 -0700 |
---|---|---|
committer | Andrew G. Morgan <morgan@kernel.org> | 2022-04-10 15:39:14 -0700 |
commit | 7617af6b0754da00c1094215ee7828d6592f8ade (patch) | |
tree | 9ff8aaa7a0d57c6f39280650e4a686aadc183688 | |
parent | fc029cb5170361981b6d971fe56ff60f1ab10b45 (diff) | |
download | libcap-7617af6b0754da00c1094215ee7828d6592f8ade.tar.gz |
Avoid a deadlock in forked psx thread exit.
go/captree was seeing lots of libcap_psx_test processes hanging around.
It turns out that the newly added _psx_cleanup() function was deadlocking
because inside a forked processes the psx_tracker.state was _PSX_INFORK
and never _PSX_IDLE.
This completes the fix for:
https://bugzilla.kernel.org/show_bug.cgi?id=215551
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
-rw-r--r-- | psx/psx.c | 13 |
1 files changed, 10 insertions, 3 deletions
@@ -287,7 +287,9 @@ static void psx_unlock(void) } /* - * under lock perform a state transition. + * under lock perform a state transition. Changing state is generally + * done via this function. However, there is a single exception in + * _psx_cleanup(). */ static void psx_new_state(psx_tracker_state_t was, psx_tracker_state_t is) { @@ -351,7 +353,7 @@ static void _psx_forked_child(void) { * * We do this because the glibc man page for fork() suggests that * only a subset of things will work post fork(). Specifically, - * only a "async-signal-safe functions (see signal- safety(7)) + * only a "async-signal-safe functions (see signal-safety(7)) * until such time as it calls execve(2)" can be relied upon. That * man page suggests that you can't expect mutexes to work: "not * async-signal-safe because it uses pthread_mutex_lock(3) @@ -733,7 +735,12 @@ static void _psx_cleanup(void) { * never leave this state since this cleanup is only done at * program exit. */ - psx_new_state(_PSX_IDLE, _PSX_EXITING); + psx_lock(); + while (psx_tracker.state != _PSX_IDLE && psx_tracker.state != _PSX_INFORK) { + pthread_cond_wait(&psx_tracker.cond, &psx_tracker.state_mu); + } + psx_tracker.state = _PSX_EXITING; + psx_unlock(); for (ref = psx_tracker.root; ref; ref = next) { next = ref->next; |