Skip to content

Return connections to the pool when reads fail and when receivers stop - #411

Merged
alberttwong merged 2 commits into
masterfrom
fix/connection-leaks
Nov 23, 2026
Merged

alberttwong merged 2 commits into
masterfrom
fix/connection-leaks

Conversation

@alberttwong

Copy link
Copy Markdown
Collaborator

Problem

Several places borrowed a Jedis connection and closed it by hand after running commands. An exception in between skipped the close() and leaked a pooled connection. Each executor keeps at most 250 connections per endpoint, so repeated failures can drain the pool.

  • RDD reads (RedisRDD.scala):
    • getKV, getHASH, getSET, getLIST and the four sorted set reads, on any error other than the WRONGTYPE that reads skip (for example NOPERM or a dropped connection);
    • the SCAN in Keys.getKeys.
  • Topology discovery (RedisConfig.scala): INFO, INFO replication and CLUSTER SLOTS.
  • RedisSourceRelation.loadSchema: it threw Unable to read dataframe schema before closing, so every DataFrame read of a table without a stored schema leaked one connection.
  • DStream receivers: each consumer thread of createRedisXStream (RedisStreamReceiver) and each list thread of createRedisStream (RedisInputDStream) borrowed a connection and never returned it. Every receiver stop or restart leaked one connection per consumer or list key.

Fix

  • The read paths, topology discovery and loadSchema use withConnection.
  • The read results were already materialized before the connection was closed (arrays and hash sets), so returning the connection inside withConnection doesn't change what is read.
  • The receiver threads close their connection in finally when their loop exits, which happens once the receiver is stopped or restarted.

Tests

  • RedisRddReadLeakSuite (standalone and cluster):
    • creates an ACL user without the read commands, so Redis rejects the reads with NOPERM;
    • repeats each failing read and asserts that the active connection count of the pool (ConnectionPool.poolFor(...).getNumActive) doesn't grow, for fromRedisKV, fromRedisHash, fromRedisSet, fromRedisList, fromRedisZSet(WithScore), fromRedisZRangeByScore(WithScore) and fromRedisKeyPattern (SCAN);
    • does the same for a RedisConfig whose INFO is denied and for a DataFrame read with no stored schema.
  • RedisReceiverConnectionLeakSuite (standalone and cluster): starts createRedisXStream with two consumers and createRedisStream with one list, then checks that the connections are borrowed while the stream runs and returned after ssc.stop().
  • Without the fix: all 22 new tests fail, with the leak growing by 3 connections per failed read (9 was not equal to 0, 18 was not equal to 9, ...).
  • With the fix: all pass against Redis 6.0.10 in Docker, standalone (with requirepass) and a 3-node cluster.
  • Regression run: the same setup ran the existing RedisRddSuite, RedisKeysSuite, RedisRddExtraSuite, RedisRddWriteErrorSuite, RedisXStreamSuite, DataframeSuite, SparkSqlSuite and RedisStreamSourceSuite: 134/134 passed.
  • Not run locally: the committed suite classes on the shared make start ports, and the SSL suites. CI covers them.

🤖 Generated with Claude Code

alberttwong and others added 2 commits November 22, 2026 16:17
Several places borrowed a connection and closed it by hand after
running commands, so an exception in between leaked the connection:

- RedisRDD reads (getKV, getHASH, getSET, getLIST, the sorted set
  reads) and the SCAN in Keys.getKeys, on any error other than the
  WRONGTYPE that reads skip;
- RedisConfig topology discovery (INFO, INFO replication, CLUSTER
  SLOTS);
- RedisSourceRelation.loadSchema, which threw "Unable to read
  dataframe schema" before closing the connection.

They now use withConnection. The DStream receivers never returned the
connection each consumer (Redis Stream) or list key (Redis List)
holds, so every receiver stop or restart leaked them; the handler
threads now close it when they exit.

Tests check the pool's active connection count after reads that Redis
rejects with NOPERM (an ACL user without the read commands), after a
RedisConfig whose INFO is denied, after a DataFrame read with no
stored schema, and after stopping both kinds of receivers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When a task of a read fails, Spark cancels the other tasks of the job,
but they keep their connection until their current command returns.
The cluster suite in CI checked the pool right after the job failed
and saw 2 connections still held by such a task. Check with
eventually instead: a returned connection shows up within moments,
while a leaked one never does (the check still fails without the fix).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alberttwong
alberttwong merged commit ff93bc5 into master Nov 23, 2026
1 check passed
@alberttwong
alberttwong deleted the fix/connection-leaks branch November 23, 2026 00:46
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.

1 participant