fix: prevent imports stuck in Pending on stale DB connections#327
Open
level09 wants to merge 1 commit into
Open
fix: prevent imports stuck in Pending on stale DB connections#327level09 wants to merge 1 commit into
level09 wants to merge 1 commit into
Conversation
etl_process_file's except handler called db.session.get(DataImport, ...) without rolling back first. When an upstream error poisoned the session (e.g. Postgres dropping an idle connection), the fail-marker query raised PendingRollbackError, masked the original exception, and left the import row stuck in 'Pending' forever. - Roll back the session before recovering, and guard the fail-marker so a follow-up failure doesn't replace the original exception. - Enable pool_pre_ping and pool_recycle on the SQLAlchemy engine so workers don't get handed dropped connections in the first place.
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Web/upload imports could silently get stuck in Pending with no error log on the import row, even though the celery worker logged a traceback. We hit this in production when Postgres dropped an idle connection:
etl_process_fileraisedPendingRollbackError, the original exception was masked, and the import row was never marked Failed.Two fixes:
Defensive error handling in
etl_process_file(enferno/tasks/data_import.py)db.session.rollback()before reusing the session in the except branch. Otherwise a poisoned session causes the fail-marker query to raise, masking the real exception and leaving the row stuck in Pending.Connection health checks on the SQLAlchemy engine (
enferno/settings.py)pool_pre_ping=True: ping each pooled connection before use, so workers don't get handed a dropped connection.pool_recycle=300(overridable viaSQLALCHEMY_POOL_RECYCLE): cycle connections every 5 min, well under typical NAT/firewall idle timeouts.Together: the first fix makes the symptom impossible regardless of root cause; the second stops the underlying class of error from happening in the first place.
Observed in production
The line number points inside the except clause; the original exception inside
MediaImport(...)/di.process(file)was lost. We also sawOperationalError: server closed the connection unexpectedlyon the same worker pool earlier the same day, confirming the connection-drop pattern.Test plan
uv run pytest) on a Postgres with a low idle timeout and confirm no regressions.Readystatus.etl_process_file(e.g. kill the worker's connection) and confirm the import row ends inFailedwith the original exception in the log instead ofPending.