diff --git a/build/Dependencies.System2.props b/build/Dependencies.System2.props index b49238c7..d954bb15 100644 --- a/build/Dependencies.System2.props +++ b/build/Dependencies.System2.props @@ -95,7 +95,7 @@ 4.5.0 4.7.1 - 4.6.0 + 4.7.2 4.5.0 4.9.0 4.5.3 diff --git a/build/Dependencies.System3.props b/build/Dependencies.System3.props index 0013b7ca..040ef63a 100644 --- a/build/Dependencies.System3.props +++ b/build/Dependencies.System3.props @@ -98,7 +98,7 @@ 4.7.1 4.7.2 - 4.7.1 + 4.7.2 4.7.1 4.11.1 4.5.4 diff --git a/src/DotNetty.Handlers/Tls/TlsHandler.Handshake.cs b/src/DotNetty.Handlers/Tls/TlsHandler.Handshake.cs index 666885c2..180fcf8d 100644 --- a/src/DotNetty.Handlers/Tls/TlsHandler.Handshake.cs +++ b/src/DotNetty.Handlers/Tls/TlsHandler.Handshake.cs @@ -43,6 +43,7 @@ namespace DotNetty.Handlers.Tls partial class TlsHandler { private static readonly Action s_handshakeCompletionCallback = (t, s) => HandleHandshakeCompleted((Task)t, (TlsHandler)s); + private static readonly TaskCanceledException s_taskCanceledException = new TaskCanceledException(); public static readonly AttributeKey SslStreamAttrKey = AttributeKey.ValueOf("SSLSTREAM"); private bool EnsureAuthenticated(IChannelHandlerContext ctx) @@ -212,10 +213,10 @@ private static void HandleHandshakeCompleted(Task task, TlsHandler self) Debug.Assert(!oldState.HasAny(TlsHandlerState.Authenticated)); self.State = (oldState | TlsHandlerState.FailedAuthentication) & ~TlsHandlerState.Authenticating; var taskExc = task.Exception; - var cause = taskExc.Unwrap(); + var cause = task.IsFaulted ? taskExc.Unwrap() : s_taskCanceledException; try { - if (self._handshakePromise.TrySetException(taskExc)) + if (task.IsFaulted ? self._handshakePromise.TrySetException(taskExc) : self._handshakePromise.TrySetCanceled()) { TlsUtils.NotifyHandshakeFailure(capturedContext, cause, true); } diff --git a/src/DotNetty.Handlers/Tls/TlsHandler.MediationStream.NetCore.cs b/src/DotNetty.Handlers/Tls/TlsHandler.MediationStream.NetCore.cs index 7bf2f59b..ada9d6e6 100644 --- a/src/DotNetty.Handlers/Tls/TlsHandler.MediationStream.NetCore.cs +++ b/src/DotNetty.Handlers/Tls/TlsHandler.MediationStream.NetCore.cs @@ -177,18 +177,18 @@ private int ReadFromInput(Memory destination) // byte[] destination, int d } public override void Write(ReadOnlySpan buffer) - => _owner.FinishWrap(buffer, _owner._lastContextWritePromise); + => _owner.FinishWrap(buffer, _owner._lastContextWritePromise ?? _owner.CapturedContext.VoidPromise()); public override void Write(byte[] buffer, int offset, int count) - => _owner.FinishWrap(buffer, offset, count, _owner._lastContextWritePromise); + => _owner.FinishWrap(buffer, offset, count, _owner._lastContextWritePromise ?? _owner.CapturedContext.VoidPromise()); public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) { - return new ValueTask(_owner.FinishWrapNonAppDataAsync(buffer, _owner.CapturedContext.NewPromise())); + return new ValueTask(_owner.FinishWrapNonAppDataAsync(buffer, _owner._lastContextWritePromise ?? _owner.CapturedContext.NewPromise())); } public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) - => _owner.FinishWrapNonAppDataAsync(buffer, offset, count, _owner.CapturedContext.NewPromise()); + => _owner.FinishWrapNonAppDataAsync(buffer, offset, count, _owner._lastContextWritePromise ?? _owner.CapturedContext.NewPromise()); } } } diff --git a/src/DotNetty.Handlers/Tls/TlsHandler.MediationStream.NetFx.cs b/src/DotNetty.Handlers/Tls/TlsHandler.MediationStream.NetFx.cs index c6c81e6f..96f98b51 100644 --- a/src/DotNetty.Handlers/Tls/TlsHandler.MediationStream.NetFx.cs +++ b/src/DotNetty.Handlers/Tls/TlsHandler.MediationStream.NetFx.cs @@ -145,10 +145,10 @@ private int ReadFromInput(byte[] destination, int destinationOffset, int destina return length; } - public override void Write(byte[] buffer, int offset, int count) => _owner.FinishWrap(buffer, offset, count, _owner._lastContextWritePromise); + public override void Write(byte[] buffer, int offset, int count) => _owner.FinishWrap(buffer, offset, count, _owner._lastContextWritePromise ?? _owner.CapturedContext.VoidPromise()); public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) - => _owner.FinishWrapNonAppDataAsync(buffer, offset, count, _owner.CapturedContext.NewPromise()); + => _owner.FinishWrapNonAppDataAsync(buffer, offset, count, _owner._lastContextWritePromise ?? _owner.CapturedContext.NewPromise()); private static readonly Action s_writeCompleteCallback = (t, s) => HandleChannelWriteComplete(t, s); diff --git a/src/DotNetty.Handlers/Tls/TlsHandler.MediationStream.NetStandard20.cs b/src/DotNetty.Handlers/Tls/TlsHandler.MediationStream.NetStandard20.cs index 2889bf3f..688110d7 100644 --- a/src/DotNetty.Handlers/Tls/TlsHandler.MediationStream.NetStandard20.cs +++ b/src/DotNetty.Handlers/Tls/TlsHandler.MediationStream.NetStandard20.cs @@ -108,10 +108,10 @@ private int ReadFromInput(byte[] destination, int destinationOffset, int destina return length; } - public override void Write(byte[] buffer, int offset, int count) => _owner.FinishWrap(buffer, offset, count, _owner._lastContextWritePromise); + public override void Write(byte[] buffer, int offset, int count) => _owner.FinishWrap(buffer, offset, count, _owner._lastContextWritePromise ?? _owner.CapturedContext.NewPromise()); public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) - => _owner.FinishWrapNonAppDataAsync(buffer, offset, count, _owner.CapturedContext.NewPromise()); + => _owner.FinishWrapNonAppDataAsync(buffer, offset, count, _owner._lastContextWritePromise ?? _owner.CapturedContext.NewPromise()); } } } diff --git a/src/DotNetty.Handlers/Tls/TlsHandler.Writer.cs b/src/DotNetty.Handlers/Tls/TlsHandler.Writer.cs index 786a039f..df001e50 100644 --- a/src/DotNetty.Handlers/Tls/TlsHandler.Writer.cs +++ b/src/DotNetty.Handlers/Tls/TlsHandler.Writer.cs @@ -27,6 +27,7 @@ namespace DotNetty.Handlers.Tls using System.Runtime.CompilerServices; using System.Runtime.ExceptionServices; using System.Net.Security; + using System.Threading; using System.Threading.Tasks; using DotNetty.Buffers; using DotNetty.Common.Concurrency; @@ -257,6 +258,21 @@ private Task FinishWrapNonAppDataAsync(byte[] buffer, int offset, int count, IPr this.ReadIfNeeded(capturedContext); return future; } + +#if NETCOREAPP || NETSTANDARD_2_0_GREATER + private static async ValueTask LinkOutcome(ValueTask valueTask, IPromise promise) + { + try + { + await valueTask; + promise.TryComplete(); + } + catch (Exception ex) + { + promise.TrySetException(ex); + } + } +#endif [MethodImpl(MethodImplOptions.NoInlining)] private static InvalidOperationException NewPendingWritesNullException() diff --git a/src/DotNetty.Transport/Channels/AbstractCoalescingBufferQueue.cs b/src/DotNetty.Transport/Channels/AbstractCoalescingBufferQueue.cs index cdb8d53d..2d5f6973 100644 --- a/src/DotNetty.Transport/Channels/AbstractCoalescingBufferQueue.cs +++ b/src/DotNetty.Transport/Channels/AbstractCoalescingBufferQueue.cs @@ -202,7 +202,10 @@ public bool IsEmpty() /// public void ReleaseAndFailAll(Exception cause) { - ReleaseAndCompleteAll(TaskUtil.FromException(cause)); + var failedTask = TaskUtil.FromException(cause); + //if _bufAndListenerPairs queue is empty, the task will end up unobserved + failedTask.Ignore(); + ReleaseAndCompleteAll(failedTask); } /// diff --git a/test/DotNetty.Handlers.Proxy.Tests/ProxyHandlerTest.cs b/test/DotNetty.Handlers.Proxy.Tests/ProxyHandlerTest.cs index 73cee664..366f0cf7 100644 --- a/test/DotNetty.Handlers.Proxy.Tests/ProxyHandlerTest.cs +++ b/test/DotNetty.Handlers.Proxy.Tests/ProxyHandlerTest.cs @@ -123,10 +123,10 @@ public static List CreateTestItems() /* Note: Test keeps failing and Tom/Max agreed to skip it for now - new FailureTestItem( - "HTTP proxy: rejected anonymous connection", - DESTINATION, "status: 401", - new HttpProxyHandler(HttpProxy.Address)), + new FailureTestItem( + "HTTP proxy: rejected anonymous connection", + DESTINATION, "status: 401", + new HttpProxyHandler(HttpProxy.Address)), */ new SuccessTestItem( @@ -173,11 +173,11 @@ public static List CreateTestItems() /* Note: Test keeps failing and Tom/Max agreed to skip it for now - new FailureTestItem( - "Anonymous HTTPS proxy: rejected connection", - BAD_DESTINATION, "status: 403", - CreateClientTlsHandler(), - new HttpProxyHandler(AnonHttpsProxy.Address)), + new FailureTestItem( + "Anonymous HTTPS proxy: rejected connection", + BAD_DESTINATION, "status: 403", + CreateClientTlsHandler(), + new HttpProxyHandler(AnonHttpsProxy.Address)), */ new FailureTestItem(