Skip to content

Commit 9c621a2

Browse files
committed
Change how EndSegment is called for Tasks.
If the Task has already completed, we call EndSegment (synchronously) immediately. Otherwise, we always continue with TaskContinuationOptions.ExecuteSynchronously, which runs our continuation as soon as possible (on the thread that completed the task). This avoids task continuations being delayed until much later in the ASP.NET pipeline (due to when AspNetSynchronizationContext posts them), which causes inaccurate timings.
1 parent 8c2ad16 commit 9c621a2

File tree

1 file changed

+6
-10
lines changed
  • src/Agent/NewRelic/Agent/Extensions/NewRelic.Agent.Extensions/Providers/Wrapper

1 file changed

+6
-10
lines changed

src/Agent/NewRelic/Agent/Extensions/NewRelic.Agent.Extensions/Providers/Wrapper/Delegates.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,18 @@ private static void OnSuccess<T>(Task task, IAgent agent, ISegment segment, bool
153153
transaction.Hold();
154154
}
155155

156-
if (options == TaskContinueWithOption.None)
156+
if (task.IsCompleted)
157+
{
158+
EndSegment(task);
159+
}
160+
else if (options == TaskContinueWithOption.None)
157161
{
158162
if (!continuationOptions.HasValue) task.ContinueWith(EndSegment);
159163
else task.ContinueWith(EndSegment, continuationOptions.Value);
160164
}
161165
else
162166
{
163-
var context = SynchronizationContext.Current;
164-
if (context != null)
165-
{
166-
task.ContinueWith(EndSegment, TaskScheduler.FromCurrentSynchronizationContext());
167-
}
168-
else
169-
{
170-
task.ContinueWith(EndSegment, TaskContinuationOptions.ExecuteSynchronously);
171-
}
167+
task.ContinueWith(EndSegment, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
172168
}
173169

174170
void EndSegment(Task completedTask)

0 commit comments

Comments
 (0)