From 34fcf38b7f8fcb4d356d10ab138d5cdb5cc94d27 Mon Sep 17 00:00:00 2001 From: Geoffrey Kizer Date: Wed, 14 Jul 2021 19:21:02 -0700 Subject: [PATCH] remove QuicStream.ShutdownWriteCompleted API and associated implementation logic --- .../System.Net.Quic/ref/System.Net.Quic.cs | 1 - .../Quic/Implementations/Mock/MockStream.cs | 8 -- .../Implementations/MsQuic/MsQuicStream.cs | 112 +----------------- .../Implementations/QuicStreamProvider.cs | 2 - .../src/System/Net/Quic/QuicStream.cs | 2 - 5 files changed, 2 insertions(+), 123 deletions(-) diff --git a/src/libraries/System.Net.Quic/ref/System.Net.Quic.cs b/src/libraries/System.Net.Quic/ref/System.Net.Quic.cs index be6df9a7c479b..28d4332466101 100644 --- a/src/libraries/System.Net.Quic/ref/System.Net.Quic.cs +++ b/src/libraries/System.Net.Quic/ref/System.Net.Quic.cs @@ -104,7 +104,6 @@ public override void Flush() { } public override void SetLength(long value) { } public void Shutdown() { } public System.Threading.Tasks.ValueTask ShutdownCompleted(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public System.Threading.Tasks.ValueTask ShutdownWriteCompleted(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public override void Write(byte[] buffer, int offset, int count) { } public override void Write(System.ReadOnlySpan buffer) { } public System.Threading.Tasks.ValueTask WriteAsync(System.Buffers.ReadOnlySequence buffers, bool endStream, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } diff --git a/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/Mock/MockStream.cs b/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/Mock/MockStream.cs index 1d09b6331746b..313fc1eb6b691 100644 --- a/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/Mock/MockStream.cs +++ b/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/Mock/MockStream.cs @@ -203,14 +203,6 @@ internal override void AbortWrite(long errorCode) WriteStreamBuffer?.EndWrite(); } - internal override ValueTask ShutdownWriteCompleted(CancellationToken cancellationToken = default) - { - CheckDisposed(); - - return default; - } - - internal override ValueTask ShutdownCompleted(CancellationToken cancellationToken = default) { CheckDisposed(); diff --git a/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/MsQuic/MsQuicStream.cs b/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/MsQuic/MsQuicStream.cs index 0c29a9285d675..16e438408502a 100644 --- a/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/MsQuic/MsQuicStream.cs +++ b/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/MsQuic/MsQuicStream.cs @@ -65,11 +65,6 @@ private sealed class State // Resettable completions to be used for multiple calls to send. public readonly ResettableCompletionSource SendResettableCompletionSource = new ResettableCompletionSource(); - public ShutdownWriteState ShutdownWriteState; - - // Set once writes have been shutdown. - public readonly TaskCompletionSource ShutdownWriteCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - public ShutdownState ShutdownState; public int ShutdownDone; @@ -524,26 +519,12 @@ internal override void AbortWrite(long errorCode) { ThrowIfDisposed(); - bool shouldComplete = false; - lock (_state) { if (_state.SendState < SendState.Aborted) { _state.SendState = SendState.Aborted; } - - if (_state.ShutdownWriteState == ShutdownWriteState.None) - { - _state.ShutdownWriteState = ShutdownWriteState.Canceled; - shouldComplete = true; - } - } - - if (shouldComplete) - { - _state.ShutdownWriteCompletionSource.SetException( - ExceptionDispatchInfo.SetCurrentStackTrace(new QuicStreamAbortedException("Shutdown was aborted.", errorCode))); } StartShutdown(QUIC_STREAM_SHUTDOWN_FLAGS.ABORT_SEND, errorCode); @@ -555,42 +536,6 @@ private void StartShutdown(QUIC_STREAM_SHUTDOWN_FLAGS flags, long errorCode) QuicExceptionHelpers.ThrowIfFailed(status, "StreamShutdown failed."); } - internal override async ValueTask ShutdownWriteCompleted(CancellationToken cancellationToken = default) - { - ThrowIfDisposed(); - - lock (_state) - { - if (_state.ShutdownWriteState == ShutdownWriteState.ConnectionClosed) - { - throw GetConnectionAbortedException(_state); - } - } - - // TODO do anything to stop writes? - using CancellationTokenRegistration registration = cancellationToken.UnsafeRegister(static (s, token) => - { - var state = (State)s!; - bool shouldComplete = false; - lock (state) - { - if (state.ShutdownWriteState == ShutdownWriteState.None) - { - state.ShutdownWriteState = ShutdownWriteState.Canceled; // TODO: should we separate states for cancelling here vs calling Abort? - shouldComplete = true; - } - } - - if (shouldComplete) - { - state.ShutdownWriteCompletionSource.SetException( - ExceptionDispatchInfo.SetCurrentStackTrace(new OperationCanceledException("Wait for shutdown write was canceled", token))); - } - }, _state); - - await _state.ShutdownWriteCompletionSource.Task.ConfigureAwait(false); - } - internal override async ValueTask ShutdownCompleted(CancellationToken cancellationToken = default) { ThrowIfDisposed(); @@ -607,21 +552,16 @@ internal override async ValueTask ShutdownCompleted(CancellationToken cancellati using CancellationTokenRegistration registration = cancellationToken.UnsafeRegister(static (s, token) => { var state = (State)s!; - bool shouldComplete = false; lock (state) { if (state.ShutdownState == ShutdownState.None) { state.ShutdownState = ShutdownState.Canceled; - shouldComplete = true; } } - if (shouldComplete) - { - state.ShutdownWriteCompletionSource.SetException( - ExceptionDispatchInfo.SetCurrentStackTrace(new OperationCanceledException("Wait for shutdown was canceled", token))); - } + // TODO: Don't we need to cancel the waiter here? + }, _state); await _state.ShutdownCompletionSource.Task.ConfigureAwait(false); @@ -954,21 +894,6 @@ private static uint HandleEventStartComplete(State state, ref StreamEvent evt) private static uint HandleEventSendShutdownComplete(State state, ref StreamEvent evt) { - bool shouldComplete = false; - lock (state) - { - if (state.ShutdownWriteState == ShutdownWriteState.None) - { - state.ShutdownWriteState = ShutdownWriteState.Finished; - shouldComplete = true; - } - } - - if (shouldComplete) - { - state.ShutdownWriteCompletionSource.SetResult(); - } - return MsQuicStatusCodes.Success; } @@ -982,7 +907,6 @@ private static uint HandleEventShutdownComplete(State state, ref StreamEvent evt } bool shouldReadComplete = false; - bool shouldShutdownWriteComplete = false; bool shouldShutdownComplete = false; lock (state) @@ -1001,12 +925,6 @@ private static uint HandleEventShutdownComplete(State state, ref StreamEvent evt state.ReadState = ReadState.ReadsCompleted; } - if (state.ShutdownWriteState == ShutdownWriteState.None) - { - state.ShutdownWriteState = ShutdownWriteState.Finished; - shouldShutdownWriteComplete = true; - } - if (state.ShutdownState == ShutdownState.None) { state.ShutdownState = ShutdownState.Finished; @@ -1019,11 +937,6 @@ private static uint HandleEventShutdownComplete(State state, ref StreamEvent evt state.ReceiveResettableCompletionSource.Complete(0); } - if (shouldShutdownWriteComplete) - { - state.ShutdownWriteCompletionSource.SetResult(); - } - if (shouldShutdownComplete) { state.ShutdownCompletionSource.SetResult(); @@ -1367,7 +1280,6 @@ private static uint HandleEventConnectionClose(State state) bool shouldCompleteRead = false; bool shouldCompleteSend = false; - bool shouldCompleteShutdownWrite = false; bool shouldCompleteShutdown = false; lock (state) @@ -1384,12 +1296,6 @@ private static uint HandleEventConnectionClose(State state) } state.SendState = SendState.ConnectionClosed; - if (state.ShutdownWriteState == ShutdownWriteState.None) - { - shouldCompleteShutdownWrite = true; - } - state.ShutdownWriteState = ShutdownWriteState.ConnectionClosed; - if (state.ShutdownState == ShutdownState.None) { shouldCompleteShutdown = true; @@ -1409,12 +1315,6 @@ private static uint HandleEventConnectionClose(State state) ExceptionDispatchInfo.SetCurrentStackTrace(GetConnectionAbortedException(state))); } - if (shouldCompleteShutdownWrite) - { - state.ShutdownWriteCompletionSource.SetException( - ExceptionDispatchInfo.SetCurrentStackTrace(GetConnectionAbortedException(state))); - } - if (shouldCompleteShutdown) { state.ShutdownCompletionSource.SetException( @@ -1477,14 +1377,6 @@ private enum ReadState Closed } - private enum ShutdownWriteState - { - None = 0, - Canceled, - Finished, - ConnectionClosed - } - private enum ShutdownState { None = 0, diff --git a/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/QuicStreamProvider.cs b/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/QuicStreamProvider.cs index 2be277a61252a..a90bcf9bb89a3 100644 --- a/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/QuicStreamProvider.cs +++ b/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/QuicStreamProvider.cs @@ -37,8 +37,6 @@ internal abstract class QuicStreamProvider : IDisposable, IAsyncDisposable internal abstract ValueTask WriteAsync(ReadOnlyMemory> buffers, bool endStream, CancellationToken cancellationToken = default); - internal abstract ValueTask ShutdownWriteCompleted(CancellationToken cancellationToken = default); - internal abstract ValueTask ShutdownCompleted(CancellationToken cancellationToken = default); internal abstract void Shutdown(); diff --git a/src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.cs b/src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.cs index e1724eee53575..778a4cd1efa2d 100644 --- a/src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.cs +++ b/src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.cs @@ -99,8 +99,6 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati public ValueTask WriteAsync(ReadOnlyMemory> buffers, bool endStream, CancellationToken cancellationToken = default) => _provider.WriteAsync(buffers, endStream, cancellationToken); - public ValueTask ShutdownWriteCompleted(CancellationToken cancellationToken = default) => _provider.ShutdownWriteCompleted(cancellationToken); - public ValueTask ShutdownCompleted(CancellationToken cancellationToken = default) => _provider.ShutdownCompleted(cancellationToken); public void Shutdown() => _provider.Shutdown();