BaristaLabs / chrome-dev-tools-generator

Dot Net Core based Chrome Debugger Protocol Generator
MIT License
79 stars 27 forks source link

Network.GetResponseBody not working good if many threads #8

Closed ToCSharp closed 7 years ago

ToCSharp commented 7 years ago

There is problems with multithreading.
When we request Network.GetResponseBody for many requests at the same time in different threads, it gives body from other request for some of them. If we request one by one its ok. My test project MainWindow.xaml.cs : 88 line

Oceanswave commented 7 years ago

If there's only one session (tab) in use, then this is expected - you'll have to be ensure that the Navigate => Navigate Completed => GetResponseBody happens in sequence.

If you imagine a single browser tab attempting to navigate to multiple things in parallel, you can see where this pattern falls apart.

So either use a session per thread, or implement a producer/consumer type pattern -- sort of a higher-level API that better manages navigation tasks. I've got a project over here https://github.com/BaristaLabs/skrapr that demonstrates something like this.

I'll keep this open and create a small test in the Runtime repo to make sure that multi-threaded use isn't completely broken, however.

Oceanswave commented 7 years ago

I've created a unit test demonstrating this here:

https://github.com/BaristaLabs/chrome-dev-tools-runtime/blob/master/BaristaLabs.ChromeDevTools.Runtime.Tests/Tests/ParallelUnitTests.cs

My test output includes the following, which indicates the sessions being used from multiple threads.

TestContext Messages: ThreadId: 10 ThreadId: 10 ThreadId: 12 ThreadId: 10 ThreadId: 14 ThreadId: 10 ThreadId: 12 ThreadId: 12 ThreadId: 8 ThreadId: 14

So I think if multiple sessions are used, everything works in a multithreaded environment.

Feel free to reopen if you want to discuss more, or if this doesn't describe the issue. Thanks!