Skip to content

fix(spark): clean up session and port-forward on failure - #779

Open
shreya-024 wants to merge 3 commits into
kubeflow:mainfrom
shreya-024:fix/spark-connect-state-unknown-cleanup
Open

fix(spark): clean up session and port-forward on failure#779
shreya-024 wants to merge 3 commits into
kubeflow:mainfrom
shreya-024:fix/spark-connect-state-unknown-cleanup

Conversation

@shreya-024

@shreya-024 shreya-024 commented Sep 1, 2026

Copy link
Copy Markdown

What this PR does / why we need it:

Fixes the SparkConnect resource handling gap identified in #476.

  • Map unrecognised CR states to SparkConnectState.UNKNOWN instead of PROVISIONING in get_spark_connect_info_from_cr
  • Delete the SparkConnect CR on failure in create_and_connect to prevent orphaned cluster resources
  • Terminate kubectl port-forward subprocess on connection/timeout failure
  • Added "unknown status" parametrized test case inside test_get_spark_connect_info_from_cr
  • Added test_create_and_connect_cleanup_on_failure in backend_test.py

Which issue(s) this PR fixes:

@google-oss-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign electronic-waste for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

🎉 Welcome to the Kubeflow SDK! 🎉

Thanks for opening your first PR! We're happy to have you as part of our community 🚀

Here's what happens next:

  • If you haven't already, please check out our Contributing Guide for repo-specific guidelines and the Kubeflow Contributor Guide for general community standards
  • Our team will review your PR soon! cc @kubeflow/kubeflow-sdk-team

Join the community:

Feel free to ask questions in the comments if you need any help or clarification!
Thanks again for contributing to Kubeflow! 🙏

@tariq-hasan tariq-hasan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @shreya-024! I have added a few comments.

Please also follow these instructions to sign your commits: https://github.com/kubeflow/sdk/pull/779/checks?check_run_id=100012874640.

Comment thread kubeflow/spark/types/types.py Outdated
RUNNING = "Running" # Operator may set this when server is up; treated as ready
NOT_READY = "NotReady"
FAILED = "Failed"
UNKNOWN = "Unknown"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should be removed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please remove UNKNOWN = "Unknown".

state = SparkConnectState(spark_connect_cr.status.state)
except ValueError:
state = SparkConnectState.PROVISIONING
state = SparkConnectState.UNKNOWN

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please use this instead.

# Unknown indicates that the value can't be identified.
UNKNOWN = "Unknown"

Comment on lines 339 to 340

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The RUNNING state should be removed.

Suggested change
if info.state == SparkConnectState.READY:
logger.info(

Comment thread kubeflow/spark/types/types.py Outdated
@@ -32,6 +32,7 @@ class SparkConnectState(str, Enum):
RUNNING = "Running" # Operator may set this when server is up; treated as ready

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The RUNNING state should be removed.

@tariq-hasan

Copy link
Copy Markdown
Member

/ok-to-test
/retest

@tariq-hasan

Copy link
Copy Markdown
Member

/assign

Signed-off-by: shreya-024 <shreyak.2406@gmail.com>
Signed-off-by: shreya-024 <shreyak.2406@gmail.com>
@shreya-024
shreya-024 force-pushed the fix/spark-connect-state-unknown-cleanup branch from 8d74bdc to 92e552c Compare September 2, 2026 05:40

@vjkumar2756 vjkumar2756 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM
@shreya-024
I went through the changes, and the cleanup handling looks solid. Mapping unrecognized states to UNKNOWN, cleaning up the SparkConnect CR on failure, and terminating the port forward subprocess should prevent orphaned resources and state mismatches.

@tariq-hasan tariq-hasan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @shreya-024! I have added a few comments.

Comment thread kubeflow/spark/types/types.py Outdated
RUNNING = "Running" # Operator may set this when server is up; treated as ready
NOT_READY = "NotReady"
FAILED = "Failed"
UNKNOWN = "Unknown"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please remove UNKNOWN = "Unknown".

from enum import Enum
import logging
from typing import Any

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's add import kubeflow.common.constants as common_constants.

Comment thread kubeflow/spark/types/types.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's rewrite as follows.

Suggested change
state: str = common_constants.UNKNOWN

Comment thread kubeflow/spark/types/types.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same here

Suggested change
status: str = common_constants.UNKNOWN

state = SparkConnectState(spark_connect_cr.status.state)
except ValueError:
state = SparkConnectState.PROVISIONING
state = SparkConnectState(common_constants.UNKNOWN)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
state = SparkConnectState(common_constants.UNKNOWN)
state = common_constants.UNKNOWN

f"{self.namespace}/{name} (timeout: {timeout}s)"
)

time.sleep(polling_interval)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We need to be able to retry for transient errors.

Signed-off-by: shreya-024 <shreyak.2406@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SparkConnect CR cleanup behavior on failure and SDK state mismatch

3 participants