Skip to content

Commit 836be55

Browse files
committed
Using weak gchandles from native to managed
See #15
1 parent ca9e837 commit 836be55

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ private void OnRead(UvStreamHandle handle, int status, Exception error)
9595
else if (normalDone || errorDone)
9696
{
9797
KestrelTrace.Log.ConnectionReadFin(_connectionId);
98-
9998
SocketInput.RemoteIntakeFin = true;
99+
_socket.ReadStop();
100100

101101
if (errorDone && error != null)
102102
{

src/Microsoft.AspNet.Server.Kestrel/KestrelEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void Dispose()
7979
{
8080
foreach (var thread in Threads)
8181
{
82-
thread.Stop(TimeSpan.FromSeconds(45));
82+
thread.Stop(TimeSpan.FromSeconds(2.5));
8383
}
8484
Threads.Clear();
8585
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ unsafe protected void CreateHandle(Libuv uv, int size)
3636
_threadId = Thread.CurrentThread.ManagedThreadId;
3737

3838
handle = Marshal.AllocCoTaskMem(size);
39-
*(IntPtr*)handle = GCHandle.ToIntPtr(GCHandle.Alloc(this));
39+
*(IntPtr*)handle = GCHandle.ToIntPtr(GCHandle.Alloc(this, GCHandleType.Weak));
4040
}
4141

4242
protected void CreateHandle(UvLoopHandle loop, int size)
@@ -54,7 +54,7 @@ public void Validate(bool closed = false)
5454
{
5555
Trace.Assert(closed || !IsClosed, "Handle is closed");
5656
Trace.Assert(!IsInvalid, "Handle is invalid");
57-
Trace.Assert(_threadId == Thread.CurrentThread.ManagedThreadId, "ThreadId is correct");
57+
Trace.Assert(_threadId == Thread.CurrentThread.ManagedThreadId, "ThreadId is incorrect");
5858
}
5959

6060
unsafe protected static void DestroyHandle(IntPtr memory)

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Diagnostics;
6+
using System.Runtime.InteropServices;
67

78
namespace Microsoft.AspNet.Server.Kestrel.Networking
89
{
@@ -12,19 +13,33 @@ public abstract class UvStreamHandle : UvHandle
1213
private readonly static Libuv.uv_alloc_cb _uv_alloc_cb = UvAllocCb;
1314
private readonly static Libuv.uv_read_cb _uv_read_cb = UvReadCb;
1415

15-
public Action<UvStreamHandle, int, Exception, object> _connectionCallback;
16-
public object _connectionState;
16+
public Action<UvStreamHandle, int, Exception, object> _listenCallback;
17+
public object _listenState;
18+
private GCHandle _listenVitality;
1719

1820
public Func<UvStreamHandle, int, object, Libuv.uv_buf_t> _allocCallback;
19-
2021
public Action<UvStreamHandle, int, Exception, object> _readCallback;
2122
public object _readState;
23+
private GCHandle _readVitality;
2224

25+
protected override bool ReleaseHandle()
26+
{
27+
if (_listenVitality.IsAllocated)
28+
{
29+
_listenVitality.Free();
30+
}
31+
if (_readVitality.IsAllocated)
32+
{
33+
_readVitality.Free();
34+
}
35+
return base.ReleaseHandle();
36+
}
2337

2438
public void Listen(int backlog, Action<UvStreamHandle, int, Exception, object> callback, object state)
2539
{
26-
_connectionCallback = callback;
27-
_connectionState = state;
40+
_listenCallback = callback;
41+
_listenState = state;
42+
_listenVitality = GCHandle.Alloc(this, GCHandleType.Normal);
2843
_uv.listen(this, 10, _uv_connection_cb);
2944
}
3045

@@ -41,11 +56,16 @@ public void ReadStart(
4156
_allocCallback = allocCallback;
4257
_readCallback = readCallback;
4358
_readState = state;
59+
_readVitality = GCHandle.Alloc(this, GCHandleType.Normal);
4460
_uv.read_start(this, _uv_alloc_cb, _uv_read_cb);
4561
}
4662

4763
public void ReadStop()
4864
{
65+
_allocCallback = null;
66+
_readCallback = null;
67+
_readState = null;
68+
_readVitality.Free();
4969
_uv.read_stop(this);
5070
}
5171

@@ -64,7 +84,7 @@ private static void UvConnectionCb(IntPtr handle, int status)
6484

6585
try
6686
{
67-
stream._connectionCallback(stream, status, error, stream._connectionState);
87+
stream._listenCallback(stream, status, error, stream._listenState);
6888
}
6989
catch (Exception ex)
7090
{

0 commit comments

Comments
 (0)