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

Commit 3f9628f

Browse files
author
Kai Ruhnau
committed
Provide Write errors to upper layers
1 parent 65e87b7 commit 3f9628f

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,22 @@ public SocketOutput(KestrelThread thread, UvTcpStreamHandle socket)
2727
_socket = socket;
2828
}
2929

30-
public Task WriteAsync(ArraySegment<byte> buffer)
30+
public async Task WriteAsync(ArraySegment<byte> buffer)
3131
{
3232
//TODO: need buffering that works
3333
var copy = new byte[buffer.Count];
3434
Array.Copy(buffer.Array, buffer.Offset, copy, 0, buffer.Count);
3535
var arraySegment = new ArraySegment<byte>(copy);
3636

3737
KestrelTrace.Log.ConnectionWrite(0, buffer.Count);
38-
var req = new UvWriteReq(
38+
using (var req = new UvWriteReq(
3939
_thread.Loop,
4040
_socket,
41-
arraySegment);
42-
return _thread.PostAsync(req.Write);
41+
arraySegment))
42+
{
43+
await _thread.PostAsync(req.Write);
44+
await req.Task;
45+
}
4346
}
4447

4548
public bool Flush(Action drained)

src/Microsoft.AspNet.Server.Kestrel/Networking/UvWriteRequest.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Runtime.InteropServices;
7+
using System.Threading.Tasks;
78

89
namespace Microsoft.AspNet.Server.Kestrel.Networking
910
{
@@ -20,6 +21,8 @@ public class UvWriteReq : UvMemoryResource
2021
private readonly GCHandle[] _bufferHandles;
2122
private readonly GCHandle _bufferArrayHandle;
2223

24+
private readonly TaskCompletionSource<int> _tcs;
25+
2326
public UvWriteReq(
2427
UvLoopHandle loop,
2528
UvTcpStreamHandle stream,
@@ -37,43 +40,38 @@ public UvWriteReq(
3740
_uvBuffers[0] = new UvBuffer(
3841
_bufferHandles[0].AddrOfPinnedObject() + buffer.Offset,
3942
buffer.Count);
43+
44+
_tcs = new TaskCompletionSource<int>();
4045
}
4146

4247
private static int GetSize()
4348
{
4449
return UnsafeNativeMethods.uv_req_size(RequestType.WRITE);
4550
}
4651

52+
public Task Task => _tcs.Task;
53+
4754
public void Write()
4855
{
49-
try
50-
{
51-
_stream.Validate();
52-
Validate();
53-
Libuv.ThrowOnError(UnsafeNativeMethods.uv_write(
54-
this,
55-
_stream.Handle,
56-
_uvBuffers,
57-
_uvBuffers.Length,
58-
_uv_write_cb));
59-
}
60-
catch
61-
{
62-
Dispose();
63-
throw;
64-
}
56+
_stream.Validate();
57+
Validate();
58+
Libuv.ThrowOnError(UnsafeNativeMethods.uv_write(
59+
this,
60+
_stream.Handle,
61+
_uvBuffers,
62+
_uvBuffers.Length,
63+
_uv_write_cb));
6564
}
6665

6766
private void UvWriteCb(IntPtr ptr, int status)
6867
{
6968
KestrelTrace.Log.ConnectionWriteCallback(0, status);
7069

71-
Dispose();
72-
73-
if (status == -125 || status == -4081) // UV_ECANCELED on Unix and Windows
74-
return;
70+
var exception = Libuv.ExceptionForError(status);
71+
if (exception == null)
72+
_tcs.SetResult(0);
7573
else
76-
Libuv.ThrowOnError(status);
74+
_tcs.SetException(exception);
7775
}
7876

7977
protected override bool ReleaseHandle()

0 commit comments

Comments
 (0)