fix: Telegram session creation holds the global session mutex while doing blocking DB writes#894
Open
sam-saffron-jarvis wants to merge 1 commit into
Open
Conversation
…oing blocking DB writes
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.
What changed
telegramSessionMgr.mu, then release the global session mutex before writing session metadata to the store.context.WithTimeout(context.WithoutCancel(ctx), ...)so a canceled or unbounded serve context cannot leaveCreate/SetCurrentwedged forever.Createcall: another chat can still create and return its session while the first persistence call is stalled.persistSessionpasses a bounded, non-canceled context to store writes.Why this is high-value
Sam uses the Telegram/Jarvis path daily. Before this change, a slow or locked session store write during new/reset session persistence held the single
telegramSessionMgr.mu, blocking every other chat'sgetOrCreate, reset, and shutdown session-map access. Moving only the blocking I/O out of the mutex preserves the winner decision while preventing one SQLite/filesystem stall from making the Telegram bot globally appear stuck.Validation
gofmt -w internal/serve/telegram.go internal/serve/telegram_test.gogo build ./...go test ./internal/serve -run 'TestTelegramSessionMgr(GetOrCreate_DoesNotBlockOtherChatsWhilePersistingSession|PersistSession_UsesBoundedContextDetachedFromCallerCancel|GetOrCreate_PersistsOnlyWinningConcurrentSession|ResetSessionIfCurrent_RejectsStaleExpectedSession)'git diff --checkgo test ./...was run twice; both runs reached the changedinternal/servetests successfully but failed in unchangedinternal/jobs.TestProgramRunnerDoesNotLeakBackgroundChildrenbecause its background child process was still observed alive after 1s.