Skip to content

fix: main window hangs on exit; one progress bar for code generation - #2507

Merged
kevinferrare merged 2 commits into
masterfrom
fix/exit-and-codegen-progress
Nov 12, 2026
Merged

kevinferrare merged 2 commits into
masterfrom
fix/exit-and-codegen-progress

Conversation

@kevinferrare

@kevinferrare kevinferrare commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Window does not close after the emulated program exits. A thread dump of a hung process showed the UI thread blocked in McpHttpHost.Stop() on StopAsync().GetAwaiter().GetResult(). Kestrel had already stopped, but GenericWebHostService.StopAsync posted its continuation to the captured Avalonia UI context, which was the blocked thread: a deadlock. Stop() now runs the teardown on the thread pool.
  • SIGTERM / Ctrl+C were ignored (only kill -9 worked) because the MCP host used the default ConsoleLifetime. The no-op EmbeddedHostLifetime of the HTTP API server moves to its own file, and the MCP host uses it too.
  • Code generation looked frozen at 100%. After the method loop, program analysis (about 11-20 s on Wolf3D) and printing ran with no progress. Code generation now has numbered phases (1/5) to (5/5) that share one progress bar with fixed shares: preparing methods 0-40%, program analysis 40-90%, printing methods 90-100%. Program analysis reports its 4 parts as steps, and the calling conventions fixed point reports each round so the text changes about every second. Printing reports one step per method. Slow-method lines are now Code generation: slow method ....

Test plan

  • New McpHttpHostTest.Stop_Completes_WhenCallerSynchronizationContextIsBlocked: times out on the old code, passes now
  • GeneratorProgressTest, DumpProgressTest, DumpProgressReporterTest, DumpProgressViewModelTest (new CodeGenerationPhasesShareOneProgressBar)
  • Full suite without SingleStepTest: 7872/7872; Spice86.Tests.UI: 79/79; no golden changes
  • Release build: 0 warnings
  • Manual: run a program, exit it, check that the window closes and the progress bar fills once during code generation

McpHttpHost.Stop blocked the UI thread on StopAsync while ASP.NET posted a
continuation back to that same thread, so the window never closed. Run the
teardown on the thread pool. Also give the MCP host the no-op host lifetime
already used by the HTTP API server, so SIGTERM and Ctrl+C are no longer
swallowed.
The progress window showed 100% after the method preparation loop, then
spent seconds in program analysis and printing with no visible change.
Split code generation into numbered phases (1/5 to 5/5) sharing one bar
with fixed shares, report the program analysis parts and each calling
conventions round as steps, and report a step per printed method.
Copilot AI balanced review requested due to automatic review settings November 11, 2026 23:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

}
_mcpFileLogger?.Dispose();
_mcpFileLogger = null;
Task.Run(() => DisposeAsync().AsTask()).GetAwaiter().GetResult();

@maximilien-noal maximilien-noal Nov 12, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sync over async and thread pool thread used for a lightweight task (the Task is not CPU bound)

The getAwaiter getResult should be removed, task.Run should be removed. And the Task should be awaited. And the method should be of type async Task. And the caller should be async, and so on (async is viral)

To avoid a continuation on the UI thread SynchronizationContext use .ConfigureAwait(false)

await someTask.ConfigureAwait(false)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed to be done in a separate PR, thanks for the review :)

@maximilien-noal
maximilien-noal self-requested a review November 12, 2026 10:06
@kevinferrare
kevinferrare merged commit 5bd4c6d into master Nov 12, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants