fix(state): discard pooled read connections after DatabaseError - #87018
Conversation
_read_ctx always returned the borrowed reader to the LIFO pool, so a replaced or truncated state.db kept serving stale or failing queries until process restart. Close the connection on sqlite3.DatabaseError instead. Successful reads still recycle. Fixes NousResearch#86518
Duplicate of #85255: both repair the same |
fix(state): discard pooled read connections after DatabaseError
|
What does this PR do?
_read_ctxalways returned a pooled read connection to the LIFO pool, even aftersqlite3.DatabaseError. After an external replace/repair ofstate.db(forked curator closing the write fd, offline heal), that reader then either fails forever (file is not a database) or silently serves the old inode. The write path already self-heals via_reconnect_after_notadb; the read pool did not.On
DatabaseError, the connection is now closed and discarded. Successful reads still return to the pool.Related Issue
Fixes #86518
Type of Change
Changes Made
hermes_state.py(_read_ctx): close the borrowed reader onsqlite3.DatabaseErrorinstead ofput_nowait.tests/test_session_db_read_conn_pool.py: a simulatedfile is not a databaseerror is discarded; a successful checkout is still recycled.How to Test
sqlite3.DatabaseErrorinside_read_ctx.mainthe connection is returned to the pool (put_nowaitcalled)._close_read_connis called andput_nowaitis not._read_ctxstill returns the connection to the pool.scripts/run_tests.sh tests/test_session_db_read_conn_pool.pyfor the two new tests plus existing fallback coverage.Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/AScreenshots / Logs
Fail-on-base:
test_database_error_discards_pooled_read_connfails onorigin/mainbecause the broken reader is recycled. Branch: discard + recycle tests pass.ruff/uv lock --checkclean.