Skip to content

FIX: Bound slow scenario-history reads and stop false compatibility dialog - #4648

Open
Behnam (behnam-o) wants to merge 1 commit into
microsoft:mainfrom
behnam-o:fix/bound-scenario-history-reads
Open

Behnam (behnam-o) wants to merge 1 commit into
microsoft:mainfrom
behnam-o:fix/bound-scenario-history-reads

Conversation

@behnam-o

@behnam-o Behnam (behnam-o) commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Description

One slow scenario-history read can take down the whole GUI. The query keeps running after the client gives up, each auto-refresh starts another one until the DB connection pool is exhausted, and /api/version then times out, so users see a misleading "deploy matching builds" dialog. This PR puts a limit on slow reads and fixes the misleading message. It doesn't change the history query itself.

  • Azure SQL query timeout: set on each pyodbc connection, so the driver cancels the statement on the server and frees the connection. Cancelling the asyncio task alone doesn't stop the ODBC call. The default is 300 s; set it with query_timeout_seconds / AZURE_SQL_DB_QUERY_TIMEOUT_SECONDS, or use 0 to disable it. Schema migrations aren't limited.
  • /api/version: reads build info once at startup, so it no longer waits behind database calls on the default executor.
  • Scanner History and Home: don't start a new read while one is in flight, and back off exponentially (up to 16× the interval) after a failed read.
  • Compatibility gate: a failed or timed-out version check (now 30 s) shows "PyRIT backend unavailable" instead of build-mismatch guidance. Real mismatches are unchanged.

Related: #4649 and #4650 fix the slow query itself. This PR stands on its own and limits the damage from any slow read.

…ialog

Long scenario-history reads kept running after the proxy abandoned the
request, piled up on every frontend refresh, exhausted the connection pool,
and starved the default executor. The /api/version handshake then timed out
and the GUI showed a misleading "deploy matching builds" dialog.

- AzureSQLMemory: set a pyodbc query timeout on async-engine connections so
  the driver cancels long statements server-side and frees the connection.
  Cancelling the asyncio task does not interrupt the ODBC call. Configurable
  via query_timeout_seconds / AZURE_SQL_DB_QUERY_TIMEOUT_SECONDS (default
  300, 0 disables); schema migrations are not limited.
- /api/version: read build info once in the app lifespan instead of on the
  default executor per request.
- useAutoRefresh: optional exponential backoff (up to 16 intervals) while a
  view's last read failed; used by Scanner History and Home, which both call
  GET /api/scenarios/runs.
- Scanner History: queue changes wait for the in-flight read instead of
  starting a parallel one, and do not bypass the backoff after a failure.
- Compatibility gate: a failed or timed-out version check (now 30 s) shows
  "PyRIT backend unavailable" instead of build-mismatch guidance.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Retry backoff can carry over between outages and increase during pending background reads, so counter reset and scheduling fixes with regression coverage are still needed.

3 open issues
What changed in this PR

This PR limits the impact of slow scenario-history reads on PyRIT’s GUI and distinguishes backend unavailability from build incompatibility. It complements the query improvements in #4649 and #4650 without changing the history query itself.

Changes:

  • Adds a configurable Azure SQL statement timeout, excluding schema migrations.
  • Caches build metadata at startup and gives failed version checks separate unavailable messaging with a 30-second request timeout.
  • Adds refresh backoff and defers queue-triggered history reloads while a read is pending.
File Description
tests/​unit/​memory/​test_azure_sql_memory.py Tests timeout configuration, validation, and connection assignment.
tests/​unit/​backend/​test_main.py Verifies build metadata is cached during startup.
tests/​unit/​backend/​test_api_routes.py Tests cached version responses and metadata fallbacks.
pyrit/​memory/​azure_sql_memory.py Configures driver-level timeouts for async statements.
pyrit/​backend/​routes/​version.py Serves cached metadata without per-request disk reads.
pyrit/​backend/​main.py Loads build metadata during application startup.
frontend/​src/​services/​compatibility.ts Separates unavailable checks from compatibility mismatches.
frontend/​src/​services/​compatibility.test.ts Tests unavailable status and request blocking.
frontend/​src/​services/​api.ts Sets the version request timeout to 30 seconds.
frontend/​src/​services/​api.test.ts Verifies the version request timeout configuration.
frontend/​src/​hooks/​useAutoRefresh.ts Adds capped exponential refresh backoff.
frontend/​src/​hooks/​useAutoRefresh.test.tsx Tests backoff, recovery, and visibility timing.
frontend/​src/​components/​Home/​Home.tsx Passes source failures into refresh scheduling.
frontend/​src/​components/​Home/​Home.test.tsx Tests refresh backoff after source failures.
frontend/​src/​components/​History/​ScenarioHistory.tsx Defers queue reloads and enables failure backoff.
frontend/​src/​components/​History/​ScenarioHistory.test.tsx Tests deferred reloads and failure backoff.
frontend/​src/​components/​CompatibilityGate.tsx Displays distinct backend-unavailable guidance.
frontend/​src/​components/​CompatibilityGate.test.tsx Tests timeout messaging and blocked business requests.
doc/​code/​memory/​0_memory.md Documents statement timeout behavior and migration exclusion.
.env_example Shows the optional Azure SQL timeout setting.

Review effort: Balanced

reloadCurrentPage()
}
}, validReference && !displayLoading)
}, validReference && !displayLoading, error !== null)
const onRefresh = useEffectEvent(refresh)
const isFailing = useEffectEvent((): boolean => failing)
const running = useRef(false)
const failedRetries = useRef(0)
Comment on lines +39 to +41
<h1 id="compatibility-notice-title">
{snapshot.status === 'unavailable' ? 'PyRIT backend unavailable' : 'PyRIT compatibility blocked'}
</h1>

This branch has not been deployed

No deployments
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.

2 participants