Repository navigation
fix(storage): prevent concurrent chunk finalization - #13505
Conversation
Greptile SummaryThis PR serializes preparation, transfer, implicit storage finalization, and document completion under the existing per-file lock.
Confidence Score: 5/5The PR appears safe to merge, with no outstanding actionable findings. The upload lifecycle is serialized under one per-file lock, lease ownership is checked around the transfer, completed-upload replays remain handled, and the expanded regression test validates duplicate concurrency and final content integrity. Important Files Changed
Reviews (4): Last reviewed commit: "Merge branch 'main' into fix/storage-par..." | Re-trigger Greptile |
Full local before/after verificationRan the complete CI-style Storage suite against both baseline and fix in the same isolated PostgreSQL dedicated / Local-storage environment (PHP 8.5.9, locked dependencies). Both runs used the same enhanced tests from this PR; only the HTTP upload action was swapped. SHA-256 checks verified the mounted action matched baseline vendor/bin/paratest --processes 4 --functional tests/e2e/Services/Storage \
--exclude-group abuseEnabled --exclude-group screenshots --exclude-group antivirus
All four baseline failures were This supplements the earlier targeted before/after reproduction, 20 repeated fixed regression runs, and passing full-suite checks. S3, shared-table mode, and antivirus integration remain untested locally. |
|
@greptile review |
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
The per-file lock used a fixed 600 second lease measured from acquisition. A slow transfer could outlive it and let a second request finalize the same file concurrently. The lock resource now hands the Distributed lock to its callback so the upload refreshes the lease before the transfer and verifies it still holds the lock before recording completion, failing with the existing 429 instead of finalizing under a lapsed lease.
Storage learned this in #13505 and the two deployment endpoints were left behind. Both took the per-deployment lock to prepare, dropped it to transfer, and took it again to finalize. upload() finalizes by itself once it sees the last chunk, and Local::joinChunks() renames the assembled file and then unlinks the parts, so a concurrent chunk that passed the file_exists check a moment earlier finds its part gone and 500s out of joinChunks. CI showed it as `"parts":{"2":true,"5":true,"3":true},"chunks":5` — three of five parts left — on Functions and on Sites, across PostgreSQL and MongoDB. The sibling assertion, testUpdateSiteDeploymentRequiresOwnership, saw the same race as an empty deployment id. Prepare, transfer and finalize now run in one lock, shaped like the Storage fix: the lease is refreshed before the transfer so it gets a full window, and checked after it so nothing is recorded under a lapsed lease. This serialises the chunks of a single deployment, which is what Storage already accepted, and it depends on the lock pool having room for the waiters. Covered by testCreateDeploymentParallelChunksLargeFile in both suites, which is what caught it. Co-Authored-By: Claude Opus 5 <[email protected]>
What does this PR do?
Fixes the upload race behind
StorageCustomServerTest::testCreateBucketFileParallelChunksLargeFilein the linked CI job. This is a server bug, not a test timeout.Utopia\Storage\Device::upload()can finalize an upload itself. Previously Appwrite locked preparation and document completion separately but calledupload()outside either lock. Concurrent Local uploads could both enterjoinChunks(), then unlink parts another request was still reading. CI reportedFailed to open chunk ...part.3and HTTP 500; cleanup also failed afterward.Transfers to the storage device for the same file now serialize. Different files retain independent locks. This avoids an upstream storage API change; it can increase same-file contention. The existing 600-second lock lease is unchanged and is not automatically renewed.
Test Plan
Validated in an isolated Local-storage/PostgreSQL dedicated stack with the repository's locked Composer dependencies, PHP 8.5.9, and Swoole:
main'sCreate.phpinto the same stack and ran the enhanced regression: the duplicate-chunk dataset failed with HTTP 500 andFailed to open chunk ...part.2, reproducing the CI failure mechanism.vendor/bin/paratest --processes 4 --functional tests/e2e/Services/Storage --exclude-group abuseEnabled --exclude-group screenshots --exclude-group antivirus: 92 tests, 1,600 assertions, all passed.composer lintfor both changed files,php -lfor both files, andgit diff --check: passed.S3, shared-table mode, and antivirus integration were not exercised locally.
Related PRs and Issues
Checklist