puppeteer-sharp icon indicating copy to clipboard operation
puppeteer-sharp copied to clipboard

NetworkManager and others create unobserved exceptions

Open ECrownofFire opened this issue 1 year ago • 4 comments

This is mostly relevant when using logging solutions such as Sentry that hook into TaskScheduler.UnobservedTaskException. Unobserved tasks can be a major source of bugs, so I'd really like to keep noisiness in that event to a minimum.

On this line in NetworkManager, an exception is set on a Task that can only be observed if that response's body is attempted to be read.

There are a few other places where unobserved exceptions are created as well, such as here in IsolatedWorld.

Exceptions can be forcefully marked as observed with a snippet such as this, placed before TrySetException.

TaskCompletionSource taskWrapper;
// [...]

// Ensure that the exception is always marked as observed
taskWrapper.Task.ContinueWith(
    (t) => { _ = t.Exception; },
    CancellationToken.None,
    TaskContinuationOptions.OnlyOnFaulted,
    TaskScheduler.Default);
taskWrapper.TrySetException(new Exception("Something went wrong"));

It's not really ideal, but something like this should probably be placed everywhere that an exception is set that doesn't need to be observed. Several of these are basically just asynchronous getters that only want to throw an error when the result is unavailable, so observing them is unnecessary if they're not called.

ECrownofFire avatar May 22 '24 20:05 ECrownofFire

I had many attempts to resolve this here and here but I found no solutions.

PRs are welcome.

kblok avatar May 22 '24 21:05 kblok

@kblok can you give me a reliable way to reproduce this? I can try and fix it.

georgiosd avatar Oct 07 '24 15:10 georgiosd

@kblok can you give me a reliable way to reproduce this? I can try and fix it.

@georgiosd I think this was one way to reproduce it.

kblok avatar Oct 07 '24 16:10 kblok

Experiencing the same issue with Sentry.
I think Sentry does allow you to disable this unobserved exception capture but that sounds dangerous

kyle-revio avatar Nov 13 '24 15:11 kyle-revio