Skip to content
Permalink
Browse files

Prevent unhandled task exceptions (#950)

* Prevent unhandled task exceptions

* Remove double blank line
  • Loading branch information...
ggeurts authored and kblok committed Mar 4, 2019
1 parent 2e7961c commit 44956cc88dc6e6c356e6fcc7b8c4dda071ae02b2
Showing with 21 additions and 15 deletions.
  1. +12 −5 lib/PuppeteerSharp/Page.cs
  2. +9 −10 lib/PuppeteerSharp/Target.cs
@@ -79,11 +79,18 @@ public class Page : IDisposable
_ignoreHTTPSErrors = ignoreHTTPSErrors;

_screenshotTaskQueue = screenshotTaskQueue;
target.CloseTask.ContinueWith((arg) =>

_ = target.CloseTask.ContinueWith((arg) =>
{
Close?.Invoke(this, EventArgs.Empty);
IsClosed = true;
_closeCompletedTcs.TrySetResult(true);
try
{
Close?.Invoke(this, EventArgs.Empty);
}
finally
{
IsClosed = true;
_closeCompletedTcs.TrySetResult(true);
}
});

Client.MessageReceived += Client_MessageReceived;
@@ -1104,7 +1111,7 @@ public Task CloseAsync(PageCloseOptions options = null)
return Client.Connection.SendAsync("Target.closeTarget", new TargetCloseTargetRequest
{
TargetId = Target.TargetId
}).ContinueWith((task) => Target.CloseTask);
}).ContinueWith(task => Target.CloseTask);
}
}

@@ -13,8 +13,8 @@ public class Target
{
#region Private members
private TargetInfo _targetInfo;
private readonly string _targetId;
private readonly Func<TargetInfo, Task<CDPSession>> _sessionFactory;
private readonly TaskCompletionSource<bool> _initializedTaskWrapper = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
#endregion

internal bool IsInitialized;
@@ -25,24 +25,24 @@ public class Target
BrowserContext browserContext)
{
_targetInfo = targetInfo;
_targetId = targetInfo.TargetId;
_sessionFactory = sessionFactory;
BrowserContext = browserContext;
PageTask = null;

InitilizedTaskWrapper = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
InitilizedTaskWrapper.Task.ContinueWith(async initializedTask =>
_ = _initializedTaskWrapper.Task.ContinueWith(async initializedTask =>
{
var success = initializedTask.Result;
if (!success)
{
return;
}
if (Opener == null || Opener.PageTask == null || Type != TargetType.Page)

var openerPageTask = Opener?.PageTask;
if (openerPageTask == null || Type != TargetType.Page)
{
return;
}
var openerPage = await Opener.PageTask.ConfigureAwait(false);
var openerPage = await openerPageTask.ConfigureAwait(false);
if (!openerPage.HasPopupEventListeners)
{
return;
@@ -56,7 +56,7 @@ public class Target

if (IsInitialized)
{
InitilizedTaskWrapper.TrySetResult(true);
_initializedTaskWrapper.TrySetResult(true);
}
}

@@ -97,8 +97,7 @@ public class Target
/// </summary>
public BrowserContext BrowserContext { get; }

internal Task<bool> InitializedTask => InitilizedTaskWrapper.Task;
internal TaskCompletionSource<bool> InitilizedTaskWrapper { get; }
internal Task<bool> InitializedTask => _initializedTaskWrapper.Task;
internal Task CloseTask => CloseTaskWrapper.Task;
internal TaskCompletionSource<bool> CloseTaskWrapper { get; }
internal Task<Page> PageTask { get; set; }
@@ -132,7 +131,7 @@ internal void TargetInfoChanged(TargetInfo targetInfo)
if (!IsInitialized && (_targetInfo.Type != TargetType.Page || _targetInfo.Url != string.Empty))
{
IsInitialized = true;
InitilizedTaskWrapper.TrySetResult(true);
_initializedTaskWrapper.TrySetResult(true);
return;
}

0 comments on commit 44956cc

Please sign in to comment.
You can’t perform that action at this time.