Skip to content

Commit a700ba1

Browse files
committed
Handle PySparkRuntimeError when populating globals in databricks.sdk.runtime
1 parent f2b858c commit a700ba1

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

databricks/sdk/runtime/__init__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44
from typing import Dict, Optional, Union, cast
5+
from databricks.sdk.errors import DatabricksError
56

67
logger = logging.getLogger('databricks.sdk')
78
is_local_implementation = True
@@ -73,6 +74,19 @@ def inner() -> Dict[str, str]:
7374
return None, None
7475

7576

77+
def _is_unexpected_exception_loading_user_namespace(e: Exception) -> bool:
78+
# The dbruntime module is not present outside of DBR
79+
if isinstance(e, ImportError):
80+
return False
81+
# In notebooks, the UserNamespaceInitializer works, but the notebook context is not propagated to
82+
# spawned Python subprocesses, resulting in this class throwing an
83+
# pyspark.errors.exceptions.base.PySparkRuntimeError. The SDK does not depend on PySpark, so we
84+
# need to check the type and module name directly.
85+
if type(e).__name__ == 'PySparkRuntimeError' and e.__module__ == 'pyspark.errors.exceptions.base':
86+
return False
87+
return True
88+
89+
7690
try:
7791
# Internal implementation
7892
# Separated from above for backward compatibility
@@ -85,7 +99,9 @@ def inner() -> Dict[str, str]:
8599
continue
86100
_globals[var] = userNamespaceGlobals[var]
87101
is_local_implementation = False
88-
except ImportError:
102+
except Exception as e:
103+
if _is_unexpected_exception_loading_user_namespace(e):
104+
raise DatabricksError(f"Failed to initialize runtime globals") from e
89105
# OSS implementation
90106
is_local_implementation = True
91107

0 commit comments

Comments
 (0)