Skip to content

fix(filesystem backend): make delete idempotent for missing keys - #10702

Open
simpleqt wants to merge 3 commits into
celery:mainfrom
simpleqt:fix/filesystem-backend-idempotent-delete
Open

simpleqt wants to merge 3 commits into
celery:mainfrom
simpleqt:fix/filesystem-backend-idempotent-delete

Conversation

@simpleqt

Copy link
Copy Markdown

Problem

FilesystemBackend.delete is not idempotent, unlike every other KeyValueStoreBackend implementation:

def delete(self, key):
    self.unlink(self._filename(key))

redis (client.delete) and cache (cache.pop(key, None)) both tolerate missing keys, but the filesystem backend raises FileNotFoundError. AsyncResult.forget() and GroupResult.delete() are public cleanup APIs that are most needed when a result was never stored or has already been removed (e.g. by cleanup()), and they crash on this backend:

app = Celery(backend=f'file://{tmpdir}', broker='memory://')
app.AsyncResult('nonexistent-id').forget()
# FileNotFoundError: .../celery-task-meta-nonexistent-id
GroupResult(id='missing-group', results=[]).delete(backend=app.backend)
# FileNotFoundError: .../celery-taskset-meta-missing-group

Fix

Swallow FileNotFoundError in delete, matching the idempotent contract of the other key-value backends (get in the same class already handles FileNotFoundError the same way).

Test plan

Two regression tests in t/unit/backends/test_filesystem.py:

  • forget() on a task id that was never stored
  • delete() on a missing result key

Both fail on main (verified by stashing the fix) and pass with it. flake8 (repo config) clean.

Copilot AI lite review requested due to automatic review settings September 24, 2026 15:34

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.74%. Comparing base (f0b1320) to head (7765fad).
⚠️ Report is 6 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10702   +/-   ##
=======================================
  Coverage   89.73%   89.74%           
=======================================
  Files         153      153           
  Lines       20515    20518    +3     
  Branches     2448     2448           
=======================================
+ Hits        18410    18413    +3     
  Misses       1803     1803           
  Partials      302      302           
Flag Coverage Δ
unittests 89.72% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@auvipy

auvipy commented Sep 27, 2026

Copy link
Copy Markdown
Member

@MehrazRumman check this too

@MehrazRumman MehrazRumman left a comment

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.

Thanks @simpleqt. The change is correct: catching only FileNotFoundError around self.unlink() makes forget() / GroupResult.delete() idempotent, and still surfaces PermissionError and other OSErrors.

However, this is the same fix as #10678 (also yours), which makes the identical change to FilesystemBackend.delete(). Neither is merged yet, so we should land only one. The two PRs have complementary tests:

  • #10678: test_forget_missing_task_is_noop and test_forget_twice_is_noop (same task id, stored then forgotten twice).
  • #10702: test_forget_missing_task_is_idempotent and test_delete_missing_key_is_idempotent (calls delete() directly).

Suggestion: keep one PR (either is fine; #10678 is older and has the "forget twice" test), bring the delete()-level test over from this one, and close the other as a duplicate. @auvipy, your call on which to keep.

While you're touching delete(), the point from my review on #10678 applies here too: cleanup() has the same missing-file race (os.stat() / self.unlink() after os.listdir() can raise FileNotFoundError if a concurrent forget() or another cleanup() removed the file first, which aborts the rest of the pass). Wrapping that with the same except FileNotFoundError would complete the idempotency fix for this backend.

One more heads-up: #10703 (atomic writes) changes the same file, so whichever lands second will need a trivial rebase.

@auvipy

auvipy commented Sep 29, 2026

Copy link
Copy Markdown
Member

@MehrazRumman can you combine all 3 pr related to filesytem fix and create a new single pr? this will reduce distraction for maintainers

@MehrazRumman

Copy link
Copy Markdown
Contributor

Okay ! Thanks @auvipy !

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants