fix(connections): serialize concurrent pool acquire for the same key (#275) - #285
Merged
Merged
Conversation
…275) - Add PoolEntryLock to coalesce concurrent createIfAbsent calls for the same pool key so only one underlying connection is created. - Use the lock in PostgresConnectionPool, MysqlConnectionPool, and SqliteConnectionPool acquire() paths. - Increment refs after the shared creation future completes so racing callers share the same connection without orphaned sockets. - Add concurrent-acquire tests for PostgresConnectionPool.
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #275. Concurrent
acquire()calls for the same pool key could both see_pool[k] == nullwhile the first connection was still being created, resulting in duplicate sockets and orphaned connections that were never released.Changes
PoolEntryLockthat coalesces concurrentcreateIfAbsentcalls for the same key into a single shared future.PostgresConnectionPool,MysqlConnectionPool, andSqliteConnectionPool.refson the same entry so no connection is orphaned.PostgresConnectionPool(same key → one factory call; different keys → concurrent creation).Test plan
flutter test test/core/database/postgres_connection_pool_test.dartpassesCloses
Closes #275