Skip to content
This repository was archived by the owner on Oct 17, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/src/hooks/useSearchTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { fetchJson } from '../utils/api';
import type { SearchResult } from '../types/search';

const POLL_INTERVAL_MS = 1000;
// The server cancels the search query at the same bound (STATEMENT_TIMEOUT_SECONDS
// in ord_interface/api/search.py).
const POLL_TIMEOUT_MS = 120_000;
// Bounds each request, so a stalled one fails the search instead of hanging it.
// Longer than the proxy's 60-second proxy_read_timeout (ord_interface/nginx.conf),
Expand Down
8 changes: 7 additions & 1 deletion ord_interface/api/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@

BOND_LENGTH = 20
MAX_RESULTS = 1000
# Postgres cancels any statement that runs longer. Matches POLL_TIMEOUT_MS in
# app/src/hooks/useSearchTask.ts: past it, the client has stopped waiting.
STATEMENT_TIMEOUT_SECONDS = 120


@asynccontextmanager
Expand All @@ -84,8 +87,11 @@ async def get_cursor() -> AsyncIterator[AsyncCursor[dict[str, Any]]]:
host=os.environ["POSTGRES_HOST"],
),
)
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:

)
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
Comment on lines +90 to +94

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:

) as connection:
await connection.set_read_only(True)
async with connection.cursor() as cursor:
Expand Down
11 changes: 11 additions & 0 deletions ord_interface/api/search_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
import gzip
import json

import psycopg
import pytest
from ord_schema.proto import dataset_pb2
from rdkit import Chem
from tenacity import retry, stop_after_attempt, wait_fixed

from ord_interface.api import search
from ord_interface.api.queries import QueryResult


Expand Down Expand Up @@ -194,6 +196,15 @@ def test_query_async(test_client, params, num_expected):
assert len(wait_for_task(test_client, response.json())) == num_expected


@pytest.mark.asyncio
async def test_statement_timeout_cancels_slow_query(test_postgres, monkeypatch):
monkeypatch.setenv("ORD_INTERFACE_POSTGRES", test_postgres.url())
monkeypatch.setattr(search, "STATEMENT_TIMEOUT_SECONDS", 1)
async with search.get_cursor() as cursor:
with pytest.raises(psycopg.errors.QueryCanceled):
await cursor.execute("SELECT pg_sleep(2)")


def test_get_input_stats(test_client):
response = test_client.get(
"/api/input_stats",
Expand Down
Loading