Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Commit

Permalink
#602 Invoke APM callbacks on the threadpool.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Mar 31, 2016
1 parent 6725d68 commit 3a97a6b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 22 deletions.
23 changes: 16 additions & 7 deletions src/Microsoft.AspNetCore.Http/Internal/ReferenceReadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,28 @@ private async void BeginRead(byte[] buffer, int offset, int count, AsyncCallback

if (callback != null)
{
try
// Offload callbacks to avoid stack dives on sync completions.
var ignored = Task.Run(() =>
{
callback(tcs.Task);
}
catch (Exception)
{
// Suppress exceptions on background threads.
}
try
{
callback(tcs.Task);
}
catch (Exception)
{
// Suppress exceptions on background threads.
}
});
}
}

public override int EndRead(IAsyncResult asyncResult)
{
if (asyncResult == null)
{
throw new ArgumentNullException(nameof(asyncResult));
}

var task = (Task<int>)asyncResult;
return task.GetAwaiter().GetResult();
}
Expand Down
18 changes: 17 additions & 1 deletion src/Microsoft.AspNetCore.WebUtilities/BufferedReadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,18 @@ public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, Asy
tcs.TrySetResult(toCopy);
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;
}
Expand All @@ -239,6 +250,11 @@ public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, Asy

public override int EndRead(IAsyncResult asyncResult)
{
if (asyncResult == null)
{
throw new ArgumentNullException(nameof(asyncResult));
}

Task<int> task = asyncResult as Task<int>;
if (task != null)
{
Expand Down
23 changes: 16 additions & 7 deletions src/Microsoft.AspNetCore.WebUtilities/FileBufferingReadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,28 @@ private async void BeginRead(byte[] buffer, int offset, int count, AsyncCallback

if (callback != null)
{
try
// Offload callbacks to avoid stack dives on sync completions.
var ignored = Task.Run(() =>
{
callback(tcs.Task);
}
catch (Exception)
{
// Suppress exceptions on background threads.
}
try
{
callback(tcs.Task);
}
catch (Exception)
{
// Suppress exceptions on background threads.
}
});
}
}

public override int EndRead(IAsyncResult asyncResult)
{
if (asyncResult == null)
{
throw new ArgumentNullException(nameof(asyncResult));
}

var task = (Task<int>)asyncResult;
return task.GetAwaiter().GetResult();
}
Expand Down
23 changes: 16 additions & 7 deletions src/Microsoft.AspNetCore.WebUtilities/MultipartReaderStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,28 @@ private async void InternalReadAsync(byte[] buffer, int offset, int size, AsyncC

if (callback != null)
{
try
// Offload callbacks to avoid stack dives on sync completions.
var ignored = Task.Run(() =>
{
callback(tcs.Task);
}
catch (Exception)
{
// Suppress exceptions on background threads.
}
try
{
callback(tcs.Task);
}
catch (Exception)
{
// Suppress exceptions on background threads.
}
});
}
}

public override int EndRead(IAsyncResult asyncResult)
{
if (asyncResult == null)
{
throw new ArgumentNullException(nameof(asyncResult));
}

var task = (Task<int>)asyncResult;
return task.GetAwaiter().GetResult();
}
Expand Down

0 comments on commit 3a97a6b

Please sign in to comment.