#602 Invoke APM callbacks on the threadpool.

This commit is contained in:
Chris R 2016-03-31 12:35:20 -07:00
parent 6725d68559
commit 3a97a6bdfd
4 changed files with 65 additions and 22 deletions

View File

@ -138,6 +138,9 @@ namespace Microsoft.AspNetCore.Http.Internal
} }
if (callback != null) if (callback != null)
{
// Offload callbacks to avoid stack dives on sync completions.
var ignored = Task.Run(() =>
{ {
try try
{ {
@ -147,11 +150,17 @@ namespace Microsoft.AspNetCore.Http.Internal
{ {
// Suppress exceptions on background threads. // Suppress exceptions on background threads.
} }
});
} }
} }
public override int EndRead(IAsyncResult asyncResult) public override int EndRead(IAsyncResult asyncResult)
{ {
if (asyncResult == null)
{
throw new ArgumentNullException(nameof(asyncResult));
}
var task = (Task<int>)asyncResult; var task = (Task<int>)asyncResult;
return task.GetAwaiter().GetResult(); return task.GetAwaiter().GetResult();
} }

View File

@ -228,9 +228,20 @@ namespace Microsoft.AspNetCore.WebUtilities
TaskCompletionSource<int> tcs = new TaskCompletionSource<int>(state); TaskCompletionSource<int> tcs = new TaskCompletionSource<int>(state);
tcs.TrySetResult(toCopy); tcs.TrySetResult(toCopy);
if (callback != null) if (callback != null)
{
// Offload callbacks to avoid stack dives on sync completions.
var ignored = Task.Run(() =>
{
try
{ {
callback(tcs.Task); callback(tcs.Task);
} }
catch (Exception)
{
// Suppress exceptions on background threads.
}
});
}
return tcs.Task; return tcs.Task;
} }
@ -239,6 +250,11 @@ namespace Microsoft.AspNetCore.WebUtilities
public override int EndRead(IAsyncResult asyncResult) public override int EndRead(IAsyncResult asyncResult)
{ {
if (asyncResult == null)
{
throw new ArgumentNullException(nameof(asyncResult));
}
Task<int> task = asyncResult as Task<int>; Task<int> task = asyncResult as Task<int>;
if (task != null) if (task != null)
{ {

View File

@ -231,6 +231,9 @@ namespace Microsoft.AspNetCore.WebUtilities
} }
if (callback != null) if (callback != null)
{
// Offload callbacks to avoid stack dives on sync completions.
var ignored = Task.Run(() =>
{ {
try try
{ {
@ -240,11 +243,17 @@ namespace Microsoft.AspNetCore.WebUtilities
{ {
// Suppress exceptions on background threads. // Suppress exceptions on background threads.
} }
});
} }
} }
public override int EndRead(IAsyncResult asyncResult) public override int EndRead(IAsyncResult asyncResult)
{ {
if (asyncResult == null)
{
throw new ArgumentNullException(nameof(asyncResult));
}
var task = (Task<int>)asyncResult; var task = (Task<int>)asyncResult;
return task.GetAwaiter().GetResult(); return task.GetAwaiter().GetResult();
} }

View File

@ -183,6 +183,9 @@ namespace Microsoft.AspNetCore.WebUtilities
} }
if (callback != null) if (callback != null)
{
// Offload callbacks to avoid stack dives on sync completions.
var ignored = Task.Run(() =>
{ {
try try
{ {
@ -192,11 +195,17 @@ namespace Microsoft.AspNetCore.WebUtilities
{ {
// Suppress exceptions on background threads. // Suppress exceptions on background threads.
} }
});
} }
} }
public override int EndRead(IAsyncResult asyncResult) public override int EndRead(IAsyncResult asyncResult)
{ {
if (asyncResult == null)
{
throw new ArgumentNullException(nameof(asyncResult));
}
var task = (Task<int>)asyncResult; var task = (Task<int>)asyncResult;
return task.GetAwaiter().GetResult(); return task.GetAwaiter().GetResult();
} }