Skip to content

[clang][modules] Prevent deadlock in module cache (#182722)#12833

Merged
jansvoboda11 merged 1 commit intostable/21.xfrom
jan_svoboda/module-cache-deadlock-stable
Apr 27, 2026
Merged

[clang][modules] Prevent deadlock in module cache (#182722)#12833
jansvoboda11 merged 1 commit intostable/21.xfrom
jan_svoboda/module-cache-deadlock-stable

Conversation

@jansvoboda11
Copy link
Copy Markdown

When there's a dependency cycle between modules, the dependency scanner may encounter a deadlock. This was caused by not respecting the lock timeout. But even with the timeout implemented, leaving unsafeMaybeUnlock() unimplemented means trying to take a lock after a timeout would still fail and prevent making progress. This PR implements this API in a way to avoid UB on std::mutex (when it's unlocked by someone else than the owner). Lastly, this PR makes sure that unsafeUnlock() ends the wait of existing threads, so that they don't need to hit the full timeout amount.

This PR also implements -fimplicit-modules-lock-timeout=<seconds> that allows tweaking the default 90-second lock timeout, and adds #pragma clang __debug sleep that makes it easier to achieve desired execution ordering.

rdar://170738600
(cherry picked from commit de4a1a7)

When there's a dependency cycle between modules, the dependency scanner
may encounter a deadlock. This was caused by not respecting the lock
timeout. But even with the timeout implemented, leaving
`unsafeMaybeUnlock()` unimplemented means trying to take a lock after a
timeout would still fail and prevent making progress. This PR implements
this API in a way to avoid UB on `std::mutex` (when it's unlocked by
someone else than the owner). Lastly, this PR makes sure that
`unsafeUnlock()` ends the wait of existing threads, so that they don't
need to hit the full timeout amount.

This PR also implements `-fimplicit-modules-lock-timeout=<seconds>` that
allows tweaking the default 90-second lock timeout, and adds `#pragma
clang __debug sleep` that makes it easier to achieve desired execution
ordering.

rdar://170738600
(cherry picked from commit de4a1a7)
@jansvoboda11 jansvoboda11 requested a review from a team as a code owner April 23, 2026 20:10
@jansvoboda11
Copy link
Copy Markdown
Author

@swift-ci please test

@jansvoboda11
Copy link
Copy Markdown
Author

@swift-ci please test
swiftlang/swift#88652

@jansvoboda11
Copy link
Copy Markdown
Author

swiftlang/swift#88652
@swift-ci please test

@jansvoboda11 jansvoboda11 merged commit c34a768 into stable/21.x Apr 27, 2026
3 checks passed
@jansvoboda11 jansvoboda11 deleted the jan_svoboda/module-cache-deadlock-stable branch April 27, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants