Skip to content

FIX database connection exhaustion in worker and API endpoint#1067

Merged
ionparticle merged 1 commit intomasterfrom
fix/db-connection-exhaustion
Mar 13, 2026
Merged

FIX database connection exhaustion in worker and API endpoint#1067
ionparticle merged 1 commit intomasterfrom
fix/db-connection-exhaustion

Conversation

@xcompass
Copy link
Copy Markdown
Member

  • celery_worker.py: call db.session.remove() in ContextTask finally block so connections are returned to the pool after every task regardless of outcome; previously connections leaked until CELERY_WORKER_MAX_TASKS_PER_CHILD recycled the process
  • assignment_search_enddate.py: replace per-request create_engine() (which spawned a new 5-connection pool on every HTTP request) with db.session.execute() using the shared app pool; also switch to sqlalchemy.text() with bound parameters to fix SQL injection via timezone arguments
  • settings.py: add pool_pre_ping=True so stale connections are validated before use, preventing "MySQL server has gone away" errors after idle periods

- celery_worker.py: call db.session.remove() in ContextTask finally block so
  connections are returned to the pool after every task regardless of outcome;
  previously connections leaked until CELERY_WORKER_MAX_TASKS_PER_CHILD recycled
  the process
- assignment_search_enddate.py: replace per-request create_engine() (which
  spawned a new 5-connection pool on every HTTP request) with db.session.execute()
  using the shared app pool; also switch to sqlalchemy.text() with bound
  parameters to fix SQL injection via timezone arguments
- settings.py: add pool_pre_ping=True so stale connections are validated before
  use, preventing "MySQL server has gone away" errors after idle periods
@xcompass xcompass requested a review from ionparticle March 13, 2026 07:47
@xcompass xcompass added the bug label Mar 13, 2026
Copy link
Copy Markdown
Member

@ionparticle ionparticle left a comment

Choose a reason for hiding this comment

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

I would prefer the raw SQL queries to be refactored to ORM queries, but that can wait for the sqlalchemy upgrade.

@ionparticle ionparticle merged commit 2ff7b03 into master Mar 13, 2026
8 checks passed
@xcompass xcompass deleted the fix/db-connection-exhaustion branch March 13, 2026 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants