Skip to content

fix(storage): clean up uploads when the antivirus scan cannot run - #13977

Merged
HarshMN2345 merged 4 commits into
mainfrom
fix/storage-antivirus-unreachable
Sep 29, 2026
Merged

HarshMN2345 merged 4 commits into
mainfrom
fix/storage-antivirus-unreachable

Conversation

@HarshMN2345

Copy link
Copy Markdown
Member

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

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.
@ghost

ghost commented Sep 29, 2026 •

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

The cleanup fixes address the previously reported data-retention issues, but the unavailable-ClamAV regression test remains outstanding.

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.

Verdict New comments Fixed Still open
✅ Approved 0 2 1
📂 Walkthrough · 2
File Change
packages/storage/src/Device/Local.php Treat a missing chunk directory as an aborted upload only if the joined file is gone.
src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php Convert scan connection errors to failed scans and clean up failed uploads while retaining pending records if file removal fails.
⏳ Still open from earlier reviews · 1
✅ Fixed since the last review · 2
  • Do not report a successful abort if removing the file failed · packages/storage/src/Device/Local.php:226
  • Keep the pending document when deleting the scanned file fails · src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php:383

Reviewed the commits since 97ce8ba · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Tier A · Looks good to merge. Summary

Comment thread packages/storage/src/Device/Local.php Resolved
Comment on lines +374 to +378
try {
$scan = $antivirus->scanInStream($path);
} catch (\RuntimeException $e) {
$scan = ScanResult::failed($e->getMessage());
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@greptile-apps

greptile-apps Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Critical risk] Changes file upload cleanup and antivirus scan error handling.

The PR appears safe to merge, though the scanner-outage cleanup still lacks regression coverage.

Fix All in Claude CodeFindings

  1. P2 Scanner outage lacks regression coverage ▶
Fix with agent prompt
### Issue 1
src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php:374-386
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.

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!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

The PR handles ClamAV connection failures as failed scans, cleans up unsuccessful uploads, and lets local storage abort an upload after its chunk directory is gone.

  • The changes address the three previously resolved cleanup findings.
  • The scanner-outage regression test requested in the remaining thread has not been added.

Reviews (2) · Last reviewed commit: "fix(storage): only drop an upload's reco..."

Comment thread packages/storage/src/Device/Local.php Resolved
Comment on lines +374 to +386
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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 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!

Fix in Claude Code Fix in Codex

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/storage-antivirus-unreachable (after).

Metric Before After Change
🚀 Requests/sec 195.68 200.81 ⚪ +2.6%
⏱️ Latency P50 88.51 ms 87.35 ms ⚪ -1.3%
⏱️ Latency P95 205.57 ms 200.02 ms ⚪ -2.7%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 87.35 200.02 12,540 200.81 -5.56
Account 159.06 298.98 660 11.22 -42.48
TablesDB 84.18 155.31 6,820 112 -14.37
Storage 81.76 180.18 3,300 55.81 +15.96
Functions 120.35 229.7 1,760 30.51 -14.61

Top API waits (after)

API request Max wait (ms)
functions.create 429.4
account.name.update 409.21
storage.files.create 399.87
account.prefs.update 379.89
storage.buckets.create 372.86

HarshMN2345 and others added 2 commits September 29, 2026 16:14
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.
@HarshMN2345
HarshMN2345 merged commit 8f5200c into main Sep 29, 2026
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug Report: Failed to delete file due to failed to upload because of Unable to connect to ClamAv server

1 participant