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

IndexOutOfRangeException in WaitForSelectorAsync

Open dhirensham opened this issue 4 years ago • 1 comments

Description

Occasionally and randomly (I cannot reliably reproduce this), I get an Index Out Of Range exception when calling WaitForSelectorAsync.

Complete minimal example reproducing the issue

                    await page.WaitForSelectorAsync("#selector", new WaitForSelectorOptions()
                    {
                        Timeout = 10000,
                        Visible = true,
                    });

Expected behavior:

Either a WaitTaskTimeoutException if the selector isn't available, or execution to resume normally.

Actual behavior:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Collections.Generic.List`1.Add(T item)
   at PuppeteerSharp.WaitTask..ctor(DOMWorld world, String predicateBody, Boolean isExpression, String title, WaitForFunctionPollingOption polling, Nullable`1 pollingInterval, Int32 timeout, Object[] args)
   at PuppeteerSharp.DOMWorld.<WaitForSelectorOrXPathAsync>d__43.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at PuppeteerSharp.Frame.<WaitForSelectorAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at mycode

Versions

PuppeteerSharp 2.0.4 .net Framework 4.7.2

Additional Information

From browsing the source, the only List.Add call in the WaitTask constructor is at https://github.com/hardkoded/puppeteer-sharp/blob/6c78425ffadf2cedad71475e3c2e34d081c5e1e0/lib/PuppeteerSharp/WaitTask.cs#L140 That doesn't seem likely to throw so perhaps another race condition similar to https://github.com/hardkoded/puppeteer-sharp/issues/717 ?

dhirensham avatar Jan 18 '21 21:01 dhirensham

I stumbled upon the exact same problem. It seems like there is a problem with using a List (which is not thread-safe) in a concurrent environment and we experience a race.

Are there any plans to address the issue?

gmackiewicz avatar Feb 12 '21 11:02 gmackiewicz