Repository navigation
Make the cookie provider safe for parallel use - #65
Conversation
A page loads its resources in parallel, but the provider kept its cookies in a List changed from all threads. Once a race left a null entry in it, every later GetCookie and SetCookie threw, even without contention. The cookies are now an array that is never changed in place: a change swaps in a new array with Interlocked.CompareExchange, and readers keep the one they got. A single thread writes the file at a time; a change made meanwhile is written in its next round, so no thread waits. Cookies and FindCookies stay live views and can now be enumerated while the cookies change.
FlorianRappl
left a comment
There was a problem hiding this comment.
Reading and writing cookies should be cheap. Here I see the opposite. Why not just lock the access?
Generally too much code changed here.
Copying the whole array on every change made writing a cookie more expensive. The cookies are now a ConcurrentDictionary keyed like FindCookie (canonical domain, path, name), so adding, replacing and removing a cookie is a single dictionary call instead of a search through the list. Reading keeps the order of the former sorted list; the file is still written by one thread at a time.
This reverts commit 9ed425b.
|
Thanks for the review. I measured it, since cheap reads and writes are the point (BenchmarkDotNet 0.15.8, .NET 8, 500 cookies in the jar, 10 matching the page; the mean of two runs):
So reads and writes are as cheap as today or cheaper. The one cost is more allocation when a cookie changes, from copying the array. I also tried a I'd rather not use a lock. A page loads its resources in parallel, and every request reads the cookies. Every change also writes the whole cookie file. With a lock, every On the amount of code: most of the diff is the new tests (97 lines). The provider itself grows by about 130 lines, mostly the copy-on-write helper and the single-writer file loop. If that's still too much for you, feel free to close it. I'd just like the |
|
Still not fully convinced. You may argue that most new code is in the tests, but the additions I'm referring to is all in the provider itself. This is all code we need to maintain. There are a couple of observations here:
As such while I agree that there is room for improvement here I am not sure we need the full complexity. Especially paired with #66 I see that either #66 can be dropped or this one should be much more simplified. Ideally, we drop #66 and let this one ensures that there is only one write at a time. It would also be much better for custom implementations of storages (no one has to think of such race conditions as the provider ensures that this just works). |
Readers use the current list without copying it; a change copies the list, applies the existing code to the copy and swaps it in. The file is still written by one thread at a time.
|
I took a page that does what you asked for: a local server, AngleSharp's own loader, 20 resources per page, and every response sets a cookie. Core applies
"Broken" means I agree on the complexity, so I've cut it down. The provider keeps its On #66: that's a different store, the HTTP cache files of |
How did I ask for this? I did not ask for this and that test is total bogus. My comment was about practical (i.e., real-world) websites. The "every response sets a cookie" is an assumption that I've never seen in the wild and does not make much sense. I think that conflicts arise in such scenarios is obvious and was acknowledged beforehand; so not sure what the goal of the test was - if the goal was to "tell me a page where two resources load which both change the same cookie" as in "construct me a page" then sure, but "tell me a page" was meant as in "find the URL of a popular website".
I am not sure about this one. The |
|
You're right, sorry — you asked for a real website and I built a page instead; I don't have a real-world example. Since the conflict itself is agreed, the question is only whether the smaller version (+88/−28 in the provider) is acceptable for you, or whether you'd rather keep the provider as it is. Either is fine with me. |
|
Thanks, that helps. To make sure I get it right, here is what I'll do and what I'd like to ask. #65 (cookie provider): I'll go back to the larger version, the array-based one from before my last commit #66 (HTTP cache): this one is about a different part of the library. It concerns If I understand you correctly, you'd like the provider to be responsible, here
Which one would you prefer? |
|
My bad - you are right #66 is about the HTTP handler, not the Cookie handler. For some reason I was totally in the cookie direction and did not double-check... Thanks for the clarification. Let's do the following:
|
This reverts commit c2d8f5a.
A page loads its resources in parallel, but
AdvancedCookieProviderkept its cookies in aListchanged from all threads. In a repro with 8 threads, a race left anullentry in it; from then on everyGetCookieandSetCookiethrew aNullReferenceException, even without contention.Interlocked.CompareExchange, and readers keep the one they got. A single thread writes the file at a time; a change made meanwhile is written in its next round, so no thread waits and there are no locks.GetCookietakes 1.5 µs instead of 1.7 µs and allocates 1 KB instead of 4.9 KB. Changing a session cookie takes 14 µs instead of 17–18 µs, but allocates about 9 KB instead of 1 KB for the array copy. Persistent changes are unchanged, since writing the file dominates.CookiesandFindCookiesstay live views, and can now be enumerated while the cookies change. Single-threaded use is unchanged, including the order of the cookies.Cookiesused to return the internalListitself, so code casting it back toList<WebCookie>would now fail at the cast. Such code would bypass the provider and its file writes, so I left it at that; tell me if you see it differently.Tests: 8 threads × 200 cookies keep all 1600 (also after a reload), parallel removals remove each cookie once, and enumerating while setting works.
🤖 Generated with Claude Code