mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Bound SQLite integrity checks for diagnostic attachments (#40688)
## Why Diagnostic uploads skip the doctor attachment if report generation exceeds its overall timeout. SQLite integrity scans can be delayed by large databases or lock contention. ## What changed - Give each database integrity scan a one-second deadline when collecting a diagnostic attachment. - Interrupt scans and lock waits at the deadline, preserve any corruption rows already returned, and report an incomplete clean scan as a warning. - Keep direct `codex doctor` integrity checks unbounded. ## Testing - Cover interrupted scans, lock waits, retries without a deadline, and corruption results returned before interruption. GitOrigin-RevId: 4817fb459ac53953669a9ba5f03397a6fc82c72e
This commit is contained in:
1 parent
94967e03e5
commit
32ce703a80
7 files changed
+194
-53
No files matched your search
@@ -353,21 +353,74 @@ ON CONFLICT(id) DO NOTHING
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Integrity-check rows, including those emitted before interruption.
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum SqliteIntegrityCheck {
|
||||
Complete(Vec<String>),
|
||||
TimedOut(Vec<String>),
|
||||
}
|
||||
|
||||
/// Run SQLite's built-in integrity check against an existing database file.
|
||||
pub async fn sqlite_integrity_check(
|
||||
sqlite: &SqliteConfig,
|
||||
path: &Path,
|
||||
) -> anyhow::Result<Vec<String>> {
|
||||
let pool = sqlite.open_read_only_pool(path).await?;
|
||||
let rows = sqlx::query_scalar::<_, String>("PRAGMA integrity_check")
|
||||
.fetch_all(&pool)
|
||||
deadline: Option<Instant>,
|
||||
) -> anyhow::Result<SqliteIntegrityCheck> {
|
||||
let pool = sqlite
|
||||
.open_read_only_pool(
|
||||
path,
|
||||
deadline.map(|deadline| deadline.saturating_duration_since(Instant::now())),
|
||||
)
|
||||
.await?;
|
||||
let mut connection = pool.acquire().await?;
|
||||
if let Some(deadline) = deadline {
|
||||
// Lock waits do not invoke the progress handler; share the remaining budget.
|
||||
QueryBuilder::<Sqlite>::new("PRAGMA busy_timeout = ")
|
||||
.push(
|
||||
deadline
|
||||
.saturating_duration_since(Instant::now())
|
||||
.as_millis(),
|
||||
)
|
||||
.build()
|
||||
.execute(&mut *connection)
|
||||
.await?;
|
||||
// Interrupt SQLite itself: dropping a timed-out future leaves its worker scanning.
|
||||
connection
|
||||
.lock_handle()
|
||||
.await?
|
||||
.set_progress_handler(/*num_ops*/ 1_000, move || Instant::now() < deadline);
|
||||
}
|
||||
let mut rows = Vec::<String>::new();
|
||||
// Keep corruption rows even when a later step interrupts the scan.
|
||||
let result = sqlx::query::<Sqlite>("PRAGMA integrity_check")
|
||||
.try_map(|row| {
|
||||
rows.push(row.try_get(/*index*/ 0)?);
|
||||
Ok(())
|
||||
})
|
||||
.fetch_all(&mut *connection)
|
||||
.await;
|
||||
drop(connection);
|
||||
pool.close().await;
|
||||
Ok(rows)
|
||||
match result {
|
||||
Ok(_) => Ok(SqliteIntegrityCheck::Complete(rows)),
|
||||
Err(sqlx::Error::Database(error))
|
||||
if deadline.is_some()
|
||||
&& error.code().is_some_and(|code| {
|
||||
matches!(
|
||||
code.parse::<i32>().ok().map(|code| code & 0xff),
|
||||
Some(libsqlite3_sys::SQLITE_INTERRUPT | libsqlite3_sys::SQLITE_BUSY)
|
||||
)
|
||||
}) =>
|
||||
{
|
||||
Ok(SqliteIntegrityCheck::TimedOut(rows))
|
||||
}
|
||||
Err(error) => Err(error.into()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::SqliteIntegrityCheck;
|
||||
use super::StateRuntime;
|
||||
use super::runtime_state_migrator;
|
||||
use super::sqlite_integrity_check;
|
||||
@@ -386,6 +439,8 @@ mod tests {
|
||||
use std::path::Path;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
|
||||
#[derive(Default)]
|
||||
struct TestTelemetry {
|
||||
@@ -446,7 +501,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn sqlite_integrity_check_reports_ok_for_valid_db() {
|
||||
async fn sqlite_integrity_check_can_be_interrupted_and_retried() {
|
||||
let codex_home = unique_temp_dir();
|
||||
tokio::fs::create_dir_all(&codex_home)
|
||||
.await
|
||||
@@ -457,17 +512,78 @@ mod tests {
|
||||
.open_read_write_pool(&path)
|
||||
.await
|
||||
.expect("open sqlite db");
|
||||
sqlx::query("CREATE TABLE sample (id INTEGER PRIMARY KEY)")
|
||||
sqlx::query("CREATE TABLE sample (id INTEGER PRIMARY KEY, value INTEGER, other INTEGER)")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.expect("create sample table");
|
||||
sqlx::query("CREATE UNIQUE INDEX sample_value ON sample(value)")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.expect("create sample index");
|
||||
sqlx::query(
|
||||
"WITH RECURSIVE rows(id) AS (SELECT 1 UNION ALL SELECT id + 1 FROM rows WHERE id < 2048) INSERT INTO sample SELECT id, id, CASE WHEN id = 1 THEN 0 ELSE id END FROM rows",
|
||||
)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.expect("populate enough rows to invoke the progress handler");
|
||||
pool.close().await;
|
||||
|
||||
let result = sqlite_integrity_check(&sqlite, &path)
|
||||
assert_eq!(
|
||||
sqlite_integrity_check(&sqlite, &path, Some(Instant::now()))
|
||||
.await
|
||||
.expect("interrupt integrity check"),
|
||||
SqliteIntegrityCheck::TimedOut(Vec::new()),
|
||||
);
|
||||
|
||||
let result = sqlite_integrity_check(&sqlite, &path, /*deadline*/ None)
|
||||
.await
|
||||
.expect("integrity check should run");
|
||||
|
||||
assert_eq!(result, vec!["ok".to_string()]);
|
||||
assert_eq!(
|
||||
result,
|
||||
SqliteIntegrityCheck::Complete(vec!["ok".to_string()])
|
||||
);
|
||||
|
||||
let pool = sqlite
|
||||
.open_read_write_pool(&path)
|
||||
.await
|
||||
.expect("reopen sqlite db");
|
||||
let mut connection = pool.acquire().await.expect("acquire writer");
|
||||
// WAL readers do not wait on this writer, so use a rollback journal.
|
||||
sqlx::query("PRAGMA journal_mode=DELETE; BEGIN EXCLUSIVE")
|
||||
.execute(&mut *connection)
|
||||
.await
|
||||
.expect("hold an exclusive lock");
|
||||
let started = Instant::now();
|
||||
let result =
|
||||
sqlite_integrity_check(&sqlite, &path, Some(started + Duration::from_millis(50)))
|
||||
.await
|
||||
.expect("interrupt lock wait");
|
||||
let elapsed = started.elapsed();
|
||||
sqlx::query("ROLLBACK")
|
||||
.execute(&mut *connection)
|
||||
.await
|
||||
.expect("release lock");
|
||||
assert_eq!(result, SqliteIntegrityCheck::TimedOut(Vec::new()));
|
||||
assert!(elapsed < Duration::from_secs(1), "lock wait: {elapsed:?}");
|
||||
|
||||
// Misdescribe one index entry so SQLite emits an error before its first progress callback.
|
||||
sqlx::query(
|
||||
"PRAGMA writable_schema=ON; UPDATE sqlite_schema SET sql='CREATE UNIQUE INDEX sample_value ON sample(other)' WHERE name='sample_value'; PRAGMA writable_schema=OFF",
|
||||
)
|
||||
.execute(&mut *connection)
|
||||
.await
|
||||
.expect("introduce an index mismatch");
|
||||
drop(connection);
|
||||
pool.close().await;
|
||||
assert_eq!(
|
||||
sqlite_integrity_check(&sqlite, &path, Some(Instant::now()))
|
||||
.await
|
||||
.expect("retain corruption before interruption"),
|
||||
SqliteIntegrityCheck::TimedOut(vec![
|
||||
"row 1 missing from index sample_value".to_string()
|
||||
]),
|
||||
);
|
||||
let _ = tokio::fs::remove_dir_all(codex_home).await;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user