https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108861
Bug ID: 108861
Summary: notify_all_at_thread_exit() should notify on cond
while lock is held to avoid a race
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: vini.ipsmaker at gmail dot com
Target Milestone: ---
Created attachment 54493
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54493&action=edit
bugfix
Releasing the mutex before the call to notify_all is an optimization. This
optimization cannot be used here. The thread waiting on the condition might
destroy the associated resources -- mutex + condition variable -- and the
notifier thread will access an destroyed variable -- the condition variable. In
fact, notify_all_at_thread_exit is meant exactly to join on detached threads,
and the waiting thread doesn't expect for the notifier thread to access any
further shared resources, making this scenario very likely to happen. The
waiting thread might awake spuriously on the release of the mutex lock. The
reorder is necessary to prevent this race.
LLVM's libcxx already fixed the issue:
https://github.com/llvm/llvm-project/commit/64fc3cd55d586498dd21c5b3cfaa755793913772