fix(gateway): serialize dashboard and cron SQLite access - #104
Draft
cursor[bot] wants to merge 1 commit into
Draft
cursor[bot] wants to merge 1 commit into
cursor[bot] wants to merge 1 commit into
Conversation
The vendored SQLite build is SQLITE_THREADSAFE=0, and the gateway HTTP thread shares that connection with the main loop. Dashboard memory, session list, and cron routes, plus cron_poll's due-job query, touched g_db without agent_lock. A dashboard refresh during a turn or a cron poll is undefined and can crash the process or corrupt memory.db. Co-authored-by: esadrianno <esadrianno@gmail.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug and impact
Vendored SQLite is compiled with
SQLITE_THREADSAFE=0, so onesqlite3 *(g_db) must not be used from two threads. The gateway HTTP server runs on its own thread. These callers touchedg_dbwithoutagent_lock():GET /api/memory(memory_recall)GET /api/sessions(session_list)GET/POST/api/cronand cron delete/togglecron_pollon the main loop (cron_job_list_due/cron_job_get_by_id), which runs every poll even when no job is dueA concrete trigger: gateway enabled, dashboard open (it polls sessions, memory, and cron), and either an in-flight
agent_runor the main-loop cron poll. Concurrent use of the connection is undefined. That can crash the process or corruptmemory.db(sessions, memories, and cron jobs).DELETE /api/sessions/:idhas the same missing lock. That path is already covered by #103 and is left unchanged here.Root cause
#60locked inbound ASAPstate.queryandmcp.tool_call, andhandle_messagelocksagent_run/ cron ack. The dashboard handlers andcron_pollwere not updated to the same rule.Fix
Hold
agent_lock()around those SQL calls.cron_pollreleases the lock before the re-offer sleep so a dashboard request is not blocked for the wait. The mutex is not recursive; tool execution insideagent_runstill calls the SQL helpers directly and does not take the lock again.Validation
tests/test_cron.ctest_cron_poll_holds_agent_lockfailed before the lock (SQLite entry while the mutex was free) and passes after.tests/test_gateway_db_lockcallsdispatch_routefor memory, session list, and cron create/list/toggle/delete and requiresagent_lockto be held on each SQL entry and released afterward../build/test_cron,./build/test_gateway_db_lock,./build/test_dispatch,./build/test_memory,./build/test_agent, and./build/test_asap_serverpassed.