Check and drop if stale connection when saving task result in db#498
Check and drop if stale connection when saving task result in db#498lode-braced wants to merge 5 commits intocelery:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds protection against stale database connections when storing Celery task results. It calls Django's close_if_unusable_or_obsolete() method before saving results to ensure the connection is still valid, particularly for long-running tasks that may exceed the configured connection max age.
Key Changes:
- Added connection validation logic in
_store_result()to close stale connections before saving - Added test coverage to verify stale connections are properly detected and replaced
- Configured
CONN_MAX_AGE: Nonefor test databases to enable persistent connection testing
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
django_celery_results/backends/database.py |
Added close_if_unusable_or_obsolete() call and imported connections from django.db |
t/unit/backends/test_database.py |
Added test to validate stale connection handling, imported time and connections |
t/proj/settings.py |
Configured CONN_MAX_AGE: None for all test database connections |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@auvipy the copilot description beat me to the punch: I noticed some of our very long running celery tasks getting this exception - the work they do is not on the db connection of the task thread, and so that connection remains idle until the end of the task, at which point the server already kicked it off for being idle for too long. Main consequence is that I had to modify the tests to run outside of the usual pytest-django transaction, since the connection dropping does not play nice with being inside a transaction. |
No description provided.