This is an archive of the discontinued LLVM Phabricator instance.

tsan: fix failures after multi-threaded fork
ClosedPublic

Authored by dvyukov on Dec 21 2021, 12:28 AM.

Details

Summary

Creating threads after a multi-threaded fork is semi-supported,
we don't give particular guarantees, but we try to not fail
on simple cases and we have die_after_fork=0 flag that enables
not dying on creation of threads after a multi-threaded fork.
This flag is used in the wild:
https://github.com/mongodb/mongo/blob/23c052e3e321dbab90f1863d4d5539d7c1a1cf44/SConstruct#L3599

fork_multithreaded.cpp test started hanging in debug mode
after the recent "tsan: fix deadlock during race reporting" commit,
which added proactive ThreadRegistryLock check in SlotLock.

But the test broke earlier after "tsan: remove quadratic behavior in pthread_join"
commit which made tracking of alive threads based on pthread_t stricter
(CHECK-fail on 2 threads with the same pthread_t, or joining a non-existent thread).
When we start a thread after a multi-threaded fork, the new pthread_t
can actually match one of existing values (for threads that don't exist anymore).
Thread creation started CHECK-failing on this, but the test simply
ignored this CHECK failure in the child thread and "passed".
But after "tsan: fix deadlock during race reporting" the test started hanging dead,
because CHECK failures recursively lock thread registry.

Fix this purging all alive threads from thread registry on fork.

Also the thread registry mutex somehow lost the internal deadlock detector id
and was excluded from deadlock detection. If it would have the id, the CHECK
wouldn't hang because of the nested CHECK failure due to the deadlock.
But then again the test would have silently ignore this error as well
and the bugs wouldn't have been noticed.
Add the deadlock detector id to the thread registry mutex.

Also extend the test to check more cases and detect more bugs.

Diff Detail

Event Timeline

dvyukov requested review of this revision.Dec 21 2021, 12:28 AM
dvyukov created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptDec 21 2021, 12:28 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
melver accepted this revision.Dec 21 2021, 3:25 AM
This revision is now accepted and ready to land.Dec 21 2021, 3:25 AM
This revision was landed with ongoing or failed builds.Dec 21 2021, 7:54 AM
This revision was automatically updated to reflect the committed changes.