Skip to content

Commit e04a31e

Browse files
Merge pull request #84 from SergeyGulik/task_completion_hangs
TaskCompletionSource may hang an application when used with async/await.
2 parents 3f87952 + 8cec083 commit e04a31e

4 files changed

Lines changed: 10 additions & 12 deletions

File tree

libraries/MTConnect.NET-HTTP/Ceen/Common/AppDomainTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal class AppDomainTask : MarshalByRefObject
1111
/// <summary>
1212
/// A task completion source
1313
/// </summary>
14-
private TaskCompletionSource<object> m_tcs = new TaskCompletionSource<object>();
14+
private TaskCompletionSource<object> m_tcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
1515

1616
/// <summary>
1717
/// Marks the operation complete

libraries/MTConnect.NET-HTTP/Ceen/Common/AsyncLock.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ public AsyncSemaphore(int initialCount)
4848
{
4949
if (token.IsCancellationRequested)
5050
{
51-
var tcs = new TaskCompletionSource<bool>();
52-
tcs.SetCanceled();
53-
return tcs.Task;
51+
return Task.FromCanceled(token);
5452
}
5553

5654
lock (m_waiters)
@@ -62,7 +60,7 @@ public AsyncSemaphore(int initialCount)
6260
}
6361
else
6462
{
65-
var waiter = new TaskCompletionSource<bool>();
63+
var waiter = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
6664

6765
// Make sure the waiter returns asap on cancellation
6866
var registrar =

libraries/MTConnect.NET-HTTP/Ceen/Httpd/Handler/FileMirrorHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public override async Task<bool> HandleAsync(IHttpContext context, CancellationT
175175
if (!m_activeTransfers.TryGetValue(localpath, out task))
176176
{
177177
m_activeTransferSizes[localpath] = -1;
178-
m_activeTransfers[localpath] = task = (tcs = new TaskCompletionSource<long>()).Task;
178+
m_activeTransfers[localpath] = task = (tcs = new TaskCompletionSource<long>(TaskCreationOptions.RunContinuationsAsynchronously)).Task;
179179
}
180180
}
181181

@@ -518,7 +518,7 @@ private async Task DownloadRemoteFileAsync(IHttpContext context, string localpat
518518
// Swap around and notify of the progress
519519
var oldsignal = signal;
520520
lock (m_statuslock)
521-
m_activeTransfers[localpath] = (signal = new TaskCompletionSource<long>()).Task;
521+
m_activeTransfers[localpath] = (signal = new TaskCompletionSource<long>(TaskCreationOptions.RunContinuationsAsynchronously)).Task;
522522
oldsignal.TrySetResult(pg);
523523
}
524524
}

libraries/MTConnect.NET-HTTP/Ceen/Httpd/HttpServer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,15 @@ private class RunnerControl
280280
/// <summary>
281281
/// The task used to signal all requests are stopped
282282
/// </summary>
283-
private readonly TaskCompletionSource<bool> m_finishedtask = new TaskCompletionSource<bool>();
283+
private readonly TaskCompletionSource<bool> m_finishedtask = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
284284
/// <summary>
285285
/// The task used to signal all handlers to stop
286286
/// </summary>
287-
private readonly TaskCompletionSource<bool> m_stoptask = new TaskCompletionSource<bool>();
287+
private readonly TaskCompletionSource<bool> m_stoptask = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
288288
/// <summary>
289289
/// The task used to signal waiting for handlers to complete before starting new handlers
290290
/// </summary>
291-
private TaskCompletionSource<bool> m_throttletask = new TaskCompletionSource<bool>();
291+
private TaskCompletionSource<bool> m_throttletask = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
292292

293293
/// <summary>
294294
/// A logger for reporting the internal log state
@@ -338,7 +338,7 @@ public bool RegisterActive(string logtaskid)
338338
if (m_debuglogger != null) m_debuglogger("Blocking throttle", logtaskid, null);
339339
lock (m_lock)
340340
if (m_throttletask.Task.IsCompleted)
341-
m_throttletask = new TaskCompletionSource<bool>();
341+
m_throttletask = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
342342
}
343343

344344
return true;
@@ -986,7 +986,7 @@ private static async Task Runner(Stream stream, EndPoint endpoint, string logtas
986986
// Set up call context access to this instance
987987
Context.SetCurrentContext(context);
988988

989-
var timeoutcontroltask = new TaskCompletionSource<bool>();
989+
var timeoutcontroltask = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
990990
var idletime = TimeSpan.FromSeconds(config.RequestHeaderReadTimeoutSeconds);
991991

992992
// Set up timeout for processing

0 commit comments

Comments
 (0)