Skip to content

Fix intermittent deadlock in AnsiTerminal_OutputProgressFrameIsCorrect#7584

Merged
Evangelink merged 2 commits intomainfrom
copilot/fix-ansi-terminal-output-hang
Mar 20, 2026
Merged

Fix intermittent deadlock in AnsiTerminal_OutputProgressFrameIsCorrect#7584
Evangelink merged 2 commits intomainfrom
copilot/fix-ansi-terminal-output-hang

Conversation

Copy link
Contributor

Copilot AI commented Mar 20, 2026

AnsiTerminal_OutputProgressFrameIsCorrect would intermittently hang on macOS due to a deadlock between the background progress refresh timer and the test's synchronization mechanism.

Root Cause

ThreadProc invoked OnProgressStartUpdate while holding _lock. The test subscribes to this event with a blocking WaitOne() to gate timer updates for deterministic output. If the timer fired during a TestCompleted call, the following deadlock occurred:

Thread State
Timer thread Holds _lock, blocked on startHandle.WaitOne()
Test thread Blocked in WriteToTerminal waiting to acquire _lock
Test thread Never reaches startHandle.Set() → hang

Fix

Move OnProgressStartUpdate?.Invoke(...) to before the lock acquisition in ThreadProc:

// Before
lock (_lock)
{
    OnProgressStartUpdate?.Invoke(this, EventArgs.Empty); // blocks while holding lock
    _terminal.StartUpdate();
    ...
}

// After
OnProgressStartUpdate?.Invoke(this, EventArgs.Empty); // blocks without holding lock
lock (_lock)
{
    _terminal.StartUpdate();
    ...
}

No production behavior change — OnProgressStartUpdate has no subscribers outside tests.

Original prompt

This section details on the original issue you should resolve

<issue_title>AnsiTerminal_OutputProgressFrameIsCorrect hangs intermittently on macOS</issue_title>
<issue_description>The test hangs intermittently on macOS and usually passes on retry.</issue_description>

Comments on the Issue (you are @copilot in this section)


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

…t to hang on macOS

Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix intermittent hang in AnsiTerminal_OutputProgressFrameIsCorrect test Fix intermittent deadlock in AnsiTerminal_OutputProgressFrameIsCorrect Mar 20, 2026
Copilot AI requested a review from Youssef1313 March 20, 2026 15:27
@Youssef1313
Copy link
Member

Clearer description, also from Copilot:

image

I manually verified that Copilot investigation is correct. This can also be reproduced on Windows by modifying the test to have some Thread.Sleep right after TestExecutionStarted is called in the test method.

What happens then is (same as Copilot investigation but put a little bit differently):

  1. TestExecutionStarted is called -> creates a thread that runs ThreadProc, acquires lock and tirggers the event inside the lock.
  2. For the event handler to complete, the test needs to reach startHandle.Set()
  3. For the test to reach startHandle.Set(), it needs to complete the TestCompleted calls, which need to acquire the same lock held in step 1.

When the test is passing, what happens is that the calls after TestExecutionStarted are scheduled and runs before ThreadProc acquires the lock. That's why adding a sleep causes the hang to be more obvious, as it ensures that ThreadProc acquires the lock first.

@Youssef1313 Youssef1313 marked this pull request as ready for review March 20, 2026 15:40
Copilot AI review requested due to automatic review settings March 20, 2026 15:40
@Evangelink Evangelink enabled auto-merge March 20, 2026 15:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes an intermittent macOS hang in AnsiTerminal_OutputProgressFrameIsCorrect by avoiding a lock inversion/deadlock between the progress refresh timer thread and test synchronization.

Changes:

  • Move OnProgressStartUpdate invocation before _lock acquisition in ThreadProc to prevent blocking while holding _lock.
  • Add an explanatory comment documenting the deadlock scenario and rationale.

@Evangelink Evangelink merged commit 94dfc94 into main Mar 20, 2026
14 checks passed
@Evangelink Evangelink deleted the copilot/fix-ansi-terminal-output-hang branch March 20, 2026 16:48
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.

AnsiTerminal_OutputProgressFrameIsCorrect hangs intermittently on macOS

4 participants