Conversation
There was a problem hiding this comment.
Bug: WAL mode not checkpointed before database file copy
The new code applies PRAGMA journal_mode=WAL on every connection via _apply_hardened_pragmas, but the fork, checkpoint, and restore methods use shutil.copyfile to copy only the main database file. In WAL mode, uncommitted transactions live in the separate -wal file. Copying just the main file without first running PRAGMA wal_checkpoint(TRUNCATE) or copying the WAL/SHM files can result in incomplete or inconsistent database copies, leading to data loss or corruption.
eval_protocol/agent/resources/sql_resource.py#L136-L137
python-sdk/eval_protocol/agent/resources/sql_resource.py
Lines 136 to 137 in 64025a4
eval_protocol/agent/resources/sql_resource.py#L150-L151
python-sdk/eval_protocol/agent/resources/sql_resource.py
Lines 150 to 151 in 64025a4
eval_protocol/agent/resources/sql_resource.py#L172-L173
python-sdk/eval_protocol/agent/resources/sql_resource.py
Lines 172 to 173 in 64025a4
…ors and improve corruption detection. Add tests to ensure valid databases are not deleted on non-corruption DatabaseErrors.
This update introduces a new function, _checkpoint_and_copy_database, which ensures that all data in the Write-Ahead Logging (WAL) file is flushed to the main database file before copying. This function is now utilized in the SQLResource class for database forking, checkpointing, and restoring operations, enhancing data integrity during these processes.
| return True | ||
| raise DatabaseCorruptedError(db_path, e) | ||
| # For other DatabaseErrors (locks, busy, etc.), re-raise without deleting | ||
| raise |
There was a problem hiding this comment.
Bug: Database connection not closed on exception
In check_and_repair_database, when a DatabaseError is raised after test_db.connect() succeeds (during execute_sql or fetchone), the test_db connection is never closed. The test_db.close() call at line 56 only executes in the happy path. If an exception occurs, the exception handler at lines 67-82 catches DatabaseError but doesn't close the connection, causing a resource leak. The try block lacks a finally clause to ensure cleanup.
name: Pull Request
about: Propose changes to the codebase
title: "Brief description of changes"
labels: ''
assignees: ''
Description
Please include a summary of the change and which issue is fixed or feature is implemented. Please also include relevant motivation and context. List any dependencies that are required for this change.
Fixes # (issue)
Implements # (issue)
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration.
Test Configuration:
Checklist:
black .,isort .,flake8 .)Screenshots (if applicable)
If applicable, add screenshots to help showcase your changes.
Additional context
Add any other context about the PR here.
Note
Applies hardened SQLite settings and safe checkpoint-and-copy flows, adds corruption detection with auto-repair and CLI recovery, and introduces comprehensive concurrency/corruption tests.
SQLITE_HARDENED_PRAGMASand apply WAL, busy timeouts, and related pragmas across databases.check_and_repair_database,DatabaseCorruptedError, and_backup_and_remove_database.safe=True.SQLResource: use hardened connections and_checkpoint_and_copy_databasefor fork/checkpoint/restore to flush WAL before copying.SqliteEventBusDatabaseandSqliteEvaluationRowStore: initialize with hardened pragmas, auto-repair on init, and safe table creation.logs_command: detect SQLite corruption, prompt for auto-fix (backup/reset), and retry startup; improve error handling.tests/test_sqlite_hardening.pycovering pragmas, integrity checks, backup/removal, auto-repair on init, and concurrent reads/writes.pyproject.toml: includetestsin Pyright analysis; adjust excludes.Written by Cursor Bugbot for commit f05a1ff. This will update automatically on new commits. Configure here.