Repository navigation
feat(session): transparently delete and recreate terminated custom-ID sessions - #208
Merged
Merged
Conversation
… sessions When ManagedSparkSession.builder.sessionId(session_id).getOrCreate() finds an existing session with that ID in a non-active state (TERMINATED, FAILED, or TERMINATING), emit a warning noting that previous Dataproc session metadata and Spark UI history will be lost (while Cloud Logging data remains), wait for termination to finish if TERMINATING, await the DeleteSession LRO, and recreate the session with the same ID.
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the behavior of named sessions in the Managed Spark Connect client. When a session with a specified ID is in a TERMINATED, FAILED, or TERMINATING state, the client now emits a warning, waits for termination if necessary, deletes the old session, and recreates a new one with the same ID. The README.md documentation and session handling logic in session.py have been updated accordingly, and comprehensive unit tests have been added to verify these changes. There are no review comments to assess, and I have no additional feedback to provide.
dborowitz
commented
Oct 19, 2026
medb
approved these changes
Oct 19, 2026
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
When
ManagedSparkSession.builder.sessionId(session_id).getOrCreate()is called with a user-specified session ID that already exists in a non-active state (TERMINATED,FAILED, orTERMINATING), previously it was impossible to reuse that session ID without manual cleanup:_get_session_by_id()skipped the non-active session without deleting it on the server, causingcreate_session()to fail with409 AlreadyExists.getOrCreate()cell also left the stale in-memoryManagedSparkSessionclient reference behind.With this change, re-running
ManagedSparkSession.builder.sessionId(session_id).getOrCreate()transparently recovers and replaces the dead session in place:Session <id> is in state <TERMINATING/TERMINATED/FAILED> and cannot be reused. Deleting the existing session and recreating it with the same ID. Previous session metadata and Spark UI history will be lost, though logs remain available in Cloud Logging.TERMINATINGsessions (if applicable) to reach a terminal state before issuingDeleteSession.DeleteSessionLRO) and recreates a fresh session with the samesession_id.existing_session.stop(terminate=False)) when a custom-ID session is no longer active or when its server-side UUID has changed, so re-runninggetOrCreate()in the same notebook kernel attaches cleanly to the new session.Demo
In the attached recording / screenshot:
1m 2.4s): Creates a named session (demo-session-e9bd11) viaManagedSparkSession.builder.sessionId(...).getOrCreate().5m 38.6s): Terminatesdemo-session-e9bd11out-of-band (Server state: TERMINATED).54.2s): Re-runsManagedSparkSession.builder.sessionId("demo-session-e9bd11").getOrCreate()with the same ID — emits the warning, deletes the terminated session, and provisions a newACTIVEsession with the same ID (New UUID: 1a751fcf-748e-4bc0-88d4-6bd1b44004ff).32.1s): Runsspark.range(5).show()against the recreated session.Testing
tests/unit/test_session.pycoveringTERMINATED,FAILED, andTERMINATINGcustom-ID session deletion/recreation, warning emission, in-kernel session replacement, and delete error propagation.us-east4) as shown in the attached capture.