Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential race condition when accessing Application.http.client.shared #2576

Merged
merged 1 commit into from Mar 4, 2021

Conversation

gwynne
Copy link
Member

@gwynne gwynne commented Mar 4, 2021

The accessor for Application.http.client.shared was performing a lookup in the storage dictionary using double-checked locking, which is not actually thread-safe (despite all appearances to the contrary). It now unconditionally takes the lock before performing the lookup.

Applications making heavy use of Request.client or Application.client may experience a performance regression from this change.

…shared` accessor; the storage is a Dictionary and can potentially be mutated by one thread while being accessed from another which isn't holding the lock. This is not safe, even if the logic checks again after taking the lock. Unfortunately, this is likely to be a relatively obvious performance regression, but it's also a definitive thread safety concern.
@gwynne gwynne added bug Something isn't working semver-patch Internal changes only labels Mar 4, 2021
@gwynne gwynne requested review from tanner0101 and 0xTim March 4, 2021 13:44
@gwynne gwynne self-assigned this Mar 4, 2021
@gwynne gwynne added this to Awaiting Review in Vapor 4 via automation Mar 4, 2021
@gwynne
Copy link
Member Author

gwynne commented Mar 4, 2021

@weissi Can you double-check my understanding on this one? 😰

@weissi
Copy link
Contributor

weissi commented Mar 4, 2021

@gwynne Correct, double-checked locking is not legal in Swift (and also not in most other languages).

Copy link
Member

@0xTim 0xTim left a comment

Choose a reason for hiding this comment

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

LGTM

@gwynne gwynne merged commit 4ba5c2d into master Mar 4, 2021
@gwynne gwynne deleted the double-checked-locking-makes-cpus-sad branch March 4, 2021 15:02
Vapor 4 automation moved this from Awaiting Review to Done Mar 4, 2021
@tanner0101
Copy link
Member

These changes are now available in 4.41.3

1 similar comment
@tanner0101
Copy link
Member

These changes are now available in 4.41.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working semver-patch Internal changes only
Projects
Vapor 4
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants