S3: Ensure we can copy deleted keys (#5589)
This commit is contained in:
parent
8c88a93d7c
commit
bd465b032b
@ -305,7 +305,12 @@ class FakeKey(BaseModel, ManagedState):
|
|||||||
# https://docs.python.org/3/library/pickle.html#handling-stateful-objects
|
# https://docs.python.org/3/library/pickle.html#handling-stateful-objects
|
||||||
def __getstate__(self):
|
def __getstate__(self):
|
||||||
state = self.__dict__.copy()
|
state = self.__dict__.copy()
|
||||||
|
try:
|
||||||
state["value"] = self.value
|
state["value"] = self.value
|
||||||
|
except ValueError:
|
||||||
|
# Buffer is already closed, so we can't reach the data
|
||||||
|
# Only happens if the key was deleted
|
||||||
|
state["value"] = ""
|
||||||
del state["_value_buffer"]
|
del state["_value_buffer"]
|
||||||
del state["lock"]
|
del state["lock"]
|
||||||
return state
|
return state
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import copy
|
||||||
import gc
|
import gc
|
||||||
import warnings
|
import warnings
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
@ -186,3 +187,21 @@ class TestS3FileHandleClosures(TestCase):
|
|||||||
self.s3.delete_object(
|
self.s3.delete_object(
|
||||||
bucket_name="my-bucket", key_name="my-key", version_id=key._version_id
|
bucket_name="my-bucket", key_name="my-key", version_id=key._version_id
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_verify_key_can_be_copied_after_disposing():
|
||||||
|
# https://github.com/spulec/moto/issues/5588
|
||||||
|
# Multithreaded bug where:
|
||||||
|
# - User: calls list_object_versions
|
||||||
|
# - Moto creates a list of all keys
|
||||||
|
# - User: deletes a key
|
||||||
|
# - Moto iterates over the previously created list, that contains a now-deleted key, and creates a copy of it
|
||||||
|
#
|
||||||
|
# This test verifies the copy-operation succeeds, it will just not have any data
|
||||||
|
key = s3model.FakeKey(name="test", bucket_name="bucket", value="sth")
|
||||||
|
assert not key._value_buffer.closed
|
||||||
|
key.dispose()
|
||||||
|
assert key._value_buffer.closed
|
||||||
|
|
||||||
|
copied_key = copy.copy(key)
|
||||||
|
copied_key.value.should.equal(b"")
|
||||||
|
Loading…
Reference in New Issue
Block a user