Skip to content

Make the cookie provider safe for parallel use - #65

Merged
FlorianRappl merged 7 commits into
AngleSharp:develfrom
michaelthyregod:fix/cookie-provider-threads
Nov 9, 2026
Merged

FlorianRappl merged 7 commits into
AngleSharp:develfrom
michaelthyregod:fix/cookie-provider-threads

Conversation

@michaelthyregod

@michaelthyregod michaelthyregod commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

A page loads its resources in parallel, but AdvancedCookieProvider kept its cookies in a List changed from all threads. In a repro with 8 threads, a race left a null entry in it; from then on every GetCookie and SetCookie threw a NullReferenceException, even without contention.

  • Change: the cookies are 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 and there are no locks.
  • Cost (500 cookies, BenchmarkDotNet): GetCookie takes 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.
  • Behaviour: no API change. Cookies and FindCookies stay live views, and can now be enumerated while the cookies change. Single-threaded use is unchanged, including the order of the cookies.
  • Edge case: Cookies used to return the internal List itself, so code casting it back to List<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

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 FlorianRappl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@michaelthyregod

Copy link
Copy Markdown
Contributor Author

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):

devel (List) this PR (array)
GetCookie 1.62 µs, 4.9 KB 1.49 µs, 1.0 KB
replace a session cookie 17.6 µs, 1.1 KB 14.1 µs, 8.7 KB
replace a persistent cookie 92 µs 91 µs
add + remove, session 22.2 µs 15.8 µs, 9.1 KB
add + remove, persistent 167 µs 167 µs

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 ConcurrentDictionary: its reads were 5–9× slower, so I dropped it again.

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 GetCookie on every thread waits while the file is written. Here, readers never wait, and the file is written by one thread at a time; a change made during a write goes into its next round.

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 NullReferenceException (a race left a null in the List, after which every call threw) to get fixed one way or another.

@FlorianRappl

Copy link
Copy Markdown
Contributor

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:

  1. Cookies are read far move often than written.
  2. In almost all practical use you won't encounter any write problem (tell me a page where two resources load which both change the same cookie).

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).

FlorianRappl and others added 2 commits November 9, 2026 08:07
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.
@michaelthyregod

Copy link
Copy Markdown
Contributor Author

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 Set-Cookie from documents and redirects, so I used 20 iframes and 20 images behind a cookie-setting redirect (tracking pixels). Each case ran 100 page loads on current devel:

Page provider broken afterwards OpenAsync threw
20 redirecting images, different cookies 63/100 3/100
20 redirecting images, the same cookie 43/100 4/100
20 iframes, different cookies 40/100 25/100
20 iframes, the same cookie 16/100 1/100

"Broken" means SetCookie threw ("Collection was modified", ArgumentOutOfRangeException), or a null was left in the list, after which every GetCookie throws. With this PR, all 400 page loads are clean.

I agree on the complexity, so I've cut it down. The provider keeps its List and all its existing code. A change copies the list, applies the existing code to the copy and swaps it in, so reads never copy anything, which fits reads being far more frequent. The file is written by one thread at a time, as you suggested. The provider diff is now +88/−28. Cost compared with devel (500 cookies): GetCookie is the same, 1.7 µs, but allocates 1 KB instead of 4.9 KB. Changing a session cookie is about 5–8% slower because of the copy. Persistent changes are the same, since the file write dominates.

On #66: that's a different store, the HTTP cache files of PersistenceHttpCacheHandler, not the cookie file. The single writer here doesn't cover it, and parallel stores there still fail (48 of 64 in my repro). I'm happy to rebuild #66 with your per-origin queue instead, if you prefer that.

@FlorianRappl

Copy link
Copy Markdown
Contributor

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.

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".

On #66: that's a different store, the HTTP cache files of PersistenceHttpCacheHandler, not the cookie file

I am not sure about this one. The PersistenceHttpCacheHandler is the handler that enters in the provider. The provider is the one that controls / uses the handler. As such, the provider can already control it in a way that makes it implicitly thread-safe. So the idea should not be to have a thread-safe PersistenceHttpCacheHandler, but rather to have a thread-safe provider. The reason for this was stated: We should not put that burden on handler implementation, but rather solve it in the provider directly.

@michaelthyregod

Copy link
Copy Markdown
Contributor Author

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.

@FlorianRappl

Copy link
Copy Markdown
Contributor

I think changes to the provider are fine and I'd also keep the larger version - the important piece for me is that we make #66 obsolete with this change here. #66 should not be necessary if the provider is sound.

@michaelthyregod

Copy link
Copy Markdown
Contributor Author

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 c2d8f5a. That is a revert commit on top, without force-pushing, and I'll update the description to match. Nothing else changes in this PR.

#66 (HTTP cache): this one is about a different part of the library. It concerns HttpCache and its handler PersistenceHttpCacheHandler, which store cached HTTP responses on disk, one file per URL. The cookie provider doesn't use either of them, so the changes here don't affect #66. The problem there is that parallel stores to the persistent HTTP cache fail with an IOException (48 of 64 in my repro).

If I understand you correctly, you'd like the provider to be responsible, here HttpCache, so that handlers don't need to care about parallel writes. I see three options and am fine with any of them:

  1. Add a one-write-per-origin rule to HttpCache in this PR, drop the handler changes, and close Let parallel stores to the persistent HTTP cache succeed #66.
  2. Make the same change to HttpCache, but in Let parallel stores to the persistent HTTP cache succeed #66, replacing its current handler changes, so this PR stays about cookies only.
  3. Close Let parallel stores to the persistent HTTP cache succeed #66 and leave the HTTP cache as it is for now.

Which one would you prefer?

@FlorianRappl

FlorianRappl commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

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:

  1. As stated go back to the larger version - this is fine.
  2. For Let parallel stores to the persistent HTTP cache succeed #66 let's continue the discussion there. While it does not touch cookies, I still feel that its a more general problem (independent of HTTP cache we have the threading problem also with other areas where we interact with the FS, so potentially we can solve them together, but that's an independent discussion).

@FlorianRappl FlorianRappl added this to the v1.3.1 milestone Nov 9, 2026
@michaelthyregod

Copy link
Copy Markdown
Contributor Author

Done — back to the larger version (79ea869), description updated. I'll pick up the file-system part in #66.

@FlorianRappl
FlorianRappl merged commit aba2016 into AngleSharp:devel Nov 9, 2026
5 checks passed
@michaelthyregod
michaelthyregod deleted the fix/cookie-provider-threads branch November 15, 2026 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants