fix(server): prevent devenv.exe from hanging on shutdown - #99
Merged
Conversation
Package disposal blocked the Visual Studio UI thread on ServerProcessManager .StopAsync(), whose awaits captured that thread's synchronization context and posted their continuations straight back to it. The shutdown never completed, so devenv.exe stayed resident after the main window closed. The deadlock only reproduced with the server running: with no child process and no listening pipe, every await in the path completed synchronously and nothing was ever posted back. - Use ConfigureAwait(false) throughout the shutdown path in ServerProcessManager and RpcServer. - Run shutdown via Task.Run and wait with a timeout in package disposal, so no continuation can need the UI thread and VS exits regardless. - Bound the cooperative shutdown. A half-open pipe could leave the RPC shutdown call pending forever; the previous 5s + 2s waits were also unbounded in the worst case. - Assign the server process to a kill-on-close job object so it dies with Visual Studio even when devenv.exe terminates abnormally, rather than surviving to hold its HTTP port against the next session. - Stop suppressing VSTHRD002 project-wide. That analyzer flagged this exact deadlock; it is now suppressed only at the two Dispose call sites that genuinely require a blocking wait, each with a justification. - Fix the pipe listener faulting its task when cancelled during the retry backoff, and avoid disposing the cancellation source out from under it. Adds a test project with regression coverage that drives the shutdown path from a thread whose synchronization context cannot run posted callbacks. Both new tests fail against the previous code and pass against this change.
The test project references the extension project, so a standalone test workflow rebuilt the extension and its self-contained server publish a second time on every PR. Passing test-project to the shared vsix-build workflow runs the tests in the same job, reusing the build output. Requires CodingWithCalvin/.github#76.
Contributor
Author
🔧 CI consolidationDropped the standalone Tests now run from
|
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.
Resolves #97
The problem
MCPServerPackage.Disposeruns on the Visual Studio UI thread and blocked it onServerManager.StopAsync(). That method had noConfigureAwait(false)anywhere, so everyawaitinside captured the UI thread's synchronization context and posted its continuation back to the thread already blocked waiting for it. Classic deadlock: the main window closes, the UI thread never returns fromDispose, and devenv.exe stays in Task Manager.The reporter noted this only happens when the server is running, which is the confirming detail. With no child process and no listening pipe,
RequestShutdownAsyncand bothStopAsyncmethods return already-completed tasks, awaiting those continues inline, and nothing is ever posted back.The fix
ConfigureAwait(false)throughout the shutdown path inServerProcessManagerandRpcServer, so no continuation ever needs the caller's thread back.Task.Runand waits with a timeout.Task.Runstarts with no synchronization context, so nothing can post back to the UI thread; the timeout bounds the damage if anything ever does.5s+2swaits had no ceiling in that case. Now: 1s for the RPC ack, 1.5s for a clean exit, then kill with a 0.5s wait.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, so it dies with Visual Studio even if devenv.exe crashes or is killed from Task Manager. This fixes the mirror-image problem nobody had filed yet: an orphaned server holding port 5050 against the next VS session.VSTHRD002is no longer suppressed project-wide. That analyzer exists to catch exactly this deadlock and was globally disabled. It is now suppressed only at the twoDisposecall sites that genuinely need a blocking wait, each with a written justification.catchblock and faulted the listener task; the cancellation source could also be disposed while the listener was still observing its token.Tests
This adds the repo's first test project (
CodingWithCalvin.MCPServer.Tests, xunit on net48) plus aTestworkflow, since the existing build workflow only compiles the VSIX project and would not have run them.ServerShutdownTestsdrives the shutdown path from a thread whose synchronization context silently drops posted callbacks — the observable behaviour of a UI thread blocked inDispose. Both tests were verified to fail against the previous code and pass against this change.ProcessJobObjectTestscovers the new P/Invoke wrapper, including that closing the job actually terminates an assigned process.