Fix IdleConnectionEvictor thread leak in long-running services#1271
Open
samikshya-db wants to merge 2 commits intomainfrom
Open
Fix IdleConnectionEvictor thread leak in long-running services#1271samikshya-db wants to merge 2 commits intomainfrom
samikshya-db wants to merge 2 commits intomainfrom
Conversation
…textFactory The feature-flags context is shared per-host but HTTP clients are scoped per-connection UUID. The old ref-count approach was broken in two ways: 1. Multiple getInstance() calls from the same connection incremented the ref-count each time (e.g. from isTelemetryEnabled()), but DatabricksConnection.close() only calls removeInstance() once. This meant the ref-count was always > 1 after close, so the context (and its scheduler) never shut down. 2. When a connection closed and its HTTP client was removed from the factory, the feature-flags context still held a reference to that connection's context (UUID). On the next 15-minute refresh, refreshAllFeatureFlags() called getClient(staleContext), which re-created a DatabricksHttpClient (and a new IdleConnectionEvictor thread) for a UUID that nobody would ever clean up. Fix: switch to UUID-based tracking (mirroring TelemetryClientFactory). - getInstance() now idempotently registers the connection UUID. - removeInstance() removes the UUID; when the set is empty the context is shut down. - When the "owner" connection (whose context is stored in the feature-flags context) closes but other connections remain, the stored connectionContext is updated to another active connection so future HTTP-client lookups resolve correctly. Closes #1221 Signed-off-by: Samikshya Chand <samikshya.chand@databricks.com> Signed-off-by: samikshya-chand_data <samikshya.chand@databricks.com>
Signed-off-by: Samikshya Chand <samikshya.chand@databricks.com> Signed-off-by: samikshya-chand_data <samikshya.chand@databricks.com>
gopalldb
reviewed
Mar 13, 2026
| @@ -70,6 +84,7 @@ public static void setFeatureFlagsContext( | |||
| contextMap.put( | |||
| key, | |||
Collaborator
There was a problem hiding this comment.
what if there is an existing holder for this key? Shall we check and shutdown that?
gopalldb
approved these changes
Mar 13, 2026
Collaborator
gopalldb
left a comment
There was a problem hiding this comment.
Added a comment on existing key, PTAL, LG otherwise
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 #1221 —
IdleConnectionEvictorthreads accumulate over time in long-running services even when all JDBC connections are properly closed.Root cause (two related bugs in
DatabricksDriverFeatureFlagsContextFactory):Ref-count overcounting:
getInstance()incremented the ref-count on every call, butisTelemetryEnabled()calls it repeatedly for the same connection whileDatabricksConnection.close()only callsremoveInstance()once. This meant the feature-flags context (and its 15-minute scheduler) never shut down after the last connection closed.Stale connection context → orphaned HTTP client: The feature-flags context is shared per-host but HTTP clients are scoped per-connection UUID. The context stored the first connection's
connectionContext. When that connection closed, its HTTP client was removed from the factory. On the next 15-min refresh,refreshAllFeatureFlags()calledDatabricksHttpClientFactory.getClient(staleContext)— the UUID was gone, socomputeIfAbsentsilently created a brand-newDatabricksHttpClient(and a newIdleConnectionEvictorthread) that nobody would ever clean up.Fix (mirrors the pattern already used correctly in
TelemetryClientFactory):FeatureFlagsContextHoldernow tracks connection UUIDs (ConcurrentHashMap<String, IDatabricksConnectionContext>) instead of a bare ref-count.getInstance()registers the UUID idempotently — no overcounting.removeInstance()removes the UUID; when the set is empty the context shuts down.connectionContextis atomically updated to another active connection so future HTTP-client lookups resolve to a live client.Test plan
DatabricksDriverFeatureFlagsContextFactoryTest— updated existing tests for new UUID-based semantics and added:testMultipleCallsWithSameConnectionAreIdempotent— single connection, multiplegetInstancecalls, oneremoveInstanceis sufficienttestMultipleConnectionsRequireAllToClose— two connections share context; closing one keeps it alive, closing both shuts it downtestConnectionContextUpdatedWhenOwnerConnectionCloses— verifies the storedconnectionContextswitches to another connection when the owner closesDatabricksDriverFeatureFlagsContextTesttests still passmvn testpasses