Skip to content

fix: Preserve original exception cause in timeout context managers (closes #16299)#22262

Open
botbikamordehai2-sketch wants to merge 1 commit into
PrefectHQ:mainfrom
botbikamordehai2-sketch:fix/issue-16299-1781182461
Open

fix: Preserve original exception cause in timeout context managers (closes #16299)#22262
botbikamordehai2-sketch wants to merge 1 commit into
PrefectHQ:mainfrom
botbikamordehai2-sketch:fix/issue-16299-1781182461

Conversation

@botbikamordehai2-sketch

Copy link
Copy Markdown

What

When a timeout occurs in the timeout_async or timeout context managers, the original CancelledError is caught and re-raised as a generic TimeoutError, losing the underlying cause (e.g., an asyncpg database timeout). This makes debugging difficult, as seen in issue #16299 where database communication timeouts are masked.

Fix

Use raise ... from e to chain the original CancelledError as the cause of the new timeout_exc_type exception. This preserves the full traceback and allows users to see the original error that triggered the timeout.

Closes #16299

@github-actions github-actions Bot added bug Something isn't working Database performance Related to an optimization or performance improvement labels Jun 11, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 46b5e4027d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

except CancelledError:
raise timeout_exc_type(f"Scope timed out after {seconds} second(s).")
except CancelledError as e:
raise timeout_exc_type(f"Scope timed out after {seconds} second(s).") from e

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the cause for sync timeouts too

This change only chains the CancelledError in timeout_async, but the sync timeout context manager below still raises an unchained TimeoutError. Sync tasks use timeout directly (src/prefect/task_engine.py:1008) and sync flows select it when self.flow.isasync is false (src/prefect/flow_engine.py:1274), so those timeout failures continue to lose the underlying cancellation cause this fix is meant to preserve. Please mirror the except CancelledError as e / raise ... from e change in the sync path as well.

Useful? React with 👍 / 👎.

@codspeed-hq

codspeed-hq Bot commented Jun 11, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 8 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing botbikamordehai2-sketch:fix/issue-16299-1781182461 (46b5e40) with main (4c3d694)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports.

@zzstoatzz

zzstoatzz commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

hi @botbikamordehai2-sketch thanks for opening a PR

this seems incomplete and afaict over-claims the fix. it only chains the async timeout_async path; sync timeout is still the same, so sync flow/task timeouts would still lose their cancellation cause.

also, #16299 appears to be about DB latency/locking/server starvation more than timeout traceback formatting. this may improve traceback readability, but I don’t think it resolves the linked issue. I’d suggest adding sync coverage + tests asserting __cause__

@github-actions

Copy link
Copy Markdown
Contributor

This pull request is stale because it has been open 14 days with no activity. To keep this pull request open remove stale label or comment.

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

Labels

bug Something isn't working Database performance Related to an optimization or performance improvement status:stale

2 participants