From af432a32afd1562aef21e6e7b6555c267241d273 Mon Sep 17 00:00:00 2001 From: JeyBee Date: Mon, 28 Sep 2026 08:28:46 +0000 Subject: [PATCH] Return the uploaded GCS generation without reloading _sync_write called blob.reload() after upload_from_string() to read the new generation. That is an extra metadata round trip on every write, and it is racy: if another writer committed between our upload and the reload, we returned their generation as our etag. upload_from_string() already sets the blob's properties from the upload response, so blob.generation is exactly the generation we wrote. Return it directly. --- jqueue/adapters/storage/gcs.py | 5 ++++- tests/test_storage_gcs.py | 30 ++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/jqueue/adapters/storage/gcs.py b/jqueue/adapters/storage/gcs.py index 102b5f4..391a818 100644 --- a/jqueue/adapters/storage/gcs.py +++ b/jqueue/adapters/storage/gcs.py @@ -126,5 +126,8 @@ def _sync_write(self, content: bytes, if_match: str | None) -> str: except gapi_exc.PreconditionFailed as exc: raise CASConflictError("GCS generation mismatch") from exc - blob.reload() + # upload_from_string() already sets blob properties from the upload + # response, so blob.generation is the generation *we* just wrote. + # Re-fetching (blob.reload()) would cost a round trip and could + # return a newer generation written by someone else in between. return str(blob.generation) diff --git a/tests/test_storage_gcs.py b/tests/test_storage_gcs.py index f111728..3f6ad8c 100644 --- a/tests/test_storage_gcs.py +++ b/tests/test_storage_gcs.py @@ -161,12 +161,38 @@ def test_sync_write_returns_generation_as_string(): assert etag == "77" -def test_sync_write_reloads_blob_after_upload(): +def test_sync_write_does_not_reload_blob_after_upload(): pytest.importorskip("google.api_core.exceptions") storage, blob, _ = _make_storage() blob.generation = 55 storage._sync_write(b"content", if_match=None) - blob.reload.assert_called_once() + blob.reload.assert_not_called() + + +def test_sync_write_returns_uploaded_generation_not_a_later_one(): + """A concurrent write after our upload must not leak into our etag.""" + pytest.importorskip("google.api_core.exceptions") + storage, blob, _ = _make_storage() + + def upload(*args: object, **kwargs: object) -> None: + blob.generation = 5 # set from our upload response + + def reload(*args: object, **kwargs: object) -> None: + blob.generation = 6 # someone else wrote after us + + blob.upload_from_string.side_effect = upload + blob.reload.side_effect = reload + + assert storage._sync_write(b"content", if_match="4") == "5" + + +def test_sync_write_precondition_failed_does_not_reload(): + gapi_exc = pytest.importorskip("google.api_core.exceptions") + storage, blob, _ = _make_storage() + blob.upload_from_string.side_effect = gapi_exc.PreconditionFailed("mismatch") + with pytest.raises(CASConflictError): + storage._sync_write(b"content", if_match="42") + blob.reload.assert_not_called() def test_sync_write_precondition_failed_raises_cas_conflict():