This is an archive of the discontinued LLVM Phabricator instance.

[LLD] Avoid exiting with a locked mutex NFC
ClosedPublic

Authored by andrewng on Jan 23 2020, 10:41 AM.

Details

Summary

In ErrorHandler::error(), rearrange code to avoid calling exitLld with
the mutex locked. Acquire mutex lock when flushing the output streams in
exitLld.

Diff Detail

Event Timeline

andrewng created this revision.Jan 23 2020, 10:41 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 23 2020, 10:41 AM
MaskRay added inline comments.Jan 23 2020, 5:18 PM
lld/Common/ErrorHandler.cpp
66

it does not seem that exitLld can be invoked concurrently.

andrewng marked an inline comment as done.Jan 24 2020, 2:51 AM

It is generally not good practice to hold a lock whilst exiting, as this has the potential to cause deadlocks. I don't believe that this situation can occur currently with LLD. However, after some recent observations, it appears that on Windows, in particular when using the static run-time, the only guaranteed way to not intermittently crash on exit is to wait for all threads to terminate (see related review D70447). Doing so would enable this deadlock situation to occur.

lld/Common/ErrorHandler.cpp
66

Yes, indeed that is another potential problem that exists and I believe that multiple threads calling _exit is also undefined behaviour.

However, this change is really to protect against flushing the streams whilst some other thread could be writing to them.

MaskRay accepted this revision.Jan 27 2020, 9:53 AM
This revision is now accepted and ready to land.Jan 27 2020, 9:53 AM
This revision was automatically updated to reflect the committed changes.