Skip to content

Harden coordinator timer cleanup and tests - #15375

Merged
AR-May merged 6 commits into
mainfrom
dustinca/harden-coordinator-and-fix-tests
Oct 23, 2026
Merged

AR-May merged 6 commits into
mainfrom
dustinca/harden-coordinator-and-fix-tests

Conversation

@DustinCampbell

Copy link
Copy Markdown
Member

Related issue(s): #14193

Context

Timer.Dispose() prevents future scheduling but does not wait for callbacks that are already running.
A coordinator heartbeat or auto-shutdown callback could therefore continue after server cleanup,
access disposed state, or write through an xUnit ITestOutputHelper after its valid lifetime.

Coordinator integration tests also allowed separately launched coordinator processes to outlive test
cleanup, leaving trace files open while subsequent tests inspected build-failure logs.

ConcurrentClients_AllReceiveGrants also withheld every client handshake until all concurrent pipe
connections completed. On Windows .NET Framework, accepted server handlers could block waiting for
those handshakes while another connection timed out.

Changes Made

  • Add a two-phase DrainableTimer that stops future callbacks separately from draining callbacks
    already in progress.
  • Stop server timers when RunAsync exits and drain them when CoordinatorServer is disposed.
  • Add regression coverage proving that disposal waits for an in-flight shutdown callback.
  • Wait for external coordinator processes to exit before integration-test cleanup examines or removes
    trace files.
  • Restore the cached coordinator debug path after each integration test.
  • Extract TestCoordinatorClient to encapsulate pipe connections, handshakes, protocol I/O, and
    common client operations.
  • Centralize server creation, cancellation, shutdown, and disposal in documented test helpers.
  • Complete each concurrent client handshake during connection so accepted server handlers can
    continue while remaining pipe instances are created.
  • Keep ConcurrentClients_AllReceiveGrants quarantined while the fix gathers stability evidence.

Testing

The MSBuild Coordinator tests were run and are passing on .NET Framework and .NET 11.

Dependencies and Follow-up

ConcurrentClients_AllReceiveGrants remains associated with #14193 and retains its
Windows-scoped [ActiveIssue] attribute. It can be unquarantined after the quarantine pipeline
collects sufficient post-merge stability evidence.

Timer.Dispose() prevents future scheduling but does not wait for callbacks
that are already running. A heartbeat or auto-shutdown callback could
therefore continue after CoordinatorServer cleanup and access disposed
state or write through an xUnit ITestOutputHelper after the test ended.

Add a DrainableTimer that separates stopping from draining. RunAsync stops
future callbacks without blocking its shutdown path, while
CoordinatorServer.Dispose waits for active callbacks before disposing
shared server state.
Block the auto-shutdown callback in the test debug output and verify that
CoordinatorServer.Dispose does not return until the callback completes.

Allow TestCoordinatorDebugOutput to observe messages so timer lifecycle
tests can coordinate with callbacks without changing production code.
Coordinator builds can finish before the separately launched server reaches
its idle timeout. Test cleanup could then inspect or delete trace files while
the coordinator was still writing them.

Wait for the per-test server mutex to disappear before restoring test state,
then refresh the cached debug path and remove the trace directory.
Encapsulate pipe connection, handshake, protocol readers and writers, and
common client messages in TestCoordinatorClient.

Refactor CoordinatorServer tests to describe requests and responses
directly while preserving raw protocol validation and concurrent ordering.
Centralize server creation, cancellation, and shutdown in documented test
helpers so each test can focus on its coordinator protocol scenario.

Keep auto-shutdown and disposal tests in control of their special lifecycle,
and suppress CA2007 because xUnit tests intentionally use normal awaits.
Complete each client handshake as part of its concurrent connection instead
of waiting for every pipe to connect before sending any protocol data.

This lets accepted server handlers progress while the accept loop creates
the remaining pipe instances, avoiding Windows .NET Framework pipe timeouts.
Keep the test quarantined while stability evidence is collected.

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.

🟡 Changes recommended

Exception-path cleanup can leave client pipes open and cause server shutdown waits to hang indefinitely.

2 open issues
What changed in this PR

This PR hardens coordinator shutdown and test cleanup so timer callbacks and external coordinator processes do not outlive the resources they use.

Changes:

  • Separates timer stopping from callback draining and adds an in-flight callback disposal test.
  • Waits for external coordinators during integration-test cleanup and restores the cached debug path.
  • Centralizes test client/server lifecycle handling and performs handshakes during concurrent connections while retaining Windows quarantine.
File Description
src/​MSBuild.Coordinator/​CoordinatorServer.DrainableTimer.cs Adds two-phase timer stopping and draining.
src/​MSBuild.Coordinator/​CoordinatorServer.cs Integrates drainable timers and cancellation checks.
src/​MSBuild.Coordinator.UnitTests/​TestCoordinatorDebugOutput.cs Adds an observer for synchronizing callback tests.
src/​MSBuild.Coordinator.UnitTests/​TestCoordinatorClient.cs Encapsulates pipe ownership, handshakes, and protocol operations.
src/​MSBuild.Coordinator.UnitTests/​GlobalSuppressions.cs Suppresses ConfigureAwait guidance for xUnit tests.
src/​MSBuild.Coordinator.UnitTests/​CoordinatorServer_Tests.cs Refactors lifecycle helpers and tests callback draining.
src/​MSBuild.Coordinator.UnitTests/​CoordinatorIntegration_Tests.cs Waits for coordinator exit and restores debug-path state.

Review effort: Balanced

@AR-May AR-May left a comment

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.

LGTM

@AR-May
AR-May merged commit 65e29bb into main Oct 23, 2026
20 of 22 checks passed
@AR-May
AR-May deleted the dustinca/harden-coordinator-and-fix-tests branch October 23, 2026 08:48

This branch had an error being deployed

1 failed deployment
copilot-pat-pool — 25b2586f Deployed Oct 23, 2026 by DustinCampbell via conclusion #1059
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