-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-46308] Forbid recursive error handling by adding recursion guards #44210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
285b85c
test
cdkrot eaa16e6
minor
cdkrot 8b707fd
test coverage
cdkrot 3565443
Merge branch 'master' into forbid_recursive_error_handling_2
cdkrot 8ed9134
Revert "[SPARK-46241][PYTHON][CONNECT] Fix error handling routine so …
cdkrot a9433bd
slight rewrite
cdkrot c7f2130
changed to be martin way
cdkrot e4f9280
remove
cdkrot 8220c46
thread locals
cdkrot e0dfa86
whitespace
cdkrot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -544,6 +544,25 @@ def fromProto(cls, pb: pb2.ConfigResponse) -> "ConfigResult": | |
| ) | ||
|
|
||
|
|
||
| class ForbidRecursion: | ||
| def __init__(self): | ||
| self._local = threading.local() | ||
| self._local.in_recursion = False | ||
|
|
||
| @property | ||
| def can_enter(self): | ||
| return self._local.in_recursion | ||
|
|
||
| def __enter__(self): | ||
| if self._local.in_recursion: | ||
| raise RecursionError | ||
|
|
||
| self._local.in_recursion = True | ||
|
|
||
| def __exit__(self, exc_type, exc_val, exc_tb): | ||
| self._local.in_recursion = False | ||
|
|
||
|
|
||
| class SparkConnectClient(object): | ||
| """ | ||
| Conceptually the remote spark session that communicates with the server | ||
|
|
@@ -631,6 +650,8 @@ def __init__( | |
| # be updated on the first response received. | ||
| self._server_session_id: Optional[str] = None | ||
|
|
||
| self._inside_error_handling: bool = False | ||
|
|
||
| def _retrying(self) -> "Retrying": | ||
| return Retrying(self._retry_policies) | ||
|
|
||
|
|
@@ -1494,14 +1515,25 @@ def _handle_error(self, error: Exception) -> NoReturn: | |
| ------- | ||
| Throws the appropriate internal Python exception. | ||
| """ | ||
| if isinstance(error, grpc.RpcError): | ||
| self._handle_rpc_error(error) | ||
| elif isinstance(error, ValueError): | ||
| if "Cannot invoke RPC" in str(error) and "closed" in str(error): | ||
| raise SparkConnectException( | ||
| error_class="NO_ACTIVE_SESSION", message_parameters=dict() | ||
| ) from None | ||
| raise error | ||
|
|
||
| if not self._inside_error_handling: | ||
| # We are already inside error handling routine, | ||
| # avoid recursive error processing (with potentially infinite recursion) | ||
| raise error | ||
|
|
||
| try: | ||
| self._inside_error_handling = True | ||
|
||
|
|
||
cdkrot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if isinstance(error, grpc.RpcError): | ||
| self._handle_rpc_error(error) | ||
| elif isinstance(error, ValueError): | ||
| if "Cannot invoke RPC" in str(error) and "closed" in str(error): | ||
| raise SparkConnectException( | ||
| error_class="NO_ACTIVE_SESSION", message_parameters=dict() | ||
| ) from None | ||
| raise error | ||
| finally: | ||
| self._inside_error_handling = False | ||
|
|
||
| def _fetch_enriched_error(self, info: "ErrorInfo") -> Optional[pb2.FetchErrorDetailsResponse]: | ||
| if "errorId" not in info.metadata: | ||
|
|
@@ -1520,6 +1552,20 @@ def _fetch_enriched_error(self, info: "ErrorInfo") -> Optional[pb2.FetchErrorDet | |
| except grpc.RpcError: | ||
| return None | ||
|
|
||
| def _display_server_stack_trace(self) -> bool: | ||
| from pyspark.sql.connect.conf import RuntimeConf | ||
|
|
||
| conf = RuntimeConf(self) | ||
| try: | ||
| if conf.get("spark.sql.connect.serverStacktrace.enabled") == "true": | ||
| return True | ||
| return conf.get("spark.sql.pyspark.jvmStacktrace.enabled") == "true" | ||
| except Exception as e: # noqa: F841 | ||
| # Falls back to true if an exception occurs during reading the config. | ||
| # Otherwise, it will recursively try to get the conf when it consistently | ||
| # fails, ending up with `RecursionError`. | ||
| return True | ||
|
|
||
| def _handle_rpc_error(self, rpc_error: grpc.RpcError) -> NoReturn: | ||
| """ | ||
| Error handling helper for dealing with GRPC Errors. On the server side, certain | ||
|
|
@@ -1553,7 +1599,7 @@ def _handle_rpc_error(self, rpc_error: grpc.RpcError) -> NoReturn: | |
| info, | ||
| status.message, | ||
| self._fetch_enriched_error(info), | ||
| True, | ||
| self._display_server_stack_trace(), | ||
cdkrot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) from None | ||
|
|
||
| raise SparkConnectGrpcException(status.message) from None | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.