Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle exceptions during writes to Azure #783

Merged
merged 7 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion smart_open/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,18 @@ def __init__(
def flush(self):
pass

def terminate(self):
"""Do not commit block list on abort.

Uploaded (uncommitted) blocks will be garbage collected after 7 days.

See also https://stackoverflow.com/a/69673084/5511061."""
logger.debug("terminating")
if not self.closed:
self._block_list = []
self._is_closed = True
logger.debug("successfully terminated")

#
# Override some methods from io.IOBase.
#
Expand Down Expand Up @@ -509,7 +521,10 @@ def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
if exc_type is not None:
self.terminate()
else:
self.close()

def __str__(self):
return "(%s, %r, %r)" % (
Expand Down
21 changes: 21 additions & 0 deletions smart_open/tests/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def commit_block_list(self, block_list, metadata=None):
metadata.update({"size": len(data)})
self.set_blob_metadata(metadata)
self._container_client.register_blob_client(self)
self._staged_contents = {}

def delete_blob(self):
self._container_client.delete_blob(self)
Expand Down Expand Up @@ -618,6 +619,26 @@ def test_write_blob_client(self):
))
self.assertEqual(output, [test_string])

def test_abort_upload(self):
"""Does aborted upload skip commit_block_list?"""
test_string = b"42" * 42
blob_name = "test_abort_upload_%s" % BLOB_NAME

container_client = CLIENT.get_container_client(CONTAINER_NAME)
blob_client = container_client.get_blob_client(blob_name)

try:
with smart_open.open(
"azure://%s/%s" % (CONTAINER_NAME, blob_name),
"wb",
transport_params={"client": blob_client, "min_part_size": 42},
) as fout:
fout.write(test_string)
raise ValueError
except ValueError:
# FakeBlobClient.commit_block_list was not called
self.assertGreater(len(blob_client._staged_contents), 0)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should add a similar test including compression, after #786 merges

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


def test_incorrect_input(self):
"""Does azure write fail on incorrect input?"""
blob_name = "test_incorrect_input_%s" % BLOB_NAME
Expand Down
Loading