Skip to content

Commit abb72ab

Browse files
authored
Fix memory leak
Creating a new session each time S3Storage is instantiated creates a memory leak. It seems that S3Storage can get created a bunch of times (I'm seeing it get created again and again as my app runs) and a boto3's Session takes loads of memory (see boto/boto3#1670) so my app eventually runs out of memory. This should fix the issue while still avoiding using the same session across different threads.
1 parent 2fd1b66 commit abb72ab

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

django_s3_storage/storage.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,17 @@ def open(self, mode="rb"):
9393
if self.closed:
9494
self.file = self._storage.open(self.name, mode).file
9595
return super(S3File, self).open(mode)
96+
97+
98+
class _LocalSession(local):
99+
def __init__(self):
100+
self.session = boto3.session.Session()
101+
102+
103+
local_session = _LocalSession()
96104

97105

98106
class _Local(local):
99-
100107
"""
101108
Thread-local connection manager.
102109
@@ -116,8 +123,7 @@ def __init__(self, storage):
116123
connection_kwargs["aws_session_token"] = storage.settings.AWS_SESSION_TOKEN
117124
if storage.settings.AWS_S3_ENDPOINT_URL:
118125
connection_kwargs["endpoint_url"] = storage.settings.AWS_S3_ENDPOINT_URL
119-
self.session = boto3.session.Session()
120-
self.s3_connection = self.session.client("s3", config=Config(
126+
self.s3_connection = local_session.session.client("s3", config=Config(
121127
s3={"addressing_style": storage.settings.AWS_S3_ADDRESSING_STYLE},
122128
signature_version=storage.settings.AWS_S3_SIGNATURE_VERSION,
123129
), **connection_kwargs)

0 commit comments

Comments
 (0)