Skip to content
This repository was archived by the owner on Oct 17, 2026. It is now read-only.

Cancel database statements that outlive the search poll - #225

Open
skearnes wants to merge 1 commit into
mainfrom
agent/statement-timeout
Open

skearnes wants to merge 1 commit into
mainfrom
agent/statement-timeout

Conversation

@skearnes

@skearnes skearnes commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Search queries had no time limit. In the 30 days to 2026-10-03, 112 of 2,878 searches were still running when the client stopped polling at 120 s, and their queries ran on in Aurora with nobody waiting.
  • Every API database connection now sets statement_timeout to 120 s, the client's polling bound.

Changes

  • get_cursor passes -c statement_timeout=120s alongside the existing search_path, from a new STATEMENT_TIMEOUT_SECONDS constant in search.py.
  • Comments on STATEMENT_TIMEOUT_SECONDS and POLL_TIMEOUT_MS (useSearchTask.ts) point at each other, since nothing else keeps the two in step.

Testing

  • New test_statement_timeout_cancels_slow_query: with the constant set to 1 s, pg_sleep(2) raises QueryCanceled. It fails with the option removed.
  • SHOW statement_timeout on a get_cursor connection reads 2min.
  • pytest: 92 passed. pre-commit hooks pass.

Notes

  • The timeout covers every endpoint, not only searches. Synchronous requests already end at nginx's and the load balancer's 60 s, so it cuts nothing a client could still receive.
  • A cancelled search fails inside the background task, so the client keeps seeing "pending" until its own 120 s timeout, as it does today.

🤖 Generated with Claude Code

RetriggerConfidence Score: 4/5

The PR should not merge until server-side search work is bounded to the client’s total wait.

Findings

  1. P1 Search outlives polling deadline ▶
  2. P2 Canceled tasks remain pending ▶
Summary

The PR sets a 120-second PostgreSQL statement timeout on API connections and documents its relationship to the browser’s search-poll deadline. A new test verifies cancellation of one slow statement.

  • The per-statement limit does not bound a multi-statement search to the browser’s total wait.
  • A canceled background task remains reported as pending.

Reviews (1) · Last reviewed commit: "Cancel database statements that outlive ..."

Every API connection sets statement_timeout to 120 seconds, the same
bound at which useSearchTask stops polling. A search still running past
it has no client waiting for the result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
),
)
options = (
f"-c search_path=public,ord -c statement_timeout={STATEMENT_TIMEOUT_SECONDS}s"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Search outlives polling deadline A similarity search can run a filter query followed by a separate ranking query. This timeout gives each statement its own 120 seconds, while the browser allows 120 seconds for the whole task from submission. If the filter query uses most of that time, ranking can keep the database busy after the browser stops polling, leaving the work this change is meant to prevent. Bound the total task time rather than only each statement.

Knowledge Base Used:

Comment on lines +90 to +94
options = (
f"-c search_path=public,ord -c statement_timeout={STATEMENT_TIMEOUT_SECONDS}s"
)
async with await psycopg.AsyncConnection[dict[str, Any]].connect(
dsn, row_factory=dict_row, options="-c search_path=public,ord"
dsn, row_factory=dict_row, options=options

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 Canceled tasks remain pending When this timeout cancels a background search, the task exits before writing a result or a failure state. The result endpoint then returns “pending” until the query key expires an hour later. This makes a canceled task look like one that is still running and prevents callers from handling the failure promptly. Record a terminal failure state for canceled tasks.

Knowledge Base Used:

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant