Skip to content

fix(worker): prevent poisoned SQLAlchemy sessions from reuse - #1876

Open
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/worker-db-session-cleanup-s0e3Pi
Open

fix(worker): prevent poisoned SQLAlchemy sessions from reuse#1876
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/worker-db-session-cleanup-s0e3Pi

Conversation

@sentry

@sentry sentry Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

This PR addresses an issue where SQLAlchemy sessions could be left in a 'prepared' state and subsequently reused by other tasks, leading to InvalidRequestError: This session is in 'prepared' state; no further SQL can be emitted within this transaction.

The root cause was identified in apps/worker/tasks/base.py within the wrap_up_dbsession method. When a SoftTimeLimitExceeded exception occurred during db_session.commit(), the session would enter a 'prepared' state. If the subsequent retry commit also failed with an exception other than InvalidRequestError, the get_db_session.remove() call was bypassed, leaving the poisoned session in the scoped registry.

To fix this, two changes were made:

  1. get_db_session.remove() is now called immediately after catching the initial SoftTimeLimitExceeded. This ensures the poisoned session is evicted from the registry regardless of what happens during the retry attempt.
  2. The inner except InvalidRequestError block, which handles failures during the retry commit, has been widened to except Exception. This guarantees that get_db_session.remove() is called for any exception that might occur during the retry commit, preventing any further session leakage.

Legal Boilerplate

Look, I get it. The entity doing business as "Codecov" is owned by Harness, Inc. In 2026 Harness acquired Codecov and as a result Harness is going to need some rights from me in order to utilize my contributions in this PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Harness can use, modify, copy, and redistribute my contributions, under Harness's choice of terms.

Fixes WORKER-YND

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants