This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Make Python initialization atomic
ClosedPublic

Authored by JDevlieghere on Jan 18 2022, 1:34 PM.

Details

Summary

We got a few crash reports that showed LLDB initializing Python on two separate threads. Make sure Python is initialized exactly once.

rdar://87287005

Diff Detail

Event Timeline

JDevlieghere requested review of this revision.Jan 18 2022, 1:34 PM
JDevlieghere created this revision.
JDevlieghere edited the summary of this revision. (Show Details)
mib accepted this revision.Jan 18 2022, 2:15 PM

LGTM

This revision is now accepted and ready to land.Jan 18 2022, 2:15 PM

I don't think that atomic<bool> is what you want here. In the case of a race, the "loser" will immediately continue to use python as if it was initialized, even though the winner has not finished the initialization. You most likely need call_once semantics, blocking all threads until the initialization completes.

That said, I think think it would be better to do this initialization in the Initialize static function. Out of general cleanliness, but with a particular with a view towards the SIGINT patch. That way the initialization functions happens in a predictable and single-threaded context (as you can see, threads are hard), hopefully at a point where nobody will care that we're mucking with the SIGINT handlers.

JDevlieghere planned changes to this revision.Jan 18 2022, 11:38 PM

I don't think that atomic<bool> is what you want here. In the case of a race, the "loser" will immediately continue to use python as if it was initialized, even though the winner has not finished the initialization. You most likely need call_once semantics, blocking all threads until the initialization completes.

That said, I think think it would be better to do this initialization in the Initialize static function. Out of general cleanliness, but with a particular with a view towards the SIGINT patch. That way the initialization functions happens in a predictable and single-threaded context (as you can see, threads are hard), hopefully at a point where nobody will care that we're mucking with the SIGINT handlers.

Makes sense. I’ll update the patch tomorrow.

JDevlieghere edited the summary of this revision. (Show Details)

Do private initialization exactly once in ScriptInterpreterPython::Initialize

This revision is now accepted and ready to land.Jan 19 2022, 8:56 AM
JDevlieghere added inline comments.Jan 19 2022, 8:57 AM
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
3154–3176

I just moved this into the existing anonymous namespace above.

labath accepted this revision.Jan 19 2022, 9:15 AM

LG, thanks

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptJan 19 2022, 9:57 AM