Skip to content

Commit 3e5c3dc

Browse files
committed
Address warning reported by thread safety analyzer
Motivation: Thread safety analyzer reports warning about observable state leaving lock guarded code blocks. This PR address the warnings. Analysis of the warning does not prove that we have a real bug and all the warnings are considered as potential bugs. Analyzer used is: `docker run --rm -v $(pwd):/src -w /src swift:5.3.1 swift test -c release --sanitize=thread --enable-test-discovery` Modifications: * accessor to count of connections in connection pool is guarded by lock * forced materialization of a potentially lazy collection into an array before returning the collection from lock guarded code block Result: Most of thread safety warnings are addressed without modification of observable code behaviour.
1 parent 0a8dddb commit 3e5c3dc

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Diff for: Sources/AsyncHTTPClient/ConnectionPool.swift

+7-2
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,19 @@ final class ConnectionPool {
103103

104104
func close(on eventLoop: EventLoop) -> EventLoopFuture<Bool> {
105105
let providers = self.lock.withLock {
106-
self.providers.values
106+
// we materialize Values into an Array before returning from under a lock
107+
// as thread safety analyzer considers safety of Values as undefined and dependant on implementation
108+
// e.g. a lazy collection implementation could by thread unsafe to be returned from within a lock
109+
Array(self.providers.values)
107110
}
108111

109112
return EventLoopFuture.reduce(true, providers.map { $0.close() }, on: eventLoop) { $0 && $1 }
110113
}
111114

112115
var count: Int {
113-
return self.providers.count
116+
return self.lock.withLock {
117+
return self.providers.count
118+
}
114119
}
115120

116121
/// Used by the `ConnectionPool` to index its `HTTP1ConnectionProvider`s

0 commit comments

Comments
 (0)