Skip to content

Commit da6aa6f

Browse files
Sync shared code from runtime (#24845)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 93cfbe4 commit da6aa6f

File tree

4 files changed

+30
-26
lines changed

4 files changed

+30
-26
lines changed

src/Shared/runtime/Quic/Implementations/MsQuic/Internal/MsQuicSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public IntPtr ListenerOpen(QuicListenerOptions options)
5656

5757
QuicExceptionHelpers.ThrowIfFailed(MsQuicApi.Api.ListenerOpenDelegate(
5858
_nativeObjPtr,
59-
MsQuicListener.NativeCallbackHandler,
59+
MsQuicListener.s_listenerDelegate,
6060
IntPtr.Zero,
6161
out IntPtr listenerPointer),
6262
"Could not open listener.");

src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicConnection.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ internal sealed class MsQuicConnection : QuicConnectionProvider
3737
private SslApplicationProtocol _negotiatedAlpnProtocol;
3838

3939
// TODO: only allocate these when there is an outstanding connect/shutdown.
40-
private readonly TaskCompletionSource<uint> _connectTcs = new TaskCompletionSource<uint>();
41-
private readonly TaskCompletionSource<uint> _shutdownTcs = new TaskCompletionSource<uint>();
40+
private readonly TaskCompletionSource<uint> _connectTcs = new TaskCompletionSource<uint>(TaskCreationOptions.RunContinuationsAsynchronously);
41+
private readonly TaskCompletionSource<uint> _shutdownTcs = new TaskCompletionSource<uint>(TaskCreationOptions.RunContinuationsAsynchronously);
4242

4343
private bool _disposed;
4444
private bool _connected;
@@ -291,6 +291,8 @@ private ValueTask ShutdownAsync(
291291
ErrorCode);
292292
QuicExceptionHelpers.ThrowIfFailed(status, "Failed to shutdown connection.");
293293

294+
Debug.Assert(_shutdownTcs.Task.IsCompleted == false);
295+
294296
return new ValueTask(_shutdownTcs.Task);
295297
}
296298

@@ -349,6 +351,8 @@ private void Dispose(bool disposing)
349351
_disposed = true;
350352
}
351353

354+
// TODO: this appears abortive and will cause prior successfully shutdown and closed streams to drop data.
355+
// It's unclear how to gracefully wait for a connection to be 100% done.
352356
internal override ValueTask CloseAsync(long errorCode, CancellationToken cancellationToken = default)
353357
{
354358
ThrowIfDisposed();

src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicListener.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal sealed class MsQuicListener : QuicListenerProvider, IDisposable
2626
private GCHandle _handle;
2727

2828
// Delegate that wraps the static function that will be called when receiving an event.
29-
private static readonly ListenerCallbackDelegate s_listenerDelegate = new ListenerCallbackDelegate(NativeCallbackHandler);
29+
internal static readonly ListenerCallbackDelegate s_listenerDelegate = new ListenerCallbackDelegate(NativeCallbackHandler);
3030

3131
// Ssl listening options (ALPN, cert, etc)
3232
private readonly SslServerAuthenticationOptions _sslOptions;
@@ -189,7 +189,7 @@ private void StopAcceptingConnections()
189189
_acceptConnectionQueue.Writer.TryComplete();
190190
}
191191

192-
internal static uint NativeCallbackHandler(
192+
private static uint NativeCallbackHandler(
193193
IntPtr listener,
194194
IntPtr context,
195195
ref ListenerEvent connectionEventStruct)

src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicStream.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ internal sealed class MsQuicStream : QuicStreamProvider
3434
// Resettable completions to be used for multiple calls to receive.
3535
private readonly ResettableCompletionSource<uint> _receiveResettableCompletionSource;
3636

37-
private readonly ResettableCompletionSource<uint> _shutdownWriteResettableCompletionSource;
37+
// Set once writes have been shutdown.
38+
private readonly TaskCompletionSource _shutdownWriteCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
3839

3940
// Buffers to hold during a call to send.
4041
private MemoryHandle[] _bufferArrays = new MemoryHandle[1];
@@ -49,7 +50,7 @@ internal sealed class MsQuicStream : QuicStreamProvider
4950
private ReadState _readState;
5051
private long _readErrorCode = -1;
5152

52-
private ShutdownWriteState _shutdownState;
53+
private ShutdownWriteState _shutdownWriteState;
5354

5455
private SendState _sendState;
5556
private long _sendErrorCode = -1;
@@ -76,7 +77,6 @@ internal MsQuicStream(MsQuicConnection connection, QUIC_STREAM_OPEN_FLAG flags,
7677

7778
_sendResettableCompletionSource = new ResettableCompletionSource<uint>();
7879
_receiveResettableCompletionSource = new ResettableCompletionSource<uint>();
79-
_shutdownWriteResettableCompletionSource = new ResettableCompletionSource<uint>();
8080
SetCallbackHandler();
8181

8282
bool isBidirectional = !flags.HasFlag(QUIC_STREAM_OPEN_FLAG.UNIDIRECTIONAL);
@@ -323,16 +323,16 @@ internal override void AbortWrite(long errorCode)
323323

324324
lock (_sync)
325325
{
326-
if (_shutdownState == ShutdownWriteState.None)
326+
if (_shutdownWriteState == ShutdownWriteState.None)
327327
{
328-
_shutdownState = ShutdownWriteState.Canceled;
328+
_shutdownWriteState = ShutdownWriteState.Canceled;
329329
shouldComplete = true;
330330
}
331331
}
332332

333333
if (shouldComplete)
334334
{
335-
_shutdownWriteResettableCompletionSource.CompleteException(new QuicStreamAbortedException("Shutdown was aborted.", errorCode));
335+
_shutdownWriteCompletionSource.SetException(new QuicStreamAbortedException("Shutdown was aborted.", errorCode));
336336
}
337337

338338
MsQuicApi.Api.StreamShutdownDelegate(_ptr, (uint)QUIC_STREAM_SHUTDOWN_FLAG.ABORT_SEND, errorCode);
@@ -348,20 +348,20 @@ internal override ValueTask ShutdownWriteCompleted(CancellationToken cancellatio
348348
bool shouldComplete = false;
349349
lock (_sync)
350350
{
351-
if (_shutdownState == ShutdownWriteState.None)
351+
if (_shutdownWriteState == ShutdownWriteState.None)
352352
{
353-
_shutdownState = ShutdownWriteState.Canceled;
353+
_shutdownWriteState = ShutdownWriteState.Canceled;
354354
shouldComplete = true;
355355
}
356356
}
357357

358358
if (shouldComplete)
359359
{
360-
_shutdownWriteResettableCompletionSource.CompleteException(new OperationCanceledException("Shutdown was canceled", cancellationToken));
360+
_shutdownWriteCompletionSource.SetException(new OperationCanceledException("Shutdown was canceled", cancellationToken));
361361
}
362362
});
363363

364-
return _shutdownWriteResettableCompletionSource.GetTypelessValueTask();
364+
return new ValueTask(_shutdownWriteCompletionSource.Task);
365365
}
366366

367367
internal override void Shutdown()
@@ -408,8 +408,6 @@ public override ValueTask DisposeAsync()
408408
return default;
409409
}
410410

411-
CleanupSendState();
412-
413411
if (_ptr != IntPtr.Zero)
414412
{
415413
// TODO resolve graceful vs abortive dispose here. Will file a separate issue.
@@ -419,6 +417,8 @@ public override ValueTask DisposeAsync()
419417

420418
_handle.Free();
421419

420+
CleanupSendState();
421+
422422
_disposed = true;
423423

424424
return default;
@@ -442,8 +442,6 @@ private void Dispose(bool disposing)
442442
return;
443443
}
444444

445-
CleanupSendState();
446-
447445
if (_ptr != IntPtr.Zero)
448446
{
449447
// TODO resolve graceful vs abortive dispose here. Will file a separate issue.
@@ -453,6 +451,8 @@ private void Dispose(bool disposing)
453451

454452
_handle.Free();
455453

454+
CleanupSendState();
455+
456456
_disposed = true;
457457
}
458458

@@ -461,7 +461,7 @@ private void EnableReceive()
461461
MsQuicApi.Api.StreamReceiveSetEnabledDelegate(_ptr, enabled: true);
462462
}
463463

464-
internal static uint NativeCallbackHandler(
464+
private static uint NativeCallbackHandler(
465465
IntPtr stream,
466466
IntPtr context,
467467
ref StreamEvent streamEvent)
@@ -614,16 +614,16 @@ private uint HandleEventSendShutdownComplete(ref MsQuicNativeMethods.StreamEvent
614614
bool shouldComplete = false;
615615
lock (_sync)
616616
{
617-
if (_shutdownState == ShutdownWriteState.None)
617+
if (_shutdownWriteState == ShutdownWriteState.None)
618618
{
619-
_shutdownState = ShutdownWriteState.Finished;
619+
_shutdownWriteState = ShutdownWriteState.Finished;
620620
shouldComplete = true;
621621
}
622622
}
623623

624624
if (shouldComplete)
625625
{
626-
_shutdownWriteResettableCompletionSource.Complete(MsQuicStatusCodes.Success);
626+
_shutdownWriteCompletionSource.TrySetResult();
627627
}
628628

629629
return MsQuicStatusCodes.Success;
@@ -646,9 +646,9 @@ private uint HandleEventShutdownComplete()
646646

647647
_readState = ReadState.ReadsCompleted;
648648

649-
if (_shutdownState == ShutdownWriteState.None)
649+
if (_shutdownWriteState == ShutdownWriteState.None)
650650
{
651-
_shutdownState = ShutdownWriteState.Finished;
651+
_shutdownWriteState = ShutdownWriteState.Finished;
652652
shouldShutdownWriteComplete = true;
653653
}
654654
}
@@ -660,7 +660,7 @@ private uint HandleEventShutdownComplete()
660660

661661
if (shouldShutdownWriteComplete)
662662
{
663-
_shutdownWriteResettableCompletionSource.Complete(MsQuicStatusCodes.Success);
663+
_shutdownWriteCompletionSource.TrySetResult();
664664
}
665665

666666
return MsQuicStatusCodes.Success;

0 commit comments

Comments
 (0)