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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 12 additions & 16 deletions Sources/Vapor/HTTP/Client/Application+HTTP+Client.swift
Expand Up @@ -17,25 +17,21 @@ extension Application.HTTP {
let application: Application

public var shared: HTTPClient {
let lock = self.application.locks.lock(for: Key.self)
lock.lock()
defer { lock.unlock() }
if let existing = self.application.storage[Key.self] {
return existing
} else {
let lock = self.application.locks.lock(for: Key.self)
lock.lock()
defer { lock.unlock() }
if let existing = self.application.storage[Key.self] {
return existing
}
let new = HTTPClient(
eventLoopGroupProvider: .shared(self.application.eventLoopGroup),
configuration: self.configuration,
backgroundActivityLogger: self.application.logger
)
self.application.storage.set(Key.self, to: new) {
try $0.syncShutdown()
}
return new
}
let new = HTTPClient(
eventLoopGroupProvider: .shared(self.application.eventLoopGroup),
configuration: self.configuration,
backgroundActivityLogger: self.application.logger
)
self.application.storage.set(Key.self, to: new) {
try $0.syncShutdown()
}
return new
}

public var configuration: HTTPClient.Configuration {
Expand Down