Skip to content

Commit

Permalink
S3: Close more filehandles (#5939)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers authored Feb 18, 2023
1 parent c2afe19 commit 0b1333c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 0 additions & 2 deletions moto/s3/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1827,8 +1827,6 @@ def put_object(
if bucket.is_versioned:
keys = existing_keys + [new_key]
else:
for key in existing_keys:
key.dispose()
keys = [new_key]
bucket.keys.setlist(key_name, keys)

Expand Down
6 changes: 6 additions & 0 deletions moto/s3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ def setlist(self, key, list_):
elif not isinstance(list_, list):
list_ = [list_]

for existing_version in self.getlist(key, []):
# Dispose of any FakeKeys that we will not keep
# We should only have FakeKeys here - but we're checking hasattr to be sure
if existing_version not in list_ and hasattr(existing_version, "dispose"):
existing_version.dispose()

super().__setitem__(key, list_)

def _iteritems(self):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_s3/test_s3_file_handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,17 @@ def test_overwrite_file(self):
with mock_s3():
pass

@verify_zero_warnings
def test_delete_object_with_version(self):
with mock_s3():
self.s3.create_bucket(Bucket="foo")
self.s3.put_bucket_versioning(
Bucket="foo",
VersioningConfiguration={"Status": "Enabled", "MFADelete": "Disabled"},
)
version = self.s3.put_object(Bucket="foo", Key="b", Body="s")["VersionId"]
self.s3.delete_object(Bucket="foo", Key="b", VersionId=version)


def test_verify_key_can_be_copied_after_disposing():
# https://github.com/getmoto/moto/issues/5588
Expand Down

0 comments on commit 0b1333c

Please sign in to comment.