-
-
Notifications
You must be signed in to change notification settings - Fork 444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. #331
Comments
Could you post this issue following the issue template? |
Not understand. Can you please more clarify? |
When you create a New Issue you'll find a template to follow. |
I have had the same issue with the network manager, after some investigation looks like the issue is due to contention over two dictionaries in the NetworkManager class, “_requestIdToRequest” and “_interceptionIdToRequest”. Both these dictionaries were accessed by multiple threads causing the issue of KeyNotFound. After making some code changes to these to make them ConcurrentDictionary and making them thread safe seems to have solved the issue. This also required similar changes to the “_responses” dictionary in Connection class and the “_callbacks” dictionary in CDPSession class. |
@Goregakalack Could you share a piece of code to reproduce that? |
@Goregakalack @kblok Have you fixed this issue. |
@virenderkverma Could you share a piece of code to reproduce the issue? |
Sorry it took me so long to get back to you, had quite a few deadlines to meet. Anyway there is code to reporduce, it may take a few attempts to work. Exception thrown: System.IndexOutOfRangeException `
` |
Same here
|
Do you have a piece of code to test @DominicBoettger ? |
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using NDesk.Options;
using PuppeteerSharp;
using System.Reflection;
using System.Collections;
namespace com.inspirationlabs.prerenderer
{
class Prerenderer
{
static string Host = "http://localhost:2015";
static int Threads = Environment.ProcessorCount * 20;
static string Jsonurl = "https://my.urls";
static string OutputPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + Path.DirectorySeparatorChar + "output";
static DirectoryInfo Cwd;
static void Main(string[] args)
{
var p = new OptionSet() {
{ "host=", "Set the hostname", v => Host = v },
{ "threads=", "Set the amount of paralell threads", (int v) => Threads = v },
{ "jsonurl=", "Set the endpoint url to get the url list", v => Jsonurl = v},
{ "outputpath=", "Set the path to output the contents", v => OutputPath = v }
};
List<string> extra = p.Parse(args);
try
{
// delete outputpath if it exists
if(OutputPath.Length > 0 && Directory.Exists(OutputPath))
{
Directory.Delete(OutputPath, true);
}
if(OutputPath.Length > 0)
{
Console.WriteLine("Creating outputpath " + OutputPath);
Cwd = Directory.CreateDirectory(OutputPath);
}
} catch(Exception e)
{
Console.WriteLine(e.Message);
}
// wait for MainTask (async)
Maintask().Wait();
}
static async Task Maintask()
{
try
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(Jsonurl);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
JObject jObject = JObject.Parse(responseBody);
JArray urldata = (JArray)jObject["data"];
var fetcher = await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
await DownloadAsync(urldata);
} catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
// download data
static async Task DownloadAsync(JArray urls)
{
Processing processing = new Processing();
Queue<Page> qt = new Queue<Page>();
using (SemaphoreSlim semaphore = new SemaphoreSlim(Threads))
using (Browser browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = false,
Args = new[] { "--no-sandbox", "--disable-setuid-sandbox" }
}))
{
for (int i = 0; i <= Threads+50; i++)
{
qt.Enqueue(await browser.NewPageAsync());
}
var tasks = urls.Select(async (urldata) =>
{
if ((bool)urldata.SelectToken("published") && (bool)urldata.SelectToken("indexed"))
{
await semaphore.WaitAsync();
Page page = qt.Dequeue();
try
{
page.DefaultNavigationTimeout = 120000;
var setIsServer = @"
Object.defineProperty(window, 'isServer', {
get() {
return true
}
});
";
await page.EvaluateOnNewDocumentAsync(setIsServer);
await page.SetRequestInterceptionAsync(true);
page.Request += (sender, e) =>
{
string resType = e.Request.ResourceType.ToString();
if (resType == "Image" || resType == "Font")
{
e.Request.AbortAsync();
}
else
{
e.Request.ContinueAsync();
}
};
string path = (string)urldata.SelectToken("url");
string url = Host + path;
await page.GoToAsync(url, new NavigationOptions { WaitUntil = new[] { WaitUntilNavigation.Networkidle0 } });
string content = await page.GetContentAsync();
// put the result on the processing pipeline
processing.QueueItemAsync(content, path, OutputPath);
}
finally
{
qt.Enqueue(page);
semaphore.Release();
}
}
});
await Task.WhenAll(tasks.ToArray());
// await processing.WaitForCompleteAsync();
}
}
}
} The problem occurs after ~ 2000 rendered pages. |
@DominicBoettger I'm not getting that error. I created an array of 50 calls to Amazon. |
I tried this but this did not work too. And I also only get the error after more than 2000 requests.
After that my code works. |
@DominicBoettger I just published v1.9. Could you give it a try? |
Fixed on #720 |
puppeteer sharp throwing following exception
Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
And the main problem is that all puppeteer sharp code running inside but after that program is breaking.
The text was updated successfully, but these errors were encountered: