Skip to content

Commit 21778f6

Browse files
committed
Guarding against more read_cb cases
1 parent 9c1cb29 commit 21778f6

File tree

4 files changed

+43
-31
lines changed

4 files changed

+43
-31
lines changed

KestrelHttpServer.sln

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SampleApp", "samples\Sample
1919
EndProject
2020
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Kestrel", "src\Kestrel\Kestrel.kproj", "{30B7617E-58EF-4382-B3EA-5B2E718CF1A6}"
2121
EndProject
22-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Mvc.Modules", "..\Entropy\samples\Mvc.Modules\Mvc.Modules.kproj", "{181C52C4-E916-416E-96BA-2B645841807F}"
23-
EndProject
24-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.RequestContainer", "..\Hosting\src\Microsoft.AspNet.RequestContainer\Microsoft.AspNet.RequestContainer.kproj", "{374A5B0C-3E93-4A23-A4A0-EE2AB6DF7814}"
25-
EndProject
26-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Hosting", "..\Hosting\src\Microsoft.AspNet.Hosting\Microsoft.AspNet.Hosting.kproj", "{3944F036-7E75-47E8-AA52-C4B89A64EC3A}"
27-
EndProject
2822
Global
2923
GlobalSection(Performance) = preSolution
3024
HasPerformanceSessions = true
@@ -50,18 +44,6 @@ Global
5044
{30B7617E-58EF-4382-B3EA-5B2E718CF1A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
5145
{30B7617E-58EF-4382-B3EA-5B2E718CF1A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
5246
{30B7617E-58EF-4382-B3EA-5B2E718CF1A6}.Release|Any CPU.Build.0 = Release|Any CPU
53-
{181C52C4-E916-416E-96BA-2B645841807F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
54-
{181C52C4-E916-416E-96BA-2B645841807F}.Debug|Any CPU.Build.0 = Debug|Any CPU
55-
{181C52C4-E916-416E-96BA-2B645841807F}.Release|Any CPU.ActiveCfg = Release|Any CPU
56-
{181C52C4-E916-416E-96BA-2B645841807F}.Release|Any CPU.Build.0 = Release|Any CPU
57-
{374A5B0C-3E93-4A23-A4A0-EE2AB6DF7814}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
58-
{374A5B0C-3E93-4A23-A4A0-EE2AB6DF7814}.Debug|Any CPU.Build.0 = Debug|Any CPU
59-
{374A5B0C-3E93-4A23-A4A0-EE2AB6DF7814}.Release|Any CPU.ActiveCfg = Release|Any CPU
60-
{374A5B0C-3E93-4A23-A4A0-EE2AB6DF7814}.Release|Any CPU.Build.0 = Release|Any CPU
61-
{3944F036-7E75-47E8-AA52-C4B89A64EC3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
62-
{3944F036-7E75-47E8-AA52-C4B89A64EC3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
63-
{3944F036-7E75-47E8-AA52-C4B89A64EC3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
64-
{3944F036-7E75-47E8-AA52-C4B89A64EC3A}.Release|Any CPU.Build.0 = Release|Any CPU
6547
EndGlobalSection
6648
GlobalSection(SolutionProperties) = preSolution
6749
HideSolutionNode = FALSE

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,43 @@ public void Start()
7676
private Libuv.uv_buf_t OnAlloc(UvStreamHandle handle, int suggestedSize)
7777
{
7878
return handle.Libuv.buf_init(
79-
SocketInput.Pin(2048),
79+
SocketInput.Pin(2048),
8080
2048);
8181
}
8282

83-
private void OnRead(UvStreamHandle handle, int nread, Exception error)
83+
private void OnRead(UvStreamHandle handle, int status, Exception error)
8484
{
85-
SocketInput.Unpin(nread);
85+
SocketInput.Unpin(status);
8686

87-
if (nread == 0 || error != null)
87+
var normalRead = error == null && status > 0;
88+
var normalDone = status == 0 || status == -4077 || status == -4095;
89+
var errorDone = !(normalDone || normalRead);
90+
91+
if (normalRead)
92+
{
93+
KestrelTrace.Log.ConnectionRead(_connectionId, status);
94+
}
95+
else if (normalDone || errorDone)
8896
{
89-
SocketInput.RemoteIntakeFin = true;
9097
KestrelTrace.Log.ConnectionReadFin(_connectionId);
9198

92-
if (error != null)
99+
SocketInput.RemoteIntakeFin = true;
100+
101+
if (errorDone && error != null)
93102
{
94103
Trace.WriteLine("Connection.OnRead " + error.ToString());
95104
}
96105
}
97-
else
106+
107+
108+
try
98109
{
99-
KestrelTrace.Log.ConnectionRead(_connectionId, nread);
110+
_frame.Consume();
111+
}
112+
catch (Exception ex)
113+
{
114+
Trace.WriteLine("Connection._frame.Consume " + ex.ToString());
100115
}
101-
102-
_frame.Consume();
103116
}
104117

105118
void IConnectionControl.Pause()

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,24 @@ public void Extend(int count)
9090

9191
Buffer = new ArraySegment<byte>(Buffer.Array, Buffer.Offset, Buffer.Count + count);
9292
}
93+
9394
public IntPtr Pin(int minimumSize)
9495
{
9596
var segment = Available(minimumSize);
9697
_gcHandle = GCHandle.Alloc(segment.Array, GCHandleType.Pinned);
9798
return _gcHandle.AddrOfPinnedObject() + segment.Offset;
9899
}
100+
99101
public void Unpin(int count)
100102
{
101-
_gcHandle.Free();
102-
Extend(count);
103+
// read_cb may called without an earlier alloc_cb
104+
// this does not need to be thread-safe
105+
// IsAllocated is checked only because Unpin can be called redundantly
106+
if (_gcHandle.IsAllocated)
107+
{
108+
_gcHandle.Free();
109+
Extend(count);
110+
}
103111
}
104112

105113
}

src/Microsoft.AspNet.Server.Kestrel/project.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@
44
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-*"
55
},
66
"configurations": {
7-
"net45": { },
7+
"net45": {
8+
"compilationOptions": {
9+
"define": [ "TRACE", "NET45" ],
10+
"allowUnsafe": true
11+
}
12+
},
813
"k10": {
14+
"compilationOptions": {
15+
"define": [ "TRACE", "K10" ],
16+
"allowUnsafe": true
17+
},
918
"dependencies": {
1019
"System.Threading.ThreadPool": "4.0.10.0",
1120
"System.Diagnostics.Debug": "4.0.10.0",

0 commit comments

Comments
 (0)