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

Commit b43e594

Browse files
committed
Gracefully shutdown even when there are open connections
1 parent 2bc5d95 commit b43e594

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ _ReSharper.*/
77
packages/
88
artifacts/
99
PublishProfiles/
10+
.vs/
1011
*.user
1112
*.suo
1213
*.cache

src/Microsoft.AspNet.Server.Kestrel/Infrastructure/KestrelThread.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ public void Stop(TimeSpan timeout)
5353
Post(OnStop, null);
5454
if (!_thread.Join((int)timeout.TotalMilliseconds))
5555
{
56-
Post(OnStopImmediate, null);
56+
Post(OnStopRude, null);
5757
if (!_thread.Join((int)timeout.TotalMilliseconds))
5858
{
59+
Post(OnStopImmediate, null);
60+
if (!_thread.Join((int)timeout.TotalMilliseconds))
61+
{
5962
#if DNX451
60-
_thread.Abort();
63+
_thread.Abort();
6164
#endif
65+
}
6266
}
6367
}
6468
if (_closeError != null)
@@ -72,6 +76,21 @@ private void OnStop(object obj)
7276
_post.Unreference();
7377
}
7478

79+
private void OnStopRude(object obj)
80+
{
81+
_engine.Libuv.walk(
82+
_loop,
83+
(ptr, arg) =>
84+
{
85+
var handle = UvMemory.FromIntPtr<UvHandle>(ptr);
86+
if (handle != _post)
87+
{
88+
handle.Dispose();
89+
}
90+
},
91+
IntPtr.Zero);
92+
}
93+
7594
private void OnStopImmediate(object obj)
7695
{
7796
_stopImmediate = true;
@@ -133,14 +152,8 @@ private void ThreadStart(object parameter)
133152
// run the loop one more time to delete the open handles
134153
_post.Reference();
135154
_post.DangerousClose();
136-
_engine.Libuv.walk(
137-
_loop,
138-
(ptr, arg) =>
139-
{
140-
var handle = UvMemory.FromIntPtr<UvHandle>(ptr);
141-
handle.Dispose();
142-
},
143-
IntPtr.Zero);
155+
156+
// Ensure the "DangerousClose" operation completes in the event loop.
144157
var ran2 = _loop.Run();
145158

146159
_loop.Dispose();
@@ -203,7 +216,7 @@ private void DoPostCloseHandle()
203216
queue = _closeHandleAdding;
204217
_closeHandleAdding = _closeHandleRunning;
205218
_closeHandleRunning = queue;
206-
}
219+
}
207220
while (queue.Count != 0)
208221
{
209222
var closeHandle = queue.Dequeue();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ protected override bool ReleaseHandle()
3232
{
3333
_uv.close(memory, _destroyMemory);
3434
}
35-
else
35+
else if (_queueCloseHandle != null)
3636
{
37-
_queueCloseHandle(memory2 => _uv.close(memory2, _destroyMemory), memory);
37+
// This can be called from the finalizer.
38+
// Ensure the closure doesn't reference "this".
39+
var uv = _uv;
40+
_queueCloseHandle(memory2 => uv.close(memory2, _destroyMemory), memory);
3841
}
3942
}
4043
return true;

0 commit comments

Comments
 (0)