Repository navigation
Return connections to the pool when reads fail and when receivers stop - #411
Merged
Merged
Conversation
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>
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.
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.RedisRDD.scala):getKV,getHASH,getSET,getLISTand the four sorted set reads, on any error other than the WRONGTYPE that reads skip (for example NOPERM or a dropped connection);SCANinKeys.getKeys.RedisConfig.scala):INFO,INFO replicationandCLUSTER SLOTS.RedisSourceRelation.loadSchema: it threwUnable to read dataframe schemabefore closing, so every DataFrame read of a table without a stored schema leaked one connection.createRedisXStream(RedisStreamReceiver) and each list thread ofcreateRedisStream(RedisInputDStream) borrowed a connection and never returned it. Every receiver stop or restart leaked one connection per consumer or list key.Fix
loadSchemausewithConnection.withConnectiondoesn't change what is read.finallywhen their loop exits, which happens once the receiver is stopped or restarted.Tests
RedisRddReadLeakSuite(standalone and cluster):ConnectionPool.poolFor(...).getNumActive) doesn't grow, forfromRedisKV,fromRedisHash,fromRedisSet,fromRedisList,fromRedisZSet(WithScore),fromRedisZRangeByScore(WithScore)andfromRedisKeyPattern(SCAN);RedisConfigwhoseINFOis denied and for a DataFrame read with no stored schema.RedisReceiverConnectionLeakSuite(standalone and cluster): startscreateRedisXStreamwith two consumers andcreateRedisStreamwith one list, then checks that the connections are borrowed while the stream runs and returned afterssc.stop().9 was not equal to 0,18 was not equal to 9, ...).requirepass) and a 3-node cluster.RedisRddSuite,RedisKeysSuite,RedisRddExtraSuite,RedisRddWriteErrorSuite,RedisXStreamSuite,DataframeSuite,SparkSqlSuiteandRedisStreamSourceSuite: 134/134 passed.make startports, and the SSL suites. CI covers them.🤖 Generated with Claude Code