Repository navigation
fix(treatment-service): remove nested RLock deadlock in LocalStorage - #94
Closed
GalihFajar wants to merge 3 commits into
Closed
GalihFajar wants to merge 3 commits into
GalihFajar wants to merge 3 commits into
Conversation
findSubscribedProjectSettingsById held the read lock and called findProjectSettingsById, which took it again. A writer queued between the two RLock calls blocks the nested one, so the reader and writer wait on each other forever and every later read on the pod blocks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch is waiting to be 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.
Problem
findSubscribedProjectSettingsByIdtooks.RLock()and then calledfindProjectSettingsById, which tooks.RLock()again on the same goroutine. In Go, a waiting writer blocks every newRLock(). So if a writer asked forLock()between the twoRLock()calls, the nestedRLock()waited for the writer and the writer waited for the outerRLock(). Both waited forever. Writers include the poller'sInit()every 30s and the pubsub update handlers.After that, every read of
LocalStorageon the pod blocked. In production, one Turing router pod returned 408 for all requests while goroutines and memory grew, until the pod was OOMKilled after about 10 minutes. This happened about every 1 to 3 weeks.Fix
findProjectSettingsByIdLocked. It does the lookup and takes no lock. The caller must hold the lock.findProjectSettingsByIdtakesRLockand calls it.findSubscribedProjectSettingsByIdcalls it directly, because it already holdsRLock.Behavior does not change. I checked all lock sites in
LocalStorage, and no other code path takes the lock again in the same goroutine.This MR replaces the lock-fix part of #93. The
LocalStoragemetrics from #93 are not included and can go in a separate MR.Test
TestFindProjectSettingsWithId_ConcurrentWithWriter_NoDeadlockruns 50 readers that callFindProjectSettingsWithIdand 1 writer that callsUpdateProjectSettings2000 times, with a 5s deadline.-race, and 3/3 failed without it.-race, and 30/30 passed without it.If the test times out, its goroutines stay blocked. This is expected, because they are stuck in the deadlock that the test detects.
Follow-ups (not in this MR)
FindProjectSettingsWithId: for a project that is not subscribed, or not found, each request calls the management API and appends tos.ProjectSettingswith no dedup. The slice grows until the nextInit().fetchProjectSettingsWithIdandgetProjectSettingsuseJSON200without a status or nil check. A non-200 response causes a panic.InsertProjectSettingschecks and inserts in two separate lock sections. Concurrent inserts can add duplicates.UpdateProjectSegmenterswrites intos.ProjectSegmenters[pid]and panics if that map is nil.fetchProjectSettingsWithIdcan append to the old slice whileInit()swaps in a new one. The appended entry is lost.🤖 Generated with Claude Code