Cancel connection backoff on graceful shutdown - #187
Merged
Conversation
## Problem When a gRPC client's connection is stuck in backoff (e.g. the server is unreachable), graceful shutdown would wait out that in-flight backoff sleep before returning — so shutdown could hang for up to the max backoff (120s by default), even with zero in-flight RPCs. In practice that means processes that are slow to exit on shutdown ## Cause A subchannel that is sleeping in connection backoff has no established connection and no in-flight RPCs to drain. Its backoff `Task.sleep` was spawned into the subchannel's discarding task group, which graceful shutdown awaits rather than cancels, so `run()` (and therefore`GRPCClient.runConnections()`) did not return until the backoff elapsed — up to the default max of 120s — even with zero in-flight RPCs. This also contradicts `runConnections()`'s documented contract of returning once graceful shutdown is requested and in-flight RPCs have finished. ## Changes Spawn the backoff via `addCancellableTask` and store its handle on the `.connecting` state (mirroring how the name-resolver task handle is tracked). When `shutDown()` is called while backing off, cancel that task and finish immediately; when a connect attempt is genuinely in-flight, behaviour is unchanged. The new test fails without this fix, and passes after it.
|
|
glbrntt
approved these changes
Jul 27, 2026
glbrntt
left a comment
Collaborator
There was a problem hiding this comment.
Thanks @hamzahrmalik, nice patch.
dongjoon-hyun
added a commit
to apache/spark-connect-swift
that referenced
this pull request
Aug 4, 2026
### What changes were proposed in this pull request? This PR upgrades the `grpc-swift-nio-transport` dependency to `2.9.1`. ### Why are the changes needed? To adopt the latest `grpc-swift-nio-transport` release (`2.9.1`, 2026-08-03). - https://github.com/grpc/grpc-swift-nio-transport/releases/tag/2.9.1 - grpc/grpc-swift-nio-transport#185 - grpc/grpc-swift-nio-transport#187 ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Pass the CIs. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Fable 5 Closes #476 from dongjoon-hyun/SPARK-58542. Authored-by: Dongjoon Hyun <dongjoon@apache.org> Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
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
When a gRPC client's connection is stuck in backoff (e.g. the server is unreachable), graceful shutdown would wait out that in-flight backoff sleep before returning — so shutdown could hang for up to the max backoff (120s by default), even with zero in-flight RPCs. In practice that means processes that are slow to exit on shutdown
Cause
A subchannel that is sleeping in connection backoff has no established connection and no in-flight RPCs to drain. Its backoff
Task.sleepwas spawned into the subchannel's discarding task group, which graceful shutdown awaits rather than cancels, sorun()(and thereforeGRPCClient.runConnections()) did not return until the backoff elapsed — up to the default max of 120s — even with zero in-flight RPCs. This also contradictsrunConnections()'s documented contract of returning once graceful shutdown is requested and in-flight RPCs have finished.Changes
Spawn the backoff via
addCancellableTaskand store its handle on the.connectingstate (mirroring how the name-resolver task handle is tracked). WhenshutDown()is called while backing off, cancel that task and finish immediately; when a connect attempt is genuinely in-flight, behaviour is unchanged.The new test fails without this fix, and passes after it.