fix: lazily initialize DEFAULT_JUMPSTART_SAGEMAKER_SESSION (#4468) - #6330
mohamedzeidan2021 wants to merge 2 commits into
Conversation
Constructing the default JumpStart Session eagerly built ~6 boto3 clients and resolved credentials at import time, adding several seconds to import sagemaker even when the default session was never used. Replace it with a lazy proxy that defers Session construction until first use, stays truthy without initializing, forwards attribute reads/writes and copy/deepcopy, and preserves the historical fail-to-None contract.
|
Claude finished @mohamedzeidan2021's task in 2m 16s —— View job Code Review: Lazy
|
|
Claude finished @mohamedzeidan2021's task in 1m 32s —— View job Code Review: Lazy
|
_LazyJumpStartSagemakerSession declares __slots__ = () and forwards __getattr__/__setattr__ but not __delattr__. unittest.mock.patch reads the original via target.__dict__[name], which the proxy forwards to the real session's instance __dict__; for a class-level attribute such as Session.read_s3_file that raises KeyError, so mock records is_local=False and restores the attribute by calling delattr on teardown. Without __delattr__ that teardown raises AttributeError: '_LazyJumpStartSagemakerSession' object has no attribute ... and, worse, leaves the mock installed on the process-wide session, so unrelated tests in the same worker then see the mock instead of the real attribute. Forwarding __delattr__ removes the shadowing instance attribute created by patch's setattr, which makes the class-level attribute visible again. __slots__ is kept: dropping it does not fix this, because target.__dict__ would then resolve to the proxy's own empty dict and mock would still take the delattr branch. Adds two regression tests covering the class-level path (the defect) and the instance-level path (is_local=True, restored via setattr).
f56d460 to
e270d1e
Compare
|
Claude finished @mohamedzeidan2021's task in 1m 57s —— View job Code Review: Lazy
|
Issue
Fixes #4468.
DEFAULT_JUMPSTART_SAGEMAKER_SESSIONwas created at import time asSession(boto3.Session(...)), which eagerly builds ~6 boto3 clients (sagemaker, sagemaker-runtime, featurestore-runtime, metrics, s3 client + resource) and resolves credentials/region. This added several seconds toimport sagemakereven when the default session is never used.Fix
Replace the eager constant with a lazy proxy (
_LazyJumpStartSagemakerSession) that defers building the realSessionuntil it is first used. The proxy:session or DEFAULT_...fallbacks andif session:checks stay cheap (no boto clients built);copy/deepcopysoget_default_jumpstart_session_with_user_agent_suffix(which doescopy(DEFAULT_...)then mutates.boto_session/.sagemaker_client) keeps working;Nonebehavior for attribute access.The
DEFAULT_JUMPSTART_SAGEMAKER_SESSIONname and its role as a default argument are unchanged, so all ~173 existing consumers are unaffected.Validation
boto3.Session.client/.resource: importingconstantsandimage_urisnow builds 0 clients (was 6); first real attribute access builds the clients and returns a workingSession.sagemaker-core/tests/unit/jumpstart/test_constants.py(laziness, single-build caching, setattr forwarding, copy-then-mutate, fail→None). Fails without the fix (proxy class absent).sagemaker-core/tests/unit/jumpstart/: 341 passed.black/flake8clean.Backwards compatibility
No public API change. v2 maintenance backport: (companion PR against
master-v2).