Skip to content

Commit

Permalink
Dispose CancellationTokens (#839)
Browse files Browse the repository at this point in the history
* Dispose CancellationTokens

* One more token to dispose

* CodeFactor
  • Loading branch information
kblok authored Jan 6, 2019
1 parent aff3de2 commit e31a3fa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
46 changes: 24 additions & 22 deletions lib/PuppeteerSharp/Helpers/TaskHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,23 @@ public static class TaskHelper
public static async Task WithTimeout(this Task task, int milliseconds = 1_000)
{
var tcs = new TaskCompletionSource<bool>();
var cancellationToken = new CancellationTokenSource();

if (milliseconds > 0)
using (var cancellationToken = new CancellationTokenSource())
{
cancellationToken.CancelAfter(milliseconds);
}
if (milliseconds > 0)
{
cancellationToken.CancelAfter(milliseconds);
}

using (cancellationToken.Token.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
{
if (task != await Task.WhenAny(task, tcs.Task))
using (cancellationToken.Token.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
{
throw new TimeoutException($"Timeout Exceeded: {milliseconds}ms exceeded");
if (task != await Task.WhenAny(task, tcs.Task))
{
throw new TimeoutException($"Timeout Exceeded: {milliseconds}ms exceeded");
}
}
}

await task;
await task;
}
}

//Recipe from https://blogs.msdn.microsoft.com/pfxteam/2012/10/05/how-do-i-cancel-non-cancelable-async-operations/
Expand All @@ -48,22 +49,23 @@ public static async Task WithTimeout(this Task task, int milliseconds = 1_000)
public static async Task<T> WithTimeout<T>(this Task<T> task, int milliseconds = 1_000)
{
var tcs = new TaskCompletionSource<bool>();
var cancellationToken = new CancellationTokenSource();

if (milliseconds > 0)
using (var cancellationToken = new CancellationTokenSource())
{
cancellationToken.CancelAfter(milliseconds);
}
if (milliseconds > 0)
{
cancellationToken.CancelAfter(milliseconds);
}

using (cancellationToken.Token.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
{
if (task != await Task.WhenAny(task, tcs.Task))
using (cancellationToken.Token.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
{
throw new TimeoutException($"Timeout Exceeded: {milliseconds}ms exceeded");
if (task != await Task.WhenAny(task, tcs.Task))
{
throw new TimeoutException($"Timeout Exceeded: {milliseconds}ms exceeded");
}
}
}

return await task;
return await task;
}
}

internal static async Task<T> WithConnectionCheck<T>(this Task<T> task, IConnection connection)
Expand Down
6 changes: 5 additions & 1 deletion lib/PuppeteerSharp/Transport/WebSocketTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ private void OnClose(string closeReason)
}

/// <inheritdoc/>
public void Dispose() => _client?.Dispose();
public void Dispose()
{
_client?.Dispose();
_readerCancellationSource?.Dispose();
}
}
}
1 change: 1 addition & 0 deletions lib/PuppeteerSharp/WaitTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ internal void Terminate(Exception exception)
private void Cleanup()
{
_cts.Cancel();
_cts?.Dispose();
_frame.WaitTasks.Remove(this);
}
}
Expand Down

0 comments on commit e31a3fa

Please sign in to comment.