orlandos-nl / MongoKitten

Native MongoDB driver for Swift, written in Swift
MIT License
717 stars 102 forks source link

Race condition in MongoCluster when using lazy connection to SRV hostname #310

Closed Obbut closed 1 year ago

Obbut commented 1 year ago

Describe the bug

In MongoCluster, resolving of SRV hostnames is kicked off in .init(lazyConnectingTo:logger:eventLoopGroup:). This happens in asynchronously in a task:

https://github.com/orlandos-nl/MongoKitten/blob/0ca7b302498f711b4d0f10f2a8ad2ccb22a2c008/Sources/MongoClient/Cluster.swift#L167-L172

Because the result of this (which happens in resolveSettings) is not awaited before creating connections, a race condition exists: connections that are lazily created before the hostname resolution is complete, will connect to the wrong hostname.