I need help with this recursion error maximum recursion depth exceeded from boto3. This happens when I initialize an s3 client in my inference script to allow me read s3 objects. Your insights will be deeply appreciated! Similar issue posted 2 months ago here: AWS SageMaker Endpoint: Maximum Recursion Depth Exceeded Error When Calling boto3.client("s3") Here is the relevant code block responsible for the error:
def get_video_bytes_from_s3(bucket_name, key):
s3_client = boto3.client('s3')
try:
video_object = s3_client.get_object(Bucket= bucket_name, Key=key)
video_bytes = video_object['Body'].read()
return video_bytes
except Exception as e:
print(f"Failed to fetch video from S3: {e}")
Error log from CloudWatch:
Traceback (most recent call last):
File "/sagemaker/python_service.py", line 423, in _handle_invocation_post
res.body, res.content_type = handlers(data, context)
File "/opt/ml/model/code/inference.py", line 156, in handler
video_bytes = get_video_bytes_from_s3(key)
File "/opt/ml/model/code/inference.py", line 16, in get_video_bytes_from_s3
s3_client = boto3.client('s3')
File "/usr/local/lib/python3.10/site-packages/boto3/__init__.py", line 92, in client
return _get_default_session().client(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/boto3/session.py", line 299, in client
return self._session.create_client(
File "/usr/local/lib/python3.10/site-packages/botocore/session.py", line 997, in create_client
client = client_creator.create_client(
File "/usr/local/lib/python3.10/site-packages/botocore/client.py", line 159, in create_client
client_args = self._get_client_args(
File "/usr/local/lib/python3.10/site-packages/botocore/client.py", line 490, in _get_client_args
return args_creator.get_client_args(
File "/usr/local/lib/python3.10/site-packages/botocore/args.py", line 137, in get_client_args
endpoint = endpoint_creator.create_endpoint(
File "/usr/local/lib/python3.10/site-packages/botocore/endpoint.py", line 409, in create_endpoint
http_session = http_session_cls(
File "/usr/local/lib/python3.10/site-packages/botocore/httpsession.py", line 323, in __init__
self._manager = PoolManager(**self._get_pool_manager_kwargs())
File "/usr/local/lib/python3.10/site-packages/botocore/httpsession.py", line 341, in _get_pool_manager_kwargs
'ssl_context': self._get_ssl_context(),
File "/usr/local/lib/python3.10/site-packages/botocore/httpsession.py", line 350, in _get_ssl_context
return create_urllib3_context()
File "/usr/local/lib/python3.10/site-packages/botocore/httpsession.py", line 139, in create_urllib3_context
context.options |= options
File "/usr/local/lib/python3.10/ssl.py", line 620, in options
super(SSLContext, SSLContext).options.__set__(self, value)
File "/usr/local/lib/python3.10/ssl.py", line 620, in options
super(SSLContext, SSLContext).options.__set__(self, value)
File "/usr/local/lib/python3.10/ssl.py", line 620, in options
super(SSLContext, SSLContext).options.__set__(self, value)
[Previous line repeated 479 more times]
I have tried monkey patching using gevent i.e. by placing this
monkey.patch_all()
at the top of my imports but it doesn't resolve the issue. Your insights are appreciated.
Thanks.
I was able to circumvent the problem by setting this environment variable in my model container
SAGEMAKER_GUNICORN_WORKER_CLASS = "sync". Inspired by this issue https://github.com/aws/amazon-sagemaker-examples/issues/1561.