Skip to content

Create each endpoint's JedisPool once under concurrent first use - #407

Merged
alberttwong merged 1 commit into
masterfrom
fix/issue-90-connection-pool-thread-safety
Nov 23, 2026
Merged

alberttwong merged 1 commit into
masterfrom
fix/issue-90-connection-pool-thread-safety

Conversation

@alberttwong

Copy link
Copy Markdown
Collaborator

Fixes #90.

Problem

ConnectionPool.connect used pools.getOrElseUpdate(re, { new JedisPool(...) }) through JavaConversions. On Scala 2.12 the implicit conversion gives a concurrent.Map wrapper, and its getOrElseUpdate is get, then build the value, then putIfAbsent. The map stays consistent and every caller gets the same pool. But each thread that misses the get runs the factory, so when several tasks in one executor reach a new endpoint at once, several JedisPools are built and all but one are dropped without close().

The dropped pools never served a connection, so no connections leak. But each one stays registered in JMX (commons-pool2 enables JMX by default) and keeps its evictor task running every 30s for the life of the JVM. On Scala 2.11, which the reporter used, getOrElseUpdate was get + update (last writer wins), so callers could also get different pools.

Fix

  • A new poolFor(re) does a lock-free pools.get(re) first. On a miss it calls pools.computeIfAbsent(re, createPool), which runs createPool at most once per endpoint.
  • The plain get keeps the common path lock-free; on Java 8, computeIfAbsent locks the bin even when the key exists.
  • The pool settings are unchanged; they just moved into createPool.

Tests

  • New ConnectionPoolThreadSafetyTest (no Redis needed): 16 threads race poolFor on 10 fresh endpoints. The test asserts they all get the same instance and that exactly one pool per endpoint is registered in JMX.
    • With the old getOrElseUpdate logic it fails: 16 was not equal to 1.
    • With the fix it passes.
  • Smoke test against Redis 8.2: 32 threads call connect() concurrently, then SET and PING. All succeed through one pool.
  • Not run locally: the existing Redis-backed suites, which need the shared make start setup. CI covers them.

Making the pool size configurable (also mentioned in #90) is left as a separate feature request.

🤖 Generated with Claude Code

ConnectionPool.connect called getOrElseUpdate on its ConcurrentHashMap
through JavaConversions, which wraps it in a JConcurrentMapWrapper. On
Scala 2.12 that method is concurrent.Map.getOrElseUpdate: get, then run
the factory, then putIfAbsent. Every caller agrees on one pool, but each
task that reaches a new endpoint at the same moment builds its own
JedisPool first. The pools that lose the putIfAbsent are dropped without
close(), so they stay registered in JMX (which keeps them reachable)
with their evictor running for the life of the executor. With 16
threads racing on one endpoint, 16 pools were built and 15 leaked.

Use ConcurrentHashMap.computeIfAbsent, which runs the factory at most
once per key, behind a plain get so the common case stays lock-free on
Java 8 (where computeIfAbsent locks even when the key is present). Pool
creation moves into its own method, poolFor is private[redis] so it can
be tested, and the JavaConversions import is gone.

ConnectionPoolThreadSafetyTest needs no Redis server: it races 16
threads on fresh endpoints and checks they share one pool and that only
one pool was registered in JMX.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alberttwong
alberttwong merged commit 421b102 into master Nov 23, 2026
1 check passed
@alberttwong
alberttwong deleted the fix/issue-90-connection-pool-thread-safety branch November 23, 2026 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: ConnectionPool is not threadsafe

1 participant