Fix multi-process race in Wrapper.Start (LOCALDB_ERROR_INSTANCE_DOES_NOT_EXIST)#929
Merged
SimonCropp merged 6 commits intoSimonCropp:mainfrom May 6, 2026
Merged
Fix multi-process race in Wrapper.Start (LOCALDB_ERROR_INSTANCE_DOES_NOT_EXIST)#929SimonCropp merged 6 commits intoSimonCropp:mainfrom
SimonCropp merged 6 commits intoSimonCropp:mainfrom
Conversation
Implement serialization of the `Wrapper.Start` method for LocalDB instances using both in-process and cross-process locks. For in-process synchronization, employ a `ConcurrentDictionary` to manage `SemaphoreSlim` instances keyed by instance name, ensuring that two `Wrapper` instances within the same process don't race on the same LocalDB instance. For cross-process synchronization, use a lock file in `%TEMP%` with `FileShare.None`, preventing other processes from starting until the current instance has completed its startup task. This fix addresses the concurrency issues demonstrated by `ConcurrentStartTests` in single and multi-process scenarios. Such synchronization ensures no template rebuilds occur unexpectedly, avoiding potential races during internal state setup. The `WrapperTests` suite remains passing, confirming that existing functionality is preserved. These changes are motivated by the need to prevent the awkward interactions that can arise when multiple processes or threads attempt to interact with the same LocalDB instance simultaneously, posing risks of database corruption or test flakiness.
This reverts commit f15cf15.
This was referenced May 6, 2026
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
Wrapper.Startis unsafe under concurrent calls — both within one process and across processes — whentemplate.mdfis missing. Symptoms include:0x89C50107(LOCALDB_ERROR_INSTANCE_DOES_NOT_EXIST)CREATE DATABASE [template]template.mdf"file not found" errorsReal-world trigger: two
dotnet testhosts (e.g. CLI + Rider) sharing a LocalDB user instance. Once the wrapper directory is empty, everyStarthits the destructiveStopAndDelete+CleanStartbranch and races every other process doing the same.Fix
Serialize
Wrapper.Startper instance name with two layers, both held untilstartupTaskcompletes:ConcurrentDictionary<string, SemaphoreSlim>keyed by instance name. (The old per-instancesemaphoreSlimfield was declared but never used.)FileStreamopened withFileShare.Noneon%TEMP%\localdb_wrapper_<instanceName>.lock. Used in preference toMutexbecause file handles aren't thread-affine, so the lock survivesawait.Tests added
ConcurrentStartTests— single-process race (deterministic SQL deadlock without the fix).MultiProcessConcurrentStartTests— symmetric multi-process race using a newLocalDb.MultiProcessHelperexe spawned viaProcess.Start.InstanceDoesNotExistRaceTests— asymmetric killer/victim child processes that deterministically reproduce the exact0x89C50107error code.All three race tests fail without the fix and pass with it. The 23 existing
WrapperTestsstill pass.See
src/LocalDb.MultiProcessHelper/README.mdfor the full root-cause writeup and per-piece rationale.