Skip to content

Commit 9cae97f

Browse files
committed
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 53af304 commit 9cae97f

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,9 +93,16 @@ def open(self, mode="rb"):
9393
self.file = self._storage.open(self.name, mode).file
9494
return super().open(mode)
9595

96+
97+
class _LocalSession(local):
98+
def __init__(self):
99+
self.session = boto3.session.Session()
100+
101+
102+
local_session = _LocalSession()
96103

97-
class _Local(local):
98104

105+
class _Local(local):
99106
"""
100107
Thread-local connection manager.
101108
@@ -117,8 +124,7 @@ def __init__(self, storage):
117124
connection_kwargs["aws_session_token"] = storage.settings.AWS_SESSION_TOKEN
118125
if storage.settings.AWS_S3_ENDPOINT_URL:
119126
connection_kwargs["endpoint_url"] = storage.settings.AWS_S3_ENDPOINT_URL
120-
self.session = boto3.session.Session()
121-
self.s3_connection = self.session.client(
127+
self.s3_connection = local_session.client(
122128
"s3",
123129
config=Config(
124130
s3={"addressing_style": storage.settings.AWS_S3_ADDRESSING_STYLE},

0 commit comments

Comments
 (0)