Skip to content

Commit 59b351e

Browse files
committed
fix(core): Respect user retry settings over SDK default
An explicit botocore Config outranks AWS_MAX_ATTEMPTS, AWS_RETRY_MODE and the shared config file. Now that SageMakerClient's config reaches the boto3 clients, the synthesized standard/10 retries default would silently override those user settings whenever no config is passed. Only synthesize the default when neither the environment nor the active profile configures retries; otherwise pass an empty Config so botocore's own precedence applies. An explicit config= still wins.
1 parent 26ee8ff commit 59b351e

2 files changed

Lines changed: 80 additions & 3 deletions

File tree

‎sagemaker-core/src/sagemaker/core/utils/utils.py‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,14 @@ def __init__(
430430
region_name = session.region_name
431431

432432
if config is None:
433-
logger.debug("No config provided. Using default config.")
434-
config = Config(retries={"max_attempts": 10, "mode": "standard"})
433+
if _retries_configured_outside_code(session):
434+
# An explicit Config outranks env vars and the shared config file,
435+
# so only supply the SDK's retry default when the user set neither.
436+
logger.debug("No config provided. Using retry settings from env/config.")
437+
config = Config()
438+
else:
439+
logger.debug("No config provided. Using default config.")
440+
config = Config(retries={"max_attempts": 10, "mode": "standard"})
435441

436442
# Keep the caller's config object alive: the cache key uses its identity.
437443
self._base_config = config
@@ -466,6 +472,26 @@ def get_client(self, service_name: str) -> Any:
466472
return getattr(self, service_name + "_client")
467473

468474

475+
_RETRY_ENV_VARS = ("AWS_RETRY_MODE", "AWS_MAX_ATTEMPTS")
476+
_RETRY_CONFIG_KEYS = ("retry_mode", "max_attempts")
477+
478+
479+
def _retries_configured_outside_code(session: Session) -> bool:
480+
"""Return True if the user set retry behaviour via env vars or the shared config file.
481+
482+
These are the two sources botocore consults for retries when no explicit
483+
``Config(retries=...)`` is passed; a synthesized one would silently override them.
484+
"""
485+
if any(os.environ.get(name) for name in _RETRY_ENV_VARS):
486+
return True
487+
try:
488+
scoped = session._session.get_scoped_config()
489+
except Exception: # pylint: disable=broad-except
490+
# e.g. ProfileNotFound; botocore will surface that on first call anyway.
491+
return False
492+
return any(key in scoped for key in _RETRY_CONFIG_KEYS)
493+
494+
469495
class ResourceIterator(Generic[T]):
470496
"""ResourceIterator class to iterate over a list of resources."""
471497

‎sagemaker-core/tests/unit/test_sagemaker_client.py‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,15 @@ def test_config_argument_is_applied_and_user_agent_suffix_appended():
134134
assert "sagemaker" in meta_config.user_agent_extra.lower()
135135

136136

137-
def test_default_config_keeps_retry_settings():
137+
@pytest.fixture
138+
def no_user_retry_settings(monkeypatch, tmp_path):
139+
"""Isolate from the developer's env vars and ~/.aws/config retry settings."""
140+
for name in ("AWS_RETRY_MODE", "AWS_MAX_ATTEMPTS", "AWS_PROFILE"):
141+
monkeypatch.delenv(name, raising=False)
142+
monkeypatch.setenv("AWS_CONFIG_FILE", str(tmp_path / "config"))
143+
144+
145+
def test_default_config_keeps_retry_settings(no_user_retry_settings):
138146
session_a = _session("AKIAFIRSTSESSION0000", REGION_A)
139147

140148
client = SageMakerClient(session=session_a)
@@ -145,6 +153,49 @@ def test_default_config_keeps_retry_settings():
145153
assert retries["mode"] == "standard"
146154

147155

156+
@pytest.mark.parametrize(
157+
"env, expected",
158+
[
159+
({"AWS_MAX_ATTEMPTS": "3"}, {"total_max_attempts": 3}),
160+
({"AWS_RETRY_MODE": "adaptive"}, {"mode": "adaptive"}),
161+
],
162+
)
163+
def test_default_config_defers_to_retry_env_vars(
164+
no_user_retry_settings, monkeypatch, env, expected
165+
):
166+
for name, value in env.items():
167+
monkeypatch.setenv(name, value)
168+
session_a = _session("AKIAFIRSTSESSION0000", REGION_A)
169+
170+
client = SageMakerClient(session=session_a)
171+
172+
retries = client.sagemaker_client.meta.config.retries
173+
for key, value in expected.items():
174+
assert retries[key] == value
175+
assert retries.get("total_max_attempts") != 11
176+
177+
178+
def test_default_config_defers_to_shared_config_retries(no_user_retry_settings, tmp_path):
179+
(tmp_path / "config").write_text("[default]\nretry_mode = adaptive\nmax_attempts = 4\n")
180+
session_a = _session("AKIAFIRSTSESSION0000", REGION_A)
181+
182+
client = SageMakerClient(session=session_a)
183+
184+
retries = client.sagemaker_client.meta.config.retries
185+
assert retries["mode"] == "adaptive"
186+
assert retries["total_max_attempts"] == 4
187+
188+
189+
def test_explicit_config_retries_win_over_env(no_user_retry_settings, monkeypatch):
190+
monkeypatch.setenv("AWS_MAX_ATTEMPTS", "3")
191+
session_a = _session("AKIAFIRSTSESSION0000", REGION_A)
192+
config = Config(retries={"max_attempts": 6, "mode": "standard"})
193+
194+
client = SageMakerClient(session=session_a, config=config)
195+
196+
assert client.sagemaker_client.meta.config.retries["total_max_attempts"] == 7
197+
198+
148199
def test_distinct_config_objects_get_distinct_clients():
149200
session_a = _session("AKIAFIRSTSESSION0000", REGION_A)
150201
config_1 = Config(connect_timeout=1)

0 commit comments

Comments
 (0)