Skip to content

feat(session): transparently delete and recreate terminated custom-ID sessions - #208

Merged
dborowitz merged 1 commit into
mainfrom
dborowitz-recreate-session
Oct 20, 2026
Merged

dborowitz merged 1 commit into
mainfrom
dborowitz-recreate-session

Conversation

@dborowitz

@dborowitz dborowitz commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

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, or TERMINATING), 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, causing create_session() to fail with 409 AlreadyExists.
  • If the session terminated unexpectedly during or after a prior execution in the same notebook kernel (e.g., due to idle TTL expiration, driver failure, or out-of-band termination), re-running the getOrCreate() cell also left the stale in-memory ManagedSparkSession client reference behind.

With this change, re-running ManagedSparkSession.builder.sessionId(session_id).getOrCreate() transparently recovers and replaces the dead session in place:

  1. Logs a warning:
    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.
  2. Waits for TERMINATING sessions (if applicable) to reach a terminal state before issuing DeleteSession.
  3. Deletes the terminated session (awaiting the DeleteSession LRO) and recreates a fresh session with the same session_id.
  4. Cleans up any stale local client state (existing_session.stop(terminate=False)) when a custom-ID session is no longer active or when its server-side UUID has changed, so re-running getOrCreate() in the same notebook kernel attaches cleanly to the new session.

Demo

In the attached recording / screenshot:

  1. Cell 1 (1m 2.4s): Creates a named session (demo-session-e9bd11) via ManagedSparkSession.builder.sessionId(...).getOrCreate().
  2. Cell 2 (5m 38.6s): Terminates demo-session-e9bd11 out-of-band (Server state: TERMINATED).
  3. Cell 3 (54.2s): Re-runs ManagedSparkSession.builder.sessionId("demo-session-e9bd11").getOrCreate() with the same ID — emits the warning, deletes the terminated session, and provisions a new ACTIVE session with the same ID (New UUID: 1a751fcf-748e-4bc0-88d4-6bd1b44004ff).
  4. Cell 4 (32.1s): Runs spark.range(5).show() against the recreated session.
image

Testing

  • Added unit tests in tests/unit/test_session.py covering TERMINATED, FAILED, and TERMINATING custom-ID session deletion/recreation, warning emission, in-kernel session replacement, and delete error propagation.
  • Verified end-to-end in a live notebook (us-east4) as shown in the attached capture.

… 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.
@dborowitz
dborowitz requested a review from medb October 19, 2026 19:28

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread google/cloud/managed_spark_connect/session.py
@dborowitz
dborowitz merged commit 24415cf into main Oct 20, 2026
12 checks passed
@dborowitz
dborowitz deleted the dborowitz-recreate-session branch October 20, 2026 19:23
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.

2 participants