Repository navigation
fix(storage): clean up uploads when the antivirus scan cannot run - #13977
Conversation
An unreachable ClamAV daemon made scanInStream() throw, which escaped the scan handling entirely: the joined upload stayed on disk and a chunked upload kept its pending file document. With the chunks already joined, that document could never complete, and deleting it failed because the device had no chunk directory left to abort. A connection failure is now a failed scan, and any scan that is not clean removes both the finalized upload and the pending document before returning the error.
A pending file whose chunks were already joined has no chunk directory left, so Local::abort() returned false from rmdir() and deleting the file failed with "Failed to delete file from device". Uploads left pending by an earlier failed antivirus scan, or any other failure after the join, could never be removed. With no chunk directory there is nothing left to abort, so abort() now returns true after removing the joined file.
🔵 Tier A · Mergeable after minor fixes
This pull request treats ClamAV connection failures as failed scans and cleans up files after scans that are not clean. It also allows deletion of pending local uploads whose chunk directory is already gone. The latest changes preserve the pending document when file removal fails and verify that local abort did not leave the joined file behind. Latest changes: The newest commits make local abort check whether the joined file remains and retain the pending document when scan cleanup cannot remove it.
📂 Walkthrough · 2
⏳ Still open from earlier reviews · 1
✅ Fixed since the last review · 2
Reviewed the commits since |
| try { | ||
| $scan = $antivirus->scanInStream($path); | ||
| } catch (\RuntimeException $e) { | ||
| $scan = ScanResult::failed($e->getMessage()); | ||
| } |
There was a problem hiding this comment.
Cover the unavailable-ClamAV upload path with a regression test
The existing antivirus E2E test only exercises a reachable scanner and successful chunked upload. A test with an unavailable scanner is needed to verify that a final chunk returns an error and leaves neither the pending document nor joined file behind; the current test cannot catch the reported failure recurring.
🟡 Minor · testing · Reply if this doesn't apply.
|
| try { | ||
| $scan = $antivirus->scanInStream($path); | ||
| } catch (\RuntimeException $e) { | ||
| $scan = ScanResult::failed($e->getMessage()); | ||
| } | ||
|
|
||
| if ($scan->isInfected()) { | ||
| if (!$scan->isClean()) { | ||
| // The finalized upload has no completed file record yet, and a pending | ||
| // one would be left pointing at chunks that are already joined. | ||
| $deviceForFiles->delete($path); | ||
|
|
||
| if (!$file->isEmpty()) { | ||
| $authorization->skip(fn () => $dbForProject->deleteDocument('bucket_' . $bucket->getSequence(), $fileId)); |
There was a problem hiding this comment.
Scanner outage lacks regression coverage The existing antivirus test requires ClamAV to be reachable, so it cannot catch a recurrence of this change’s target failure: a scanner error that leaves a joined upload or pending document behind. Please add a test of the observable error response and cleanup, rather than assertions tied to the implementation.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php
Line: 374-386
Comment:
**Scanner outage lacks regression coverage** The existing antivirus test requires ClamAV to be reachable, so it cannot catch a recurrence of this change’s target failure: a scanner error that leaves a joined upload or pending document behind. Please add a test of the observable error response and cleanup, rather than assertions tied to the implementation.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Keep the pending document when the joined file could not be removed after an unclean scan, report a failed document delete, and have Local::abort() succeed only when the joined file no longer exists.
When ClamAV was unreachable, scanInStream() threw a RuntimeException that skipped the scan handling, so the joined upload stayed on disk and a chunked upload kept a pending file document that could never complete. A connection failure now counts as a failed scan, and any scan that is not clean removes both the joined file and the pending document before returning the error.
Pending files that are already stuck could not be deleted either, because the local device's abort() returned false once the chunk directory was gone. It now treats a missing chunk directory as nothing left to abort, so those files, including ones left behind before this change, can be deleted again.
Fixes #8603