Repository navigation
fix(backends): keep forget() idempotent on elasticsearch and couchdb - #10697
Conversation
Result.forget() on ElasticsearchBackend raises elasticsearch.NotFoundError and on CouchBackend raises pycouchdb.exceptions.NotFound when the result is absent (expired, cleaned up, already forgotten, or never stored), while the other KV result backends treat deleting an absent key as a no-op. Catch the backend-specific NotFound inside each delete(), mirroring the filesystem (celery#10678) and couchbase (celery#10680) fixes, so forget() stays idempotent. get()/mget() paths are unchanged. Regression tests per backend: forgetting a missing task and forgetting twice. Red before the fix (4 failed, exception propagated), green after (58 passed). pre-commit hooks pass on pinned versions. Signed-off-by: simpleqt <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10697 +/- ##
=======================================
Coverage 89.51% 89.52%
=======================================
Files 153 153
Lines 20414 20420 +6
Branches 2434 2434
=======================================
+ Hits 18274 18280 +6
Misses 1838 1838
Partials 302 302
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
MehrazRumman
left a comment
There was a problem hiding this comment.
Thanks @simpleqt, this is a clean fix. The approach looks right to me.
The fix
- Elasticsearch: catching
elasticsearch.exceptions.NotFoundErroraround bothdelete()branches (with and withoutdoc_type) is correct. Note that ES also raisesNotFoundErrorwhen the index itself doesn't exist (index_not_found_exception), so this change also hides that case. Forforget()that's reasonable (nothing to forget either way), but it would be worth mentioning in the comment so nobody later thinks it only covers a missing document. - CouchDB:
pycouchdb'sDatabase.delete(id)first fetches the document to get its_rev, and that fetch is what raisesNotFound, so catching it there is the right place. A concurrent update between the fetch and the delete can still raiseConflict. That's a different situation, and I think it's correct to leave it unhandled. - This matches the existing
DocumentNotFoundExceptionhandling incouchbase.pyand the no-op behaviour of Redis, cache, DynamoDB and ArangoDB, soforget()now behaves consistently on these backends.
Backends that still raise
For completeness, I checked the other KeyValueStoreBackend.delete() implementations. Two still let a missing-key error through:
azureblockblob.py:blob_client.delete_blob()raisesazure.core.exceptions.ResourceNotFoundErrorfor a missing blob.cosmosdbsql.py:DeleteDocument()raises anHTTPFailurewith status 404 for a missing document.
(gcs.py checks blob.exists() first, and S3 delete_object doesn't raise for a missing key.)
These don't need to be in this PR, but since the description says "like the other KV backends", it would be good to either fix them here or open a follow-up issue so forget() behaves the same everywhere.
Tests
test_forget_twice_is_noopcallsforget(uuid())twice, which generates two different task ids. Becauseside_effectis a list, the second call raises regardless, so this is effectively the same test astest_forget_missing_task_is_noop. To test what the name says, please use the same id for both calls:
tid = uuid()
x.forget(tid)
x.forget(tid)
assert x._connection.delete.call_count == 2- None of the new tests check which key
delete()was called with. Addingdelete.assert_called_with(x.get_key_for_task(tid))(converted withbytes_to_strfor CouchDB) would make sure the exception is being suppressed for the right call. - In
test_elasticsearch.py, theNotFoundError(..., ApiResponseMeta(404, ...), None)construction is now repeated three times, including the existing test just above. A small helper such as_not_found_error()would keep this readable and give one place to update if the elasticsearch client's constructor changes again.
Summary
The code change is correct and small, and I'd approve it once the "twice" tests use the same task id. Handling Azure Block Blob and CosmosDB can be a follow-up.
- note that Elasticsearch NotFoundError also covers a missing index (index_not_found_exception); the forget() outcome is the same either way - test_forget_twice_is_noop generated two different task ids, making it identical to the missing-task test; use the same id so it really exercises the second forget on an already-forgotten result Signed-off-by: simpleqt <[email protected]>
Problem
AsyncResult(task_id).forget()raises on two KV result backends when the result is absent — expired, cleaned up, already forgotten, or never stored:forget()is documented as removing the result and is expected to be idempotent ("forget about it"), and every other KV backend already tolerates an absent key: filesystem was fixed in #10678, couchbase in #10680, and redis'sdeleteis a no-op for missing keys.Root cause
KeyValueStoreBackend._forget(celery/backends/base.py:1257) callsself.delete(self.get_key_for_task(task_id)), and both backends let their client's missing-document exception propagate out ofdelete():celery/backends/elasticsearch.py:260—self.server.delete(...)raiseselasticsearch.exceptions.NotFoundError(404) for an absent doc (theget()path at lines 112/184 already handles this exception, onlydelete()leaks it)celery/backends/couchdb.py:98—self.connection.delete(key)raisespycouchdb.exceptions.NotFoundFix
Catch the backend-specific NotFound inside each
delete()and return, soforget()is idempotent — same shape as the filesystem and couchbase fixes.get()/mget()and every other path are unchanged.(analogous for
couchdb.pywithpycouchdb.exceptions.NotFound)Tests
Two regression tests per backend in
t/unit/backends/test_elasticsearch.py/test_couchdb.py:test_forget_missing_task_is_noop— forget a task whose document does not existtest_forget_twice_is_noop— forget the same stored task twiceRed/green verified: with the fix reverted all 4 new tests fail with the exception propagating out of
forget(); with the fix all 58 tests in both files pass.pre-commit run --files(pinned hooks: flake8, isort, mypy, pyupgrade, codespell) passes on all changed files.Note on CI: the elasticsearch/couchdb extras are not part of the default CI requirements, so these tests are
importorskip'd there (same situation as couchbase before #10680) — they were run locally against elasticsearch 8.19.3 / pycouchdb 1.17.1 (extras' pinned range).