Skip to content

Fix unhandled ObjectDisposedException in TimestampedFileLogger during workload repair#54097

Open
Copilot wants to merge 1 commit into
mainfrom
copilot/fix-unhandled-exception-wasm-tools-repair
Open

Fix unhandled ObjectDisposedException in TimestampedFileLogger during workload repair#54097
Copilot wants to merge 1 commit into
mainfrom
copilot/fix-unhandled-exception-wasm-tools-repair

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 27, 2026

After dotnet workload repair completes on Windows, an unhandled ObjectDisposedException crashes the process because BlockingCollection<T>.IsAddingCompleted itself throws when the collection is already disposed — and that check sat outside the try-catch in WriteMessage.

The race: Shutdown() disposes the logger, then the elevated server process exit fires Process.Exited asynchronously on a threadpool thread (InstallClientElevationContext.ServerExitedLogMessageWriteMessage), hitting the disposed BlockingCollection.

Change

  • TimestampedFileLogger.WriteMessage: expand the try-catch to also cover the IsAddingCompleted property access, not just Add().
// Before — IsAddingCompleted outside try, throws ObjectDisposedException unhandled
if (!_messageQueue.IsAddingCompleted)
{
    try { _messageQueue.Add(message); }
    catch (ObjectDisposedException) { }
    catch (InvalidOperationException) { }
}

// After — entire guard + add is protected
try
{
    if (!_messageQueue.IsAddingCompleted)
        _messageQueue.Add(message);
}
catch (ObjectDisposedException) { }
catch (InvalidOperationException) { }

Copilot AI changed the title [WIP] Fix unhandled exception during android wasm-tools repair Fix unhandled ObjectDisposedException in TimestampedFileLogger during workload repair Apr 27, 2026
Copilot AI requested a review from marcpopMSFT April 27, 2026 16:08
@marcpopMSFT marcpopMSFT marked this pull request as ready for review May 1, 2026 22:27
Copilot AI review requested due to automatic review settings May 1, 2026 22:27
Copy link
Copy Markdown
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 a shutdown race in the Windows workload repair logging path where TimestampedFileLogger.WriteMessage could throw an unhandled ObjectDisposedException after the logger was disposed.

Changes:

  • Wrapes the BlockingCollection.IsAddingCompleted check in the existing exception handling so disposal races no longer crash the process.

Comment on lines +177 to 191
try
{
try
if (!_messageQueue.IsAddingCompleted)
{
_messageQueue.Add(message);
}
catch (ObjectDisposedException)
{
// The logger was disposed between the IsAddingCompleted check and Add.
}
catch (InvalidOperationException)
{
// CompleteAdding was called between the IsAddingCompleted check and Add.
}
}
catch (ObjectDisposedException)
{
// The logger was disposed before or between the IsAddingCompleted check and Add.
}
catch (InvalidOperationException)
{
// CompleteAdding was called between the IsAddingCompleted check and Add.
}
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

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

Consider adding a regression test that calls LogMessage after TimestampedFileLogger.Dispose() (or races Dispose() with a thread calling LogMessage) to ensure the ObjectDisposedException/InvalidOperationException paths here remain non-fatal. This scenario is easy to exercise in WindowsInstallerTests, and it would prevent this shutdown race from reappearing.

Copilot uses AI. Check for mistakes.
@marcpopMSFT marcpopMSFT force-pushed the copilot/fix-unhandled-exception-wasm-tools-repair branch 2 times, most recently from d448959 to 0ec020b Compare May 11, 2026 23:38
…mpedFileLogger.WriteMessage

Agent-Logs-Url: https://github.com/dotnet/sdk/sessions/f6f3e405-fa5d-4de8-9621-887154b8609d

Co-authored-by: marcpopMSFT <12663534+marcpopMSFT@users.noreply.github.com>
@marcpopMSFT marcpopMSFT force-pushed the copilot/fix-unhandled-exception-wasm-tools-repair branch from 0ec020b to b7570b2 Compare May 12, 2026 23:57
@marcpopMSFT marcpopMSFT requested a review from nagilson May 12, 2026 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants