Skip to content

Fixes #5579. SynchronizationContext is not correctly implemented in v2#5588

Merged
BDisp merged 5 commits into
tui-cs:developfrom
BDisp:issue/mainloop-sync-context
Jul 9, 2026
Merged

Fixes #5579. SynchronizationContext is not correctly implemented in v2#5588
BDisp merged 5 commits into
tui-cs:developfrom
BDisp:issue/mainloop-sync-context

Conversation

@BDisp

@BDisp BDisp commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Fixes

Proposed Changes/Todos

  • The SynchronizationContext is set on the Init method and also on the Begin method. Without the fix the unit tests will be fail.

Pull Request checklist:

  • I've named my PR in the form of "Fixes #issue. Terse description."
  • My code follows the style guidelines of Terminal.Gui - if you use Visual Studio, hit CTRL-K-D to automatically reformat your files before committing.
  • My code follows the Terminal.Gui library design guidelines
  • I ran dotnet test before commit
  • I have made corresponding changes to the API documentation (using /// style comments)
  • My changes generate no new warnings
  • I have checked my code and corrected any poor grammar or misspellings
  • I conducted basic QA to assure all features are working

@BDisp BDisp requested a review from tig as a code owner July 8, 2026 13:43
…d guarantee set the sync context for the current running app.

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.

Pull request overview

Addresses #5579 by restoring a Terminal.Gui main-loop-backed SynchronizationContext so async/await continuations resume on the app’s main thread in v2.

Changes:

  • Reintroduces MainLoopSyncContext and sets it during ApplicationImpl.Init and session Begin.
  • Adds unit tests asserting SynchronizationContext.Post runs on the app’s main thread and is instance-specific.
  • Updates MainLoopCoordinator to avoid capturing synchronization context during startup waits.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
Tests/UnitTestsParallelizable/Application/ApplicationImplTests.cs Adds tests validating SynchronizationContext posts back to the app main thread and is per-app-instance.
Terminal.Gui/App/MainLoop/MainLoopSyncContext.cs Implements an app-instance-backed SynchronizationContext that marshals work to the app via Invoke.
Terminal.Gui/App/MainLoop/MainLoopCoordinator.cs Uses ConfigureAwait (false) while awaiting startup tasks to avoid context capture.
Terminal.Gui/App/ApplicationImpl.Run.cs Ensures the app’s main-loop sync context is set when a session begins.
Terminal.Gui/App/ApplicationImpl.Lifecycle.cs Creates and installs the app’s MainLoopSyncContext during Init.

Comment thread Terminal.Gui/App/MainLoop/MainLoopSyncContext.cs
Comment thread Terminal.Gui/App/ApplicationImpl.Lifecycle.cs Outdated
Comment thread Tests/UnitTestsParallelizable/Application/ApplicationImplTests.cs
BDisp and others added 2 commits July 9, 2026 07:57
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

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.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Comment thread Tests/UnitTestsParallelizable/Application/ApplicationImplTests.cs
Comment thread Terminal.Gui/App/ApplicationImpl.Run.cs
Comment thread Terminal.Gui/App/ApplicationImpl.Lifecycle.cs Outdated
@BDisp BDisp marked this pull request as draft July 9, 2026 13:44
@BDisp BDisp marked this pull request as ready for review July 9, 2026 15:38
@BDisp BDisp merged commit 5160742 into tui-cs:develop Jul 9, 2026
14 checks passed
@BDisp BDisp deleted the issue/mainloop-sync-context branch July 9, 2026 19:19
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.

SynchronizationContext is not correctly implemented in v2

3 participants