Skip to content

Commit dd3c7b5

Browse files
authored
Fix small transaction (#586)
1 parent f83ce71 commit dd3c7b5

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

gcsfs/core.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ def _upload_chunk(self, final=False):
17361736
head = {}
17371737
l = len(data)
17381738

1739-
if (l < GCS_MIN_BLOCK_SIZE) and not final:
1739+
if (l < GCS_MIN_BLOCK_SIZE) and (not final or not self.autocommit):
17401740
# either flush() was called, but we don't have enough to
17411741
# push, or we split a big upload, and have less left than one
17421742
# block. If this is the final part, OK to violate those
@@ -1818,13 +1818,12 @@ def discard(self):
18181818
"""
18191819
if self.location is None:
18201820
return
1821-
uid = re.findall("upload_id=([^&=?]+)", self.location)
18221821
self.gcsfs.call(
18231822
"DELETE",
1824-
f"{self.fs._location}/upload/storage/v1/b/{quote(self.bucket)}/o",
1825-
params={"uploadType": "resumable", "upload_id": uid},
1826-
json_out=True,
1823+
self.location,
18271824
)
1825+
self.location = None
1826+
self.closed = True
18281827

18291828
def _simple_upload(self):
18301829
"""One-shot upload, less than 5MB"""

gcsfs/retry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ def validate_response(status, content, path, args=None):
8282
r: requests response object
8383
path: associated URL path, for error messages
8484
"""
85-
if status >= 400:
85+
if status >= 400 and status != 499:
86+
# 499 is special "upload was cancelled" status
8687
if args:
8788
from .core import quote
8889

gcsfs/tests/test_core.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,29 @@ def test_copy_cache_invalidated(gcs):
14241424
assert gcs.isfile(target_file2)
14251425

14261426

1427+
def test_transaction(gcs):
1428+
# https://github.com/fsspec/gcsfs/issues/389
1429+
if not gcs.on_google:
1430+
pytest.skip()
1431+
try:
1432+
with gcs.transaction:
1433+
with gcs.open(f"{TEST_BUCKET}/foo", "wb") as f:
1434+
f.write(b"This is a test string")
1435+
f.discard()
1436+
assert not gcs.exists(f"{TEST_BUCKET}/foo")
1437+
raise ZeroDivisionError
1438+
except ZeroDivisionError:
1439+
...
1440+
assert not gcs.exists(f"{TEST_BUCKET}/foo")
1441+
1442+
with gcs.transaction:
1443+
with gcs.open(f"{TEST_BUCKET}/foo", "wb") as f:
1444+
f.write(b"This is a test string")
1445+
assert not gcs.exists(f"{TEST_BUCKET}/foo")
1446+
1447+
assert gcs.cat(f"{TEST_BUCKET}/foo") == b"This is a test string"
1448+
1449+
14271450
def test_find_maxdepth(gcs):
14281451
assert gcs.find(f"{TEST_BUCKET}/nested", maxdepth=None) == [
14291452
f"{TEST_BUCKET}/nested/file1",

0 commit comments

Comments
 (0)