Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -510,15 +510,16 @@ private ValueTask SendFrameLockAcquiredNonCancelableAsync(MessageOpcode opcode,
if (writeTask.IsCompleted)
{
writeTask.GetAwaiter().GetResult();
ValueTask flushTask = new ValueTask(_stream.FlushAsync());
Task flushTask = _stream.FlushAsync();
if (flushTask.IsCompleted)
{
return flushTask;
flushTask.GetAwaiter().GetResult();
return ValueTask.CompletedTask;
}
else
{
releaseSendBufferAndSemaphore = false;
return WaitForWriteTaskAsync(flushTask, shouldFlush: false);
return WaitForWriteTaskAsync(new ValueTask(flushTask), shouldFlush: false);
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/libraries/System.Net.WebSockets/tests/WebSocketTestStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public Span<byte> NextAvailableBytes
/// </summary>
public bool IgnoreCancellationToken { get; set; }

/// <summary>
/// If set, causes FlushAsync to return a synchronously-faulted Task with this exception.
/// Used to exercise sync-completion-faulted code paths in the WebSocket send flow.
/// </summary>
public Exception? FlushException { get; set; }

public override bool CanRead => true;

public override bool CanSeek => false;
Expand Down Expand Up @@ -226,6 +232,15 @@ public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, Cancella

public override void Flush() { }

public override Task FlushAsync(CancellationToken cancellationToken)
{
if (FlushException is not null)
{
return Task.FromException(FlushException);
}
return base.FlushAsync(cancellationToken);
}

public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();

public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
Expand Down
17 changes: 17 additions & 0 deletions src/libraries/System.Net.WebSockets/tests/WebSocketTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ public async Task ThrowWhenContinuationWithDifferentCompressionFlags()
client.SendAsync(Memory<byte>.Empty, WebSocketMessageType.Binary, WebSocketMessageFlags.EndOfMessage, default));
}

[Fact]
public async Task SendAsync_FlushAsyncSyncFaulted_WrapsExceptionInWebSocketException()
{
var underlying = new IOException("flush failed");
using var stream = new WebSocketTestStream { FlushException = underlying };
using WebSocket ws = WebSocket.CreateFromStream(
stream, isServer: false, subProtocol: null, keepAliveInterval: Timeout.InfiniteTimeSpan);

var buffer = new ArraySegment<byte>(new byte[] { 1, 2, 3 });

WebSocketException ex = await Assert.ThrowsAsync<WebSocketException>(
() => ws.SendAsync(buffer, WebSocketMessageType.Binary, endOfMessage: true, CancellationToken.None));

Assert.Equal(WebSocketError.ConnectionClosedPrematurely, ex.WebSocketErrorCode);
Assert.Same(underlying, ex.InnerException);
}

[Fact]
public async Task ReceiveAsync_ServerUnmaskedFrame_ThrowsWebSocketException()
{
Expand Down
Loading