CRITICAL fix: use async save_session to avoid blocking tokio runtime#534
Open
skymoore wants to merge 2 commits intoRightNow-AI:mainfrom
Open
CRITICAL fix: use async save_session to avoid blocking tokio runtime#534skymoore wants to merge 2 commits intoRightNow-AI:mainfrom
skymoore wants to merge 2 commits intoRightNow-AI:mainfrom
Conversation
This was referenced Mar 12, 2026
save_session() was synchronous, holding a Mutex<Connection> on the tokio worker thread during SQLite writes. On pods with 1 CPU core (1 tokio worker thread), this starved the entire runtime — including health check endpoints — causing K8s to mark the pod not-ready and return 504 on all subsequent requests. Add save_session_async() that wraps the SQLite write in spawn_blocking, matching the pattern already used by other memory operations (recall, remember, etc.). Update all 12 call sites in the agent loop.
The health endpoint called structured_get() synchronously on the tokio async runtime, acquiring the shared std::sync::Mutex<Connection> on a worker thread. When the agent loop held this mutex during session saves, the health check blocked the tokio thread, starving the SSE stream and causing Kubernetes probe timeouts. - Health and health_detail now run the DB check via spawn_blocking - SSE message/stream endpoint now includes keep_alive to flush periodic heartbeats even during contention
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.
Summary
Prevents 1 cpu openfang deployments from hanging on save session and not responding to any requests
Changes
save_session() was synchronous, holding a Mutex on the tokio worker thread during SQLite writes. On pods with 1 CPU core (1 tokio worker thread), this starved the entire runtime — including health check endpoints — causing K8s to mark the pod not-ready and return 504 on all subsequent requests.
Add save_session_async() that wraps the SQLite write in spawn_blocking, matching the pattern already used by other memory operations (recall, remember, etc.). Update all 12 call sites in the agent loop.
Testing
cargo clippy --workspace --all-targets -- -D warningspassescargo test --workspacepassesSecurity