Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 89f4850

Browse files
authored
Avoid throwing the same exception instances in parallel (#2859)
1 parent 6dc55a0 commit 89f4850

File tree

7 files changed

+40
-45
lines changed

7 files changed

+40
-45
lines changed

src/Kestrel.Core/Internal/Http/HttpProtocol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ private void RejectNonBodyTransferEncodingResponse(bool appCompleted)
12121212
var ex = new InvalidOperationException(CoreStrings.FormatHeaderNotAllowedOnResponse("Transfer-Encoding", StatusCode));
12131213
if (!appCompleted)
12141214
{
1215-
// Back out of header creation surface exeception in user code
1215+
// Back out of header creation surface exception in user code
12161216
_requestProcessingStatus = RequestProcessingStatus.AppStarted;
12171217
throw ex;
12181218
}

src/Kestrel.Core/Internal/Infrastructure/ConnectionManagerShutdownExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ public static async Task<bool> CloseAllConnectionsAsync(this ConnectionManager c
2727
public static async Task<bool> AbortAllConnectionsAsync(this ConnectionManager connectionManager)
2828
{
2929
var abortTasks = new List<Task>();
30-
var canceledException = new ConnectionAbortedException(CoreStrings.ConnectionAbortedDuringServerShutdown);
3130

3231
connectionManager.Walk(connection =>
3332
{
34-
connection.TransportConnection.Abort(canceledException);
33+
connection.TransportConnection.Abort(new ConnectionAbortedException(CoreStrings.ConnectionAbortedDuringServerShutdown));
3534
abortTasks.Add(connection.ExecutionTask);
3635
});
3736

src/Kestrel.Core/Internal/Infrastructure/Streams.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure
1010
{
1111
public class Streams
1212
{
13-
private static readonly ThrowingWriteOnlyStream _throwingResponseStream
14-
= new ThrowingWriteOnlyStream(new InvalidOperationException(CoreStrings.ResponseStreamWasUpgraded));
13+
private static readonly ThrowingWasUpgradedWriteOnlyStream _throwingResponseStream
14+
= new ThrowingWasUpgradedWriteOnlyStream();
1515
private readonly HttpResponseStream _response;
1616
private readonly HttpRequestStream _request;
1717
private readonly WrappingStream _upgradeableResponse;

src/Kestrel.Core/Internal/Infrastructure/ThrowingWriteOnlyStream.cs renamed to src/Kestrel.Core/Internal/Infrastructure/ThrowingWasUpgradedWriteOnlyStream.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@
88

99
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure
1010
{
11-
public class ThrowingWriteOnlyStream : WriteOnlyStream
11+
public class ThrowingWasUpgradedWriteOnlyStream : WriteOnlyStream
1212
{
13-
private readonly Exception _exception;
14-
15-
public ThrowingWriteOnlyStream(Exception exception)
16-
{
17-
_exception = exception;
18-
}
19-
2013
public override bool CanSeek => false;
2114

2215
public override long Length => throw new NotSupportedException();
@@ -28,13 +21,13 @@ public override long Position
2821
}
2922

3023
public override void Write(byte[] buffer, int offset, int count)
31-
=> throw _exception;
24+
=> throw new InvalidOperationException(CoreStrings.ResponseStreamWasUpgraded);
3225

3326
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
34-
=> throw _exception;
27+
=> throw new InvalidOperationException(CoreStrings.ResponseStreamWasUpgraded);
3528

3629
public override void Flush()
37-
=> throw _exception;
30+
=> throw new InvalidOperationException(CoreStrings.ResponseStreamWasUpgraded);
3831

3932
public override long Seek(long offset, SeekOrigin origin)
4033
=> throw new NotSupportedException();

src/Kestrel.Transport.Libuv/Internal/LibuvConnection.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public async Task Start()
8888
}
8989
else
9090
{
91+
// This is unexpected.
92+
Log.ConnectionError(ConnectionId, ex);
93+
9194
inputError = ex;
9295
outputError = ex;
9396
}
@@ -240,6 +243,7 @@ private Exception LogAndWrapReadError(UvException uvError)
240243
}
241244
else
242245
{
246+
// This is unexpected.
243247
Log.ConnectionError(ConnectionId, uvError);
244248
return new IOException(uvError.Message, uvError);
245249
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
7+
using Xunit;
8+
9+
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
10+
{
11+
public class ThrowingWasUpgradedWriteOnlyStreamTests
12+
{
13+
[Fact]
14+
public async Task ThrowsOnWrite()
15+
{
16+
var stream = new ThrowingWasUpgradedWriteOnlyStream();
17+
18+
Assert.True(stream.CanWrite);
19+
Assert.False(stream.CanRead);
20+
Assert.False(stream.CanSeek);
21+
Assert.False(stream.CanTimeout);
22+
Assert.Equal(CoreStrings.ResponseStreamWasUpgraded, Assert.Throws<InvalidOperationException>(() => stream.Write(new byte[1], 0, 1)).Message);
23+
Assert.Equal(CoreStrings.ResponseStreamWasUpgraded, (await Assert.ThrowsAsync<InvalidOperationException>(() => stream.WriteAsync(new byte[1], 0, 1))).Message);
24+
Assert.Equal(CoreStrings.ResponseStreamWasUpgraded, Assert.Throws<InvalidOperationException>(() => stream.Flush()).Message);
25+
Assert.Equal(CoreStrings.ResponseStreamWasUpgraded, (await Assert.ThrowsAsync<InvalidOperationException>(() => stream.FlushAsync())).Message);
26+
}
27+
}
28+
}

test/Kestrel.Core.Tests/ThrowingWriteOnlyStreamTests.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)