Skip to content

fix(connections): serialize concurrent pool acquire for the same key (#275) - #285

Merged
ZhuchkaTriplesix merged 1 commit into
devfrom
issue/275-connection-pool-race
Jul 10, 2026
Merged

fix(connections): serialize concurrent pool acquire for the same key (#275)#285
ZhuchkaTriplesix merged 1 commit into
devfrom
issue/275-connection-pool-race

Conversation

@ZhuchkaTriplesix

Copy link
Copy Markdown
Member

Summary

Fixes #275. Concurrent acquire() calls for the same pool key could both see _pool[k] == null while the first connection was still being created, resulting in duplicate sockets and orphaned connections that were never released.

Changes

  • Added PoolEntryLock that coalesces concurrent createIfAbsent calls for the same key into a single shared future.
  • Applied the lock in PostgresConnectionPool, MysqlConnectionPool, and SqliteConnectionPool.
  • After the shared creation future completes, each caller increments refs on the same entry so no connection is orphaned.
  • Added concurrent-acquire tests for PostgresConnectionPool (same key → one factory call; different keys → concurrent creation).

Test plan

  • flutter test test/core/database/postgres_connection_pool_test.dart passes
  • Existing MySQL/SQLite connection tests still pass

Closes

Closes #275

…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.
@github-actions github-actions Bot added bug Something isn't working core Core library logic and services connections Database connections, URI parsing, pools labels Jul 10, 2026
@ZhuchkaTriplesix
ZhuchkaTriplesix merged commit c86fde9 into dev Jul 10, 2026
4 checks passed
@ZhuchkaTriplesix ZhuchkaTriplesix self-assigned this Jul 12, 2026
@ZhuchkaTriplesix
ZhuchkaTriplesix deleted the issue/275-connection-pool-race branch July 26, 2026 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working connections Database connections, URI parsing, pools core Core library logic and services

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant