Skip to content

Stop resuming continuations while holding the pending lookup mutex in RegistryDownloadsManager/RepositoryManager - #10477

Open
plemarquand wants to merge 1 commit into
swiftlang:mainfrom
plemarquand:no-continuations-in-locks-plz
Open

Stop resuming continuations while holding the pending lookup mutex in RegistryDownloadsManager/RepositoryManager#10477
plemarquand wants to merge 1 commit into
swiftlang:mainfrom
plemarquand:no-continuations-in-locks-plz

Conversation

@plemarquand

Copy link
Copy Markdown
Contributor

RegistryDownloadsManager.lookup and RepositoryManager.lookup both wrapped a Mutex.withLock in withCheckedContinuation and then resumed the continuation inside the lock.

This is a common way to end up with deadlocked code. The code that runs after the continuation is resumed but before the lock is released can end up trying to acquire the same lock again, which will deadlock.

The section inside withCheckedContinuation was already synchronous, so the continuation wasn't even required. We can just return the value directly from withLock directly.

`RegistryDownloadsManager.lookup` and `RepositoryManager.lookup` both
wrapped a `Mutex.withLock` in `withCheckedContinuation` and then resumed the
continuation inside the lock.

This is a common way to end up with deadlocked code. The code that runs after
the continuation is resumed but before the lock is released can end up trying
to acquire the same lock again, which will deadlock.

The section inside `withCheckedContinuation` was already synchronous, so the
continuation wasn't even required. We can just return the value directly from
`withLock` directly.
@plemarquand

Copy link
Copy Markdown
Contributor Author

cc @FranzBusch

@plemarquand

Copy link
Copy Markdown
Contributor Author

@swift-ci test

return inFlight
}

let lookupTask = Task {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would normally recommend even moving this outside the lock. This is also an outcall to enqueue the task on the executor. While it doesn't cause a problem here it could potentially. I know it's a bit awkward since you gotta introduce another state which is about to start. I leave it to you if you wanna do that change or file an issue for it.

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