#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

@ -139,19 +139,28 @@ namespace Microsoft.AspNetCore.Http.Internal
if (callback != null) if (callback != null)
{ {
try // Offload callbacks to avoid stack dives on sync completions.
var ignored = Task.Run(() =>
{ {
callback(tcs.Task); try
} {
catch (Exception) callback(tcs.Task);
{ }
// Suppress exceptions on background threads. catch (Exception)
} {
// 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

@ -229,7 +229,18 @@ namespace Microsoft.AspNetCore.WebUtilities
tcs.TrySetResult(toCopy); tcs.TrySetResult(toCopy);
if (callback != null) if (callback != null)
{ {
callback(tcs.Task); // Offload callbacks to avoid stack dives on sync completions.
var ignored = Task.Run(() =>
{
try
{
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

@ -232,19 +232,28 @@ namespace Microsoft.AspNetCore.WebUtilities
if (callback != null) if (callback != null)
{ {
try // Offload callbacks to avoid stack dives on sync completions.
var ignored = Task.Run(() =>
{ {
callback(tcs.Task); try
} {
catch (Exception) callback(tcs.Task);
{ }
// Suppress exceptions on background threads. catch (Exception)
} {
// 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

@ -184,19 +184,28 @@ namespace Microsoft.AspNetCore.WebUtilities
if (callback != null) if (callback != null)
{ {
try // Offload callbacks to avoid stack dives on sync completions.
var ignored = Task.Run(() =>
{ {
callback(tcs.Task); try
} {
catch (Exception) callback(tcs.Task);
{ }
// Suppress exceptions on background threads. catch (Exception)
} {
// 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();
} }