Skip to content

UnobservedTaskException from QuicConnection that has not been disposed #112094

Closed
@WANDOKING

Description

@WANDOKING

Description

I am developing a simple echo server application using System.Net.Quic.

After closing QuicConnection, if I do not Dispose, the following log is observed intermittently.

2025-02-03 20:16:08.8433	FATAL	EchoBot.BotRunner	Unobserved Task Exception: System.Net.Quic.QuicException: Operation aborted.
   at System.Net.Quic.QuicConnection.HandleEventShutdownComplete()
   at System.Net.Quic.QuicConnection.HandleConnectionEvent(QUIC_CONNECTION_EVENT& connectionEvent)
   at System.Net.Quic.QuicConnection.NativeCallback(QUIC_HANDLE* connection, Void* context, QUIC_CONNECTION_EVENT* connectionEvent)
--- End of stack trace from previous location ---

After changing the code to call DisposeAsync instead of CloseAsync on QuicConnection, the problem seems to have disappeared, but I am reporting this because I think this is a problematic behavior.

I think it might have to do with the following code related to connectedTcs:

    private unsafe int HandleEventShutdownComplete()
    {
        // make sure we log at least some secrets in case of shutdown before handshake completes.
        _tlsSecret?.WriteSecret();
 
        Exception exception = ExceptionDispatchInfo.SetCurrentStackTrace(_disposed ? new ObjectDisposedException(GetType().FullName) : ThrowHelper.GetOperationAbortedException());
        _connectionCloseTcs.TrySetException(exception);
        _acceptQueue.Writer.TryComplete(exception);
        _connectedTcs.TrySetException(exception);
        _shutdownTokenSource.Cancel();
        _shutdownTcs.TrySetResult(final: true);
        return QUIC_STATUS_SUCCESS;
    }

Reproduction Steps

Open a bidirectional stream with QuicConnection on the client side, and have a Read loop in one thread and a Write loop in another thread.
Client intermittently closes the Connection.

Expected behavior

Internally handles QuicException.

Actual behavior

If I don't call Dispose(), an UnobservedTaskException occurs.

Regression?

No response

Known Workarounds

No response

Configuration

  • .NET version: 9.0.101
  • OS: Windows 11
  • Architecture: x64

Other information

Looks like it's related to #80111

Metadata

Metadata

Assignees

Labels

area-System.Net.Quicbugin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions