From b217f4a73b3e023899df3800b3ae408622e621e1 Mon Sep 17 00:00:00 2001 From: Michael Thyregod <2386496+michaelthyregod@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:40:13 +0100 Subject: [PATCH 1/6] Make the cookie provider safe for parallel use 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. --- CHANGELOG.md | 1 + .../Cookie/ConcurrencyTests.cs | 97 ++++++++++ .../Cookie/AdvancedCookieProvider.cs | 174 ++++++++++++++---- 3 files changed, 232 insertions(+), 40 deletions(-) create mode 100644 src/AngleSharp.Io.Tests/Cookie/ConcurrencyTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index abbc4f5..7e09535 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # 1.3.2 +- Fixed the cookie provider failing for good once cookies were set or read from several threads at a time, as when a page loads its resources @michaelthyregod - Fixed `RemoveCookies` returning no cookies and `RemoveAllCookies` not updating the cookie file @michaelthyregod # 1.3.1 diff --git a/src/AngleSharp.Io.Tests/Cookie/ConcurrencyTests.cs b/src/AngleSharp.Io.Tests/Cookie/ConcurrencyTests.cs new file mode 100644 index 0000000..a80433f --- /dev/null +++ b/src/AngleSharp.Io.Tests/Cookie/ConcurrencyTests.cs @@ -0,0 +1,97 @@ +namespace AngleSharp.Io.Tests.Cookie +{ + using AngleSharp.Dom; + using AngleSharp.Io.Cookie; + using NUnit.Framework; + using System; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + + [TestFixture] + public class ConcurrencyTests + { + private const Int32 Threads = 8; + private const Int32 CookiesPerThread = 200; + + [Test] + public async Task ParallelSetAndGetKeepAllCookies() + { + var handler = new MemoryFileHandler(); + var provider = new AdvancedCookieProvider(handler); + var url = new Url("https://example.com/"); + + await RunInParallel(thread => + { + for (var i = 0; i < CookiesPerThread; i++) + { + ((ICookieProvider)provider).SetCookie(url, $"k{thread}_{i}=v; Max-Age=1000"); + ((ICookieProvider)provider).GetCookie(url); + } + }).ConfigureAwait(false); + + Assert.AreEqual(Threads * CookiesPerThread, provider.Cookies.Count()); + Assert.AreEqual(Threads * CookiesPerThread, new AdvancedCookieProvider(handler).Cookies.Count()); + } + + [Test] + public async Task ParallelRemovalsRemoveEachCookieOnce() + { + var handler = new MemoryFileHandler(); + var provider = new AdvancedCookieProvider(handler); + var url = new Url("https://example.com/"); + + for (var i = 0; i < Threads * CookiesPerThread; i++) + { + ((ICookieProvider)provider).SetCookie(url, $"k{i}=v; Max-Age=1000"); + } + + var removed = 0; + + await RunInParallel(thread => + { + for (var i = 0; i < Threads * CookiesPerThread; i++) + { + if (provider.RemoveCookie("example.com", "/", $"k{i}") != null) + { + Interlocked.Increment(ref removed); + } + } + }).ConfigureAwait(false); + + Assert.AreEqual(Threads * CookiesPerThread, removed); + Assert.IsEmpty(provider.Cookies); + Assert.IsEmpty(new AdvancedCookieProvider(handler).Cookies); + } + + [Test] + public void CookiesCanBeEnumeratedWhileTheyChange() + { + var provider = new AdvancedCookieProvider(new MemoryFileHandler()); + ((ICookieProvider)provider).SetCookie(new Url("https://example.com/"), "a=b"); + + foreach (var cookie in provider.Cookies) + { + ((ICookieProvider)provider).SetCookie(new Url("https://example.com/"), "c=d"); + } + + Assert.AreEqual(2, provider.Cookies.Count()); + } + + private static async Task RunInParallel(Action action) + { + // own threads released together by the barrier, such that the calls really overlap + using (var barrier = new Barrier(Threads)) + { + var tasks = Enumerable.Range(0, Threads) + .Select(thread => Task.Factory.StartNew(() => + { + barrier.SignalAndWait(); + action(thread); + }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default)) + .ToArray(); + await Task.WhenAll(tasks).ConfigureAwait(false); + } + } + } +} diff --git a/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs b/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs index 0cb7ffd..65ce7a5 100644 --- a/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs +++ b/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs @@ -5,6 +5,7 @@ namespace AngleSharp.Io.Cookie using System; using System.Collections.Generic; using System.Linq; + using System.Threading; using static Helpers; using static NetscapeCookieSerializer; @@ -19,7 +20,11 @@ public class AdvancedCookieProvider : ICookieProvider private readonly ICookieFileHandler _handler; private readonly Boolean _forceParse; private readonly Boolean _httpOnlyExtension; - private readonly List _cookies; + // the resources of a page are loaded in parallel, so the cookies are an array that is + // never changed in place: a change swaps in a new array, and readers keep the one they got + private WebCookie[] _cookies; + private Int32 _pendingWrites; + private Int32 _writing; /// /// Creates a new cookie provider with the given handler and options. @@ -31,7 +36,7 @@ public AdvancedCookieProvider(ICookieFileHandler handler, AdvancedCookieProvider _handler = handler; _forceParse = options.IsForceParse; _httpOnlyExtension = options.IsHttpOnlyExtension; - _cookies = ReadCookies(); + _cookies = ReadCookies().ToArray(); } String ICookieProvider.GetCookie(Url url) @@ -40,8 +45,7 @@ String ICookieProvider.GetCookie(Url url) var path = String.IsNullOrEmpty(url.Path) ? "/" : $"/{url.Path}"; var secure = url.Scheme.IsOneOf(ProtocolNames.Https, ProtocolNames.Wss); var now = DateTime.UtcNow; - var cookies = _cookies - .ToArray() + var cookies = Volatile.Read(ref _cookies) .Where(c => { if (c.IsHostOnly ?? false) @@ -125,7 +129,7 @@ void ICookieProvider.SetCookie(Url url, String value) /// /// Gets the available cookies. /// - public IEnumerable Cookies => _cookies.AsEnumerable(); + public IEnumerable Cookies => EnumerateCookies(); /// /// Gets the cookie with the given key. @@ -136,8 +140,9 @@ void ICookieProvider.SetCookie(Url url, String value) /// If matching cookie, if any. public WebCookie FindCookie(String domain, String path, String key) { - domain = CanonicalDomain(domain); - return _cookies.FirstOrDefault(cookie => cookie.Domain.Is(domain) && cookie.Path.Is(path) && cookie.Key.Is(key)); + var cookies = Volatile.Read(ref _cookies); + var index = IndexOf(cookies, domain, path, key); + return index >= 0 ? cookies[index] : null; } /// @@ -149,15 +154,7 @@ public WebCookie FindCookie(String domain, String path, String key) public IEnumerable FindCookies(String domain, String path = null) { domain = CanonicalDomain(domain); - var enumerable = _cookies.Where(cookie => cookie.Domain.Is(domain)); - - if (!String.IsNullOrEmpty(path)) - { - enumerable = enumerable - .Where(cookie => CheckPaths(path, cookie.Path)); - } - - return enumerable; + return EnumerateCookies().Where(cookie => Matches(cookie, domain, path)); } /// @@ -168,8 +165,7 @@ public void AddCookie(WebCookie cookie) { if (cookie != null) { - _cookies.Remove(FindCookie(cookie.Domain, cookie.Path, cookie.Key)); - InsertCookie(cookie); + Change(cookies => Insert(Without(cookies, cookie), cookie)); WriteCookies(); } } @@ -181,12 +177,17 @@ public void AddCookie(WebCookie cookie) /// The updated cookie content. public void UpdateCookie(WebCookie oldCookie, WebCookie newCookie) { - if (oldCookie != null) + if (newCookie != null) { - _cookies.Remove(FindCookie(oldCookie.Domain, oldCookie.Path, oldCookie.Key)); + // one change, such that no reader sees the cookie missing in between + Change(cookies => Insert(Without(Without(cookies, oldCookie), newCookie), newCookie)); + WriteCookies(); + } + else if (oldCookie != null) + { + // as before, a removal without a replacement is not written to the file + Change(cookies => Without(cookies, oldCookie)); } - - AddCookie(newCookie); } /// @@ -198,11 +199,17 @@ public void UpdateCookie(WebCookie oldCookie, WebCookie newCookie) /// The removed cookie if any. public WebCookie RemoveCookie(String domain, String path, String key) { - var cookie = FindCookie(domain, path, key); + var cookie = default(WebCookie); + + Change(cookies => + { + var index = IndexOf(cookies, domain, path, key); + cookie = index >= 0 ? cookies[index] : null; + return index >= 0 ? RemoveAt(cookies, index) : cookies; + }); if (cookie != null) { - _cookies.Remove(cookie); WriteCookies(); } @@ -217,16 +224,21 @@ public WebCookie RemoveCookie(String domain, String path, String key) /// The removed cookies. public IEnumerable RemoveCookies(String domain, String path = null) { - // taken before the removal, as the lazy query would find nothing afterwards - var cookies = FindCookies(domain, path).ToArray(); + domain = CanonicalDomain(domain); + var removed = Array.Empty(); - if (cookies.Length > 0) + Change(cookies => + { + removed = cookies.Where(cookie => Matches(cookie, domain, path)).ToArray(); + return removed.Length > 0 ? cookies.Where(cookie => !Matches(cookie, domain, path)).ToArray() : cookies; + }); + + if (removed.Length > 0) { - _cookies.RemoveAll(cookie => cookies.Contains(cookie)); WriteCookies(); } - return cookies; + return removed; } /// @@ -235,33 +247,115 @@ public IEnumerable RemoveCookies(String domain, String path = null) /// The removed cookies. public IEnumerable RemoveAllCookies() { - var cookies = _cookies.ToArray(); - _cookies.RemoveAll(cookie => true); + var cookies = Interlocked.Exchange(ref _cookies, Array.Empty()); WriteCookies(); return cookies; } private List ReadCookies() => Deserialize(_handler.ReadFile(), _forceParse, _httpOnlyExtension); - private void InsertCookie(WebCookie cookie) + // reads the array when enumerated, not when called, such that Cookies stays a live view + private IEnumerable EnumerateCookies() { - for (var i = 0; i < _cookies.Count; i++) + foreach (var cookie in Volatile.Read(ref _cookies)) { - if (cookie.CompareTo(_cookies[i]) > 0) + yield return cookie; + } + } + + private void Change(Func change) + { + while (true) + { + var current = Volatile.Read(ref _cookies); + var next = change(current); + + if (ReferenceEquals(next, current) || Interlocked.CompareExchange(ref _cookies, next, current) == current) { - _cookies.Insert(i, cookie); return; } } - - _cookies.Add(cookie); } private void WriteCookies() { - var selection = _cookies.Where(m => m.IsPersistent); - var content = Serialize(selection, _httpOnlyExtension); - _handler.WriteFile(content); + Interlocked.Increment(ref _pendingWrites); + + // a single thread writes the file; a change made meanwhile only raises the counter + // and is written in that thread's next round, so nobody waits for the file + while (Interlocked.CompareExchange(ref _writing, 1, 0) == 0) + { + try + { + while (Interlocked.Exchange(ref _pendingWrites, 0) > 0) + { + var selection = Volatile.Read(ref _cookies).Where(m => m.IsPersistent); + var content = Serialize(selection, _httpOnlyExtension); + _handler.WriteFile(content); + } + } + finally + { + Volatile.Write(ref _writing, 0); + } + + // a change raised after the last round but before the release would be lost otherwise + if (Volatile.Read(ref _pendingWrites) == 0) + { + break; + } + } + } + + private static Boolean Matches(WebCookie cookie, String domain, String path) => + cookie.Domain.Is(domain) && (String.IsNullOrEmpty(path) || CheckPaths(path, cookie.Path)); + + private static Int32 IndexOf(WebCookie[] cookies, String domain, String path, String key) + { + domain = CanonicalDomain(domain); + + for (var i = 0; i < cookies.Length; i++) + { + var cookie = cookies[i]; + + if (cookie.Domain.Is(domain) && cookie.Path.Is(path) && cookie.Key.Is(key)) + { + return i; + } + } + + return -1; + } + + private static WebCookie[] Without(WebCookie[] cookies, WebCookie cookie) + { + var index = cookie != null ? IndexOf(cookies, cookie.Domain, cookie.Path, cookie.Key) : -1; + return index >= 0 ? RemoveAt(cookies, index) : cookies; + } + + private static WebCookie[] RemoveAt(WebCookie[] cookies, Int32 index) + { + var result = new WebCookie[cookies.Length - 1]; + Array.Copy(cookies, 0, result, 0, index); + Array.Copy(cookies, index + 1, result, index, result.Length - index); + return result; + } + + // keeps the order of the former list: before the first cookie it compares greater than + private static WebCookie[] Insert(WebCookie[] cookies, WebCookie cookie) + { + var index = 0; + + while (index < cookies.Length && cookie.CompareTo(cookies[index]) <= 0) + { + index++; + } + + var result = new WebCookie[cookies.Length + 1]; + Array.Copy(cookies, 0, result, 0, index); + result[index] = cookie; + Array.Copy(cookies, index, result, index + 1, cookies.Length - index); + return result; } } } From 9ed425b50e17f567757ef0e372c7b4652b46cf7e Mon Sep 17 00:00:00 2001 From: Michael Thyregod <2386496+michaelthyregod@users.noreply.github.com> Date: Mon, 9 Nov 2026 04:45:05 +0100 Subject: [PATCH 2/6] Use a ConcurrentDictionary instead of copying an array 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. --- .../Cookie/AdvancedCookieProvider.cs | 155 ++++++------------ 1 file changed, 48 insertions(+), 107 deletions(-) diff --git a/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs b/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs index 65ce7a5..9162f11 100644 --- a/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs +++ b/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs @@ -3,6 +3,7 @@ namespace AngleSharp.Io.Cookie using AngleSharp.Dom; using AngleSharp.Text; using System; + using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -20,9 +21,9 @@ public class AdvancedCookieProvider : ICookieProvider private readonly ICookieFileHandler _handler; private readonly Boolean _forceParse; private readonly Boolean _httpOnlyExtension; - // the resources of a page are loaded in parallel, so the cookies are an array that is - // never changed in place: a change swaps in a new array, and readers keep the one they got - private WebCookie[] _cookies; + // the resources of a page are loaded in parallel, so the cookies are set and read from + // several threads; keyed like FindCookie, such that a change is a single dictionary call + private readonly ConcurrentDictionary<(String, String, String), WebCookie> _cookies; private Int32 _pendingWrites; private Int32 _writing; @@ -36,7 +37,7 @@ public AdvancedCookieProvider(ICookieFileHandler handler, AdvancedCookieProvider _handler = handler; _forceParse = options.IsForceParse; _httpOnlyExtension = options.IsHttpOnlyExtension; - _cookies = ReadCookies().ToArray(); + _cookies = ReadCookies(); } String ICookieProvider.GetCookie(Url url) @@ -45,7 +46,7 @@ String ICookieProvider.GetCookie(Url url) var path = String.IsNullOrEmpty(url.Path) ? "/" : $"/{url.Path}"; var secure = url.Scheme.IsOneOf(ProtocolNames.Https, ProtocolNames.Wss); var now = DateTime.UtcNow; - var cookies = Volatile.Read(ref _cookies) + var cookies = _cookies.Values .Where(c => { if (c.IsHostOnly ?? false) @@ -82,6 +83,7 @@ String ICookieProvider.GetCookie(Url url) return true; }) + .OrderByDescending(c => c) .Select(c => c.ToGetCookie()) .ToArray(); return String.Join("; ", cookies); @@ -140,9 +142,8 @@ void ICookieProvider.SetCookie(Url url, String value) /// If matching cookie, if any. public WebCookie FindCookie(String domain, String path, String key) { - var cookies = Volatile.Read(ref _cookies); - var index = IndexOf(cookies, domain, path, key); - return index >= 0 ? cookies[index] : null; + _cookies.TryGetValue(GetId(domain, path, key), out var cookie); + return cookie; } /// @@ -154,7 +155,15 @@ public WebCookie FindCookie(String domain, String path, String key) public IEnumerable FindCookies(String domain, String path = null) { domain = CanonicalDomain(domain); - return EnumerateCookies().Where(cookie => Matches(cookie, domain, path)); + var enumerable = EnumerateCookies().Where(cookie => cookie.Domain.Is(domain)); + + if (!String.IsNullOrEmpty(path)) + { + enumerable = enumerable + .Where(cookie => CheckPaths(path, cookie.Path)); + } + + return enumerable; } /// @@ -165,7 +174,7 @@ public void AddCookie(WebCookie cookie) { if (cookie != null) { - Change(cookies => Insert(Without(cookies, cookie), cookie)); + _cookies[GetId(cookie.Domain, cookie.Path, cookie.Key)] = cookie; WriteCookies(); } } @@ -177,17 +186,12 @@ public void AddCookie(WebCookie cookie) /// The updated cookie content. public void UpdateCookie(WebCookie oldCookie, WebCookie newCookie) { - if (newCookie != null) - { - // one change, such that no reader sees the cookie missing in between - Change(cookies => Insert(Without(Without(cookies, oldCookie), newCookie), newCookie)); - WriteCookies(); - } - else if (oldCookie != null) + if (oldCookie != null) { - // as before, a removal without a replacement is not written to the file - Change(cookies => Without(cookies, oldCookie)); + _cookies.TryRemove(GetId(oldCookie.Domain, oldCookie.Path, oldCookie.Key), out _); } + + AddCookie(newCookie); } /// @@ -199,16 +203,7 @@ public void UpdateCookie(WebCookie oldCookie, WebCookie newCookie) /// The removed cookie if any. public WebCookie RemoveCookie(String domain, String path, String key) { - var cookie = default(WebCookie); - - Change(cookies => - { - var index = IndexOf(cookies, domain, path, key); - cookie = index >= 0 ? cookies[index] : null; - return index >= 0 ? RemoveAt(cookies, index) : cookies; - }); - - if (cookie != null) + if (_cookies.TryRemove(GetId(domain, path, key), out var cookie)) { WriteCookies(); } @@ -224,21 +219,17 @@ public WebCookie RemoveCookie(String domain, String path, String key) /// The removed cookies. public IEnumerable RemoveCookies(String domain, String path = null) { - domain = CanonicalDomain(domain); - var removed = Array.Empty(); - - Change(cookies => - { - removed = cookies.Where(cookie => Matches(cookie, domain, path)).ToArray(); - return removed.Length > 0 ? cookies.Where(cookie => !Matches(cookie, domain, path)).ToArray() : cookies; - }); + // taken before the removal, as the lazy query would find nothing afterwards + var cookies = FindCookies(domain, path) + .Where(cookie => _cookies.TryRemove(GetId(cookie.Domain, cookie.Path, cookie.Key), out _)) + .ToArray(); - if (removed.Length > 0) + if (cookies.Length > 0) { WriteCookies(); } - return removed; + return cookies; } /// @@ -247,36 +238,37 @@ public IEnumerable RemoveCookies(String domain, String path = null) /// The removed cookies. public IEnumerable RemoveAllCookies() { - var cookies = Interlocked.Exchange(ref _cookies, Array.Empty()); + var cookies = EnumerateCookies() + .Where(cookie => _cookies.TryRemove(GetId(cookie.Domain, cookie.Path, cookie.Key), out _)) + .ToArray(); WriteCookies(); return cookies; } - private List ReadCookies() => Deserialize(_handler.ReadFile(), _forceParse, _httpOnlyExtension); - - // reads the array when enumerated, not when called, such that Cookies stays a live view - private IEnumerable EnumerateCookies() + private ConcurrentDictionary<(String, String, String), WebCookie> ReadCookies() { - foreach (var cookie in Volatile.Read(ref _cookies)) + var cookies = new ConcurrentDictionary<(String, String, String), WebCookie>(); + + foreach (var cookie in Deserialize(_handler.ReadFile(), _forceParse, _httpOnlyExtension)) { - yield return cookie; + cookies[GetId(cookie.Domain, cookie.Path, cookie.Key)] = cookie; } + + return cookies; } - private void Change(Func change) + // the order of the former sorted list; Values is a snapshot, taken when enumerated + private IEnumerable EnumerateCookies() { - while (true) + foreach (var cookie in _cookies.Values.OrderByDescending(c => c)) { - var current = Volatile.Read(ref _cookies); - var next = change(current); - - if (ReferenceEquals(next, current) || Interlocked.CompareExchange(ref _cookies, next, current) == current) - { - return; - } + yield return cookie; } } + private static (String, String, String) GetId(String domain, String path, String key) => + (CanonicalDomain(domain), path, key); + private void WriteCookies() { Interlocked.Increment(ref _pendingWrites); @@ -289,7 +281,7 @@ private void WriteCookies() { while (Interlocked.Exchange(ref _pendingWrites, 0) > 0) { - var selection = Volatile.Read(ref _cookies).Where(m => m.IsPersistent); + var selection = EnumerateCookies().Where(m => m.IsPersistent); var content = Serialize(selection, _httpOnlyExtension); _handler.WriteFile(content); } @@ -306,56 +298,5 @@ private void WriteCookies() } } } - - private static Boolean Matches(WebCookie cookie, String domain, String path) => - cookie.Domain.Is(domain) && (String.IsNullOrEmpty(path) || CheckPaths(path, cookie.Path)); - - private static Int32 IndexOf(WebCookie[] cookies, String domain, String path, String key) - { - domain = CanonicalDomain(domain); - - for (var i = 0; i < cookies.Length; i++) - { - var cookie = cookies[i]; - - if (cookie.Domain.Is(domain) && cookie.Path.Is(path) && cookie.Key.Is(key)) - { - return i; - } - } - - return -1; - } - - private static WebCookie[] Without(WebCookie[] cookies, WebCookie cookie) - { - var index = cookie != null ? IndexOf(cookies, cookie.Domain, cookie.Path, cookie.Key) : -1; - return index >= 0 ? RemoveAt(cookies, index) : cookies; - } - - private static WebCookie[] RemoveAt(WebCookie[] cookies, Int32 index) - { - var result = new WebCookie[cookies.Length - 1]; - Array.Copy(cookies, 0, result, 0, index); - Array.Copy(cookies, index + 1, result, index, result.Length - index); - return result; - } - - // keeps the order of the former list: before the first cookie it compares greater than - private static WebCookie[] Insert(WebCookie[] cookies, WebCookie cookie) - { - var index = 0; - - while (index < cookies.Length && cookie.CompareTo(cookies[index]) <= 0) - { - index++; - } - - var result = new WebCookie[cookies.Length + 1]; - Array.Copy(cookies, 0, result, 0, index); - result[index] = cookie; - Array.Copy(cookies, index, result, index + 1, cookies.Length - index); - return result; - } } } From f41bbc095b874a2932ff0b09af9f3b48ba460cb0 Mon Sep 17 00:00:00 2001 From: Michael Thyregod <2386496+michaelthyregod@users.noreply.github.com> Date: Mon, 9 Nov 2026 04:45:15 +0100 Subject: [PATCH 3/6] Move the CHANGELOG line into the unreleased 1.3.1 --- CHANGELOG.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e09535..d2d2fdb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,14 +1,11 @@ -# 1.3.2 - -- Fixed the cookie provider failing for good once cookies were set or read from several threads at a time, as when a page loads its resources @michaelthyregod -- Fixed `RemoveCookies` returning no cookies and `RemoveAllCookies` not updating the cookie file @michaelthyregod - # 1.3.1 Released on Sunday, November 8 2026. - Fixed `GetAttachedFileName` passing on folders from `Content-Disposition`; it now gives a plain file name @michaelthyregod - Fixed a cookie `Expires` date with a day the month does not have failing the page load @michaelthyregod +- Fixed the cookie provider failing for good once cookies were set or read from several threads at a time, as when a page loads its resources @michaelthyregod +- Fixed `RemoveCookies` returning no cookies and `RemoveAllCookies` not updating the cookie file @michaelthyregod - Added trimming and Native AOT compatibility (`IsAotCompatible`) for the .NET 8 and .NET 10 targets @michaelthyregod # 1.3.0 From 333b7db6328d00cf03f6b50bb63c8c2adda90fb7 Mon Sep 17 00:00:00 2001 From: Michael Thyregod <2386496+michaelthyregod@users.noreply.github.com> Date: Mon, 9 Nov 2026 07:48:26 +0100 Subject: [PATCH 4/6] Revert "Use a ConcurrentDictionary instead of copying an array" This reverts commit 9ed425b50e17f567757ef0e372c7b4652b46cf7e. --- .../Cookie/AdvancedCookieProvider.cs | 155 ++++++++++++------ 1 file changed, 107 insertions(+), 48 deletions(-) diff --git a/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs b/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs index 9162f11..65ce7a5 100644 --- a/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs +++ b/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs @@ -3,7 +3,6 @@ namespace AngleSharp.Io.Cookie using AngleSharp.Dom; using AngleSharp.Text; using System; - using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -21,9 +20,9 @@ public class AdvancedCookieProvider : ICookieProvider private readonly ICookieFileHandler _handler; private readonly Boolean _forceParse; private readonly Boolean _httpOnlyExtension; - // the resources of a page are loaded in parallel, so the cookies are set and read from - // several threads; keyed like FindCookie, such that a change is a single dictionary call - private readonly ConcurrentDictionary<(String, String, String), WebCookie> _cookies; + // the resources of a page are loaded in parallel, so the cookies are an array that is + // never changed in place: a change swaps in a new array, and readers keep the one they got + private WebCookie[] _cookies; private Int32 _pendingWrites; private Int32 _writing; @@ -37,7 +36,7 @@ public AdvancedCookieProvider(ICookieFileHandler handler, AdvancedCookieProvider _handler = handler; _forceParse = options.IsForceParse; _httpOnlyExtension = options.IsHttpOnlyExtension; - _cookies = ReadCookies(); + _cookies = ReadCookies().ToArray(); } String ICookieProvider.GetCookie(Url url) @@ -46,7 +45,7 @@ String ICookieProvider.GetCookie(Url url) var path = String.IsNullOrEmpty(url.Path) ? "/" : $"/{url.Path}"; var secure = url.Scheme.IsOneOf(ProtocolNames.Https, ProtocolNames.Wss); var now = DateTime.UtcNow; - var cookies = _cookies.Values + var cookies = Volatile.Read(ref _cookies) .Where(c => { if (c.IsHostOnly ?? false) @@ -83,7 +82,6 @@ String ICookieProvider.GetCookie(Url url) return true; }) - .OrderByDescending(c => c) .Select(c => c.ToGetCookie()) .ToArray(); return String.Join("; ", cookies); @@ -142,8 +140,9 @@ void ICookieProvider.SetCookie(Url url, String value) /// If matching cookie, if any. public WebCookie FindCookie(String domain, String path, String key) { - _cookies.TryGetValue(GetId(domain, path, key), out var cookie); - return cookie; + var cookies = Volatile.Read(ref _cookies); + var index = IndexOf(cookies, domain, path, key); + return index >= 0 ? cookies[index] : null; } /// @@ -155,15 +154,7 @@ public WebCookie FindCookie(String domain, String path, String key) public IEnumerable FindCookies(String domain, String path = null) { domain = CanonicalDomain(domain); - var enumerable = EnumerateCookies().Where(cookie => cookie.Domain.Is(domain)); - - if (!String.IsNullOrEmpty(path)) - { - enumerable = enumerable - .Where(cookie => CheckPaths(path, cookie.Path)); - } - - return enumerable; + return EnumerateCookies().Where(cookie => Matches(cookie, domain, path)); } /// @@ -174,7 +165,7 @@ public void AddCookie(WebCookie cookie) { if (cookie != null) { - _cookies[GetId(cookie.Domain, cookie.Path, cookie.Key)] = cookie; + Change(cookies => Insert(Without(cookies, cookie), cookie)); WriteCookies(); } } @@ -186,12 +177,17 @@ public void AddCookie(WebCookie cookie) /// The updated cookie content. public void UpdateCookie(WebCookie oldCookie, WebCookie newCookie) { - if (oldCookie != null) + if (newCookie != null) { - _cookies.TryRemove(GetId(oldCookie.Domain, oldCookie.Path, oldCookie.Key), out _); + // one change, such that no reader sees the cookie missing in between + Change(cookies => Insert(Without(Without(cookies, oldCookie), newCookie), newCookie)); + WriteCookies(); + } + else if (oldCookie != null) + { + // as before, a removal without a replacement is not written to the file + Change(cookies => Without(cookies, oldCookie)); } - - AddCookie(newCookie); } /// @@ -203,7 +199,16 @@ public void UpdateCookie(WebCookie oldCookie, WebCookie newCookie) /// The removed cookie if any. public WebCookie RemoveCookie(String domain, String path, String key) { - if (_cookies.TryRemove(GetId(domain, path, key), out var cookie)) + var cookie = default(WebCookie); + + Change(cookies => + { + var index = IndexOf(cookies, domain, path, key); + cookie = index >= 0 ? cookies[index] : null; + return index >= 0 ? RemoveAt(cookies, index) : cookies; + }); + + if (cookie != null) { WriteCookies(); } @@ -219,17 +224,21 @@ public WebCookie RemoveCookie(String domain, String path, String key) /// The removed cookies. public IEnumerable RemoveCookies(String domain, String path = null) { - // taken before the removal, as the lazy query would find nothing afterwards - var cookies = FindCookies(domain, path) - .Where(cookie => _cookies.TryRemove(GetId(cookie.Domain, cookie.Path, cookie.Key), out _)) - .ToArray(); + domain = CanonicalDomain(domain); + var removed = Array.Empty(); + + Change(cookies => + { + removed = cookies.Where(cookie => Matches(cookie, domain, path)).ToArray(); + return removed.Length > 0 ? cookies.Where(cookie => !Matches(cookie, domain, path)).ToArray() : cookies; + }); - if (cookies.Length > 0) + if (removed.Length > 0) { WriteCookies(); } - return cookies; + return removed; } /// @@ -238,37 +247,36 @@ public IEnumerable RemoveCookies(String domain, String path = null) /// The removed cookies. public IEnumerable RemoveAllCookies() { - var cookies = EnumerateCookies() - .Where(cookie => _cookies.TryRemove(GetId(cookie.Domain, cookie.Path, cookie.Key), out _)) - .ToArray(); + var cookies = Interlocked.Exchange(ref _cookies, Array.Empty()); WriteCookies(); return cookies; } - private ConcurrentDictionary<(String, String, String), WebCookie> ReadCookies() - { - var cookies = new ConcurrentDictionary<(String, String, String), WebCookie>(); + private List ReadCookies() => Deserialize(_handler.ReadFile(), _forceParse, _httpOnlyExtension); - foreach (var cookie in Deserialize(_handler.ReadFile(), _forceParse, _httpOnlyExtension)) + // reads the array when enumerated, not when called, such that Cookies stays a live view + private IEnumerable EnumerateCookies() + { + foreach (var cookie in Volatile.Read(ref _cookies)) { - cookies[GetId(cookie.Domain, cookie.Path, cookie.Key)] = cookie; + yield return cookie; } - - return cookies; } - // the order of the former sorted list; Values is a snapshot, taken when enumerated - private IEnumerable EnumerateCookies() + private void Change(Func change) { - foreach (var cookie in _cookies.Values.OrderByDescending(c => c)) + while (true) { - yield return cookie; + var current = Volatile.Read(ref _cookies); + var next = change(current); + + if (ReferenceEquals(next, current) || Interlocked.CompareExchange(ref _cookies, next, current) == current) + { + return; + } } } - private static (String, String, String) GetId(String domain, String path, String key) => - (CanonicalDomain(domain), path, key); - private void WriteCookies() { Interlocked.Increment(ref _pendingWrites); @@ -281,7 +289,7 @@ private void WriteCookies() { while (Interlocked.Exchange(ref _pendingWrites, 0) > 0) { - var selection = EnumerateCookies().Where(m => m.IsPersistent); + var selection = Volatile.Read(ref _cookies).Where(m => m.IsPersistent); var content = Serialize(selection, _httpOnlyExtension); _handler.WriteFile(content); } @@ -298,5 +306,56 @@ private void WriteCookies() } } } + + private static Boolean Matches(WebCookie cookie, String domain, String path) => + cookie.Domain.Is(domain) && (String.IsNullOrEmpty(path) || CheckPaths(path, cookie.Path)); + + private static Int32 IndexOf(WebCookie[] cookies, String domain, String path, String key) + { + domain = CanonicalDomain(domain); + + for (var i = 0; i < cookies.Length; i++) + { + var cookie = cookies[i]; + + if (cookie.Domain.Is(domain) && cookie.Path.Is(path) && cookie.Key.Is(key)) + { + return i; + } + } + + return -1; + } + + private static WebCookie[] Without(WebCookie[] cookies, WebCookie cookie) + { + var index = cookie != null ? IndexOf(cookies, cookie.Domain, cookie.Path, cookie.Key) : -1; + return index >= 0 ? RemoveAt(cookies, index) : cookies; + } + + private static WebCookie[] RemoveAt(WebCookie[] cookies, Int32 index) + { + var result = new WebCookie[cookies.Length - 1]; + Array.Copy(cookies, 0, result, 0, index); + Array.Copy(cookies, index + 1, result, index, result.Length - index); + return result; + } + + // keeps the order of the former list: before the first cookie it compares greater than + private static WebCookie[] Insert(WebCookie[] cookies, WebCookie cookie) + { + var index = 0; + + while (index < cookies.Length && cookie.CompareTo(cookies[index]) <= 0) + { + index++; + } + + var result = new WebCookie[cookies.Length + 1]; + Array.Copy(cookies, 0, result, 0, index); + result[index] = cookie; + Array.Copy(cookies, index, result, index + 1, cookies.Length - index); + return result; + } } } From c2d8f5a9977befe9361712d9bdea240f48bd981c Mon Sep 17 00:00:00 2001 From: Michael Thyregod <2386496+michaelthyregod@users.noreply.github.com> Date: Mon, 9 Nov 2026 09:24:59 +0100 Subject: [PATCH 5/6] Keep the list and copy it on change, instead of an own array 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. --- .../Cookie/AdvancedCookieProvider.cs | 136 +++++++----------- 1 file changed, 51 insertions(+), 85 deletions(-) diff --git a/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs b/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs index 65ce7a5..5abad80 100644 --- a/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs +++ b/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs @@ -20,9 +20,9 @@ public class AdvancedCookieProvider : ICookieProvider private readonly ICookieFileHandler _handler; private readonly Boolean _forceParse; private readonly Boolean _httpOnlyExtension; - // the resources of a page are loaded in parallel, so the cookies are an array that is - // never changed in place: a change swaps in a new array, and readers keep the one they got - private WebCookie[] _cookies; + // cookies are read far more often than changed, and the resources of a page are loaded in + // parallel: a change copies the list and swaps the copy in, so readers never see a half change + private List _cookies; private Int32 _pendingWrites; private Int32 _writing; @@ -36,7 +36,7 @@ public AdvancedCookieProvider(ICookieFileHandler handler, AdvancedCookieProvider _handler = handler; _forceParse = options.IsForceParse; _httpOnlyExtension = options.IsHttpOnlyExtension; - _cookies = ReadCookies().ToArray(); + _cookies = ReadCookies(); } String ICookieProvider.GetCookie(Url url) @@ -138,12 +138,8 @@ void ICookieProvider.SetCookie(Url url, String value) /// The path of the cookie. /// The key of the cookie. /// If matching cookie, if any. - public WebCookie FindCookie(String domain, String path, String key) - { - var cookies = Volatile.Read(ref _cookies); - var index = IndexOf(cookies, domain, path, key); - return index >= 0 ? cookies[index] : null; - } + public WebCookie FindCookie(String domain, String path, String key) => + Find(Volatile.Read(ref _cookies), domain, path, key); /// /// Finds all cookies that match the given domain and optional path. @@ -154,7 +150,15 @@ public WebCookie FindCookie(String domain, String path, String key) public IEnumerable FindCookies(String domain, String path = null) { domain = CanonicalDomain(domain); - return EnumerateCookies().Where(cookie => Matches(cookie, domain, path)); + var enumerable = EnumerateCookies().Where(cookie => cookie.Domain.Is(domain)); + + if (!String.IsNullOrEmpty(path)) + { + enumerable = enumerable + .Where(cookie => CheckPaths(path, cookie.Path)); + } + + return enumerable; } /// @@ -165,7 +169,11 @@ public void AddCookie(WebCookie cookie) { if (cookie != null) { - Change(cookies => Insert(Without(cookies, cookie), cookie)); + Change(cookies => + { + cookies.Remove(Find(cookies, cookie.Domain, cookie.Path, cookie.Key)); + InsertCookie(cookies, cookie); + }); WriteCookies(); } } @@ -177,17 +185,12 @@ public void AddCookie(WebCookie cookie) /// The updated cookie content. public void UpdateCookie(WebCookie oldCookie, WebCookie newCookie) { - if (newCookie != null) - { - // one change, such that no reader sees the cookie missing in between - Change(cookies => Insert(Without(Without(cookies, oldCookie), newCookie), newCookie)); - WriteCookies(); - } - else if (oldCookie != null) + if (oldCookie != null) { - // as before, a removal without a replacement is not written to the file - Change(cookies => Without(cookies, oldCookie)); + Change(cookies => cookies.Remove(Find(cookies, oldCookie.Domain, oldCookie.Path, oldCookie.Key))); } + + AddCookie(newCookie); } /// @@ -200,12 +203,10 @@ public void UpdateCookie(WebCookie oldCookie, WebCookie newCookie) public WebCookie RemoveCookie(String domain, String path, String key) { var cookie = default(WebCookie); - Change(cookies => { - var index = IndexOf(cookies, domain, path, key); - cookie = index >= 0 ? cookies[index] : null; - return index >= 0 ? RemoveAt(cookies, index) : cookies; + cookie = Find(cookies, domain, path, key); + cookies.Remove(cookie); }); if (cookie != null) @@ -224,21 +225,16 @@ public WebCookie RemoveCookie(String domain, String path, String key) /// The removed cookies. public IEnumerable RemoveCookies(String domain, String path = null) { - domain = CanonicalDomain(domain); - var removed = Array.Empty(); + // taken before the removal, as the lazy query would find nothing afterwards + var cookies = FindCookies(domain, path).ToArray(); - Change(cookies => - { - removed = cookies.Where(cookie => Matches(cookie, domain, path)).ToArray(); - return removed.Length > 0 ? cookies.Where(cookie => !Matches(cookie, domain, path)).ToArray() : cookies; - }); - - if (removed.Length > 0) + if (cookies.Length > 0) { + Change(list => list.RemoveAll(cookie => cookies.Contains(cookie))); WriteCookies(); } - return removed; + return cookies; } /// @@ -247,14 +243,14 @@ public IEnumerable RemoveCookies(String domain, String path = null) /// The removed cookies. public IEnumerable RemoveAllCookies() { - var cookies = Interlocked.Exchange(ref _cookies, Array.Empty()); + var cookies = Interlocked.Exchange(ref _cookies, new List()); WriteCookies(); - return cookies; + return cookies.ToArray(); } private List ReadCookies() => Deserialize(_handler.ReadFile(), _forceParse, _httpOnlyExtension); - // reads the array when enumerated, not when called, such that Cookies stays a live view + // reads the list when enumerated, not when called, such that Cookies stays a live view private IEnumerable EnumerateCookies() { foreach (var cookie in Volatile.Read(ref _cookies)) @@ -263,14 +259,15 @@ private IEnumerable EnumerateCookies() } } - private void Change(Func change) + private void Change(Action> change) { while (true) { var current = Volatile.Read(ref _cookies); - var next = change(current); + var next = new List(current); + change(next); - if (ReferenceEquals(next, current) || Interlocked.CompareExchange(ref _cookies, next, current) == current) + if (Interlocked.CompareExchange(ref _cookies, next, current) == current) { return; } @@ -281,8 +278,8 @@ private void WriteCookies() { Interlocked.Increment(ref _pendingWrites); - // a single thread writes the file; a change made meanwhile only raises the counter - // and is written in that thread's next round, so nobody waits for the file + // one thread writes the file at a time; a change made meanwhile is written in its + // next round, so no thread waits for the file while (Interlocked.CompareExchange(ref _writing, 1, 0) == 0) { try @@ -299,7 +296,7 @@ private void WriteCookies() Volatile.Write(ref _writing, 0); } - // a change raised after the last round but before the release would be lost otherwise + // a change made after the last round but before the release must not be lost if (Volatile.Read(ref _pendingWrites) == 0) { break; @@ -307,55 +304,24 @@ private void WriteCookies() } } - private static Boolean Matches(WebCookie cookie, String domain, String path) => - cookie.Domain.Is(domain) && (String.IsNullOrEmpty(path) || CheckPaths(path, cookie.Path)); - - private static Int32 IndexOf(WebCookie[] cookies, String domain, String path, String key) + private static WebCookie Find(List cookies, String domain, String path, String key) { domain = CanonicalDomain(domain); - - for (var i = 0; i < cookies.Length; i++) - { - var cookie = cookies[i]; - - if (cookie.Domain.Is(domain) && cookie.Path.Is(path) && cookie.Key.Is(key)) - { - return i; - } - } - - return -1; - } - - private static WebCookie[] Without(WebCookie[] cookies, WebCookie cookie) - { - var index = cookie != null ? IndexOf(cookies, cookie.Domain, cookie.Path, cookie.Key) : -1; - return index >= 0 ? RemoveAt(cookies, index) : cookies; - } - - private static WebCookie[] RemoveAt(WebCookie[] cookies, Int32 index) - { - var result = new WebCookie[cookies.Length - 1]; - Array.Copy(cookies, 0, result, 0, index); - Array.Copy(cookies, index + 1, result, index, result.Length - index); - return result; + return cookies.FirstOrDefault(cookie => cookie.Domain.Is(domain) && cookie.Path.Is(path) && cookie.Key.Is(key)); } - // keeps the order of the former list: before the first cookie it compares greater than - private static WebCookie[] Insert(WebCookie[] cookies, WebCookie cookie) + private static void InsertCookie(List cookies, WebCookie cookie) { - var index = 0; - - while (index < cookies.Length && cookie.CompareTo(cookies[index]) <= 0) + for (var i = 0; i < cookies.Count; i++) { - index++; + if (cookie.CompareTo(cookies[i]) > 0) + { + cookies.Insert(i, cookie); + return; + } } - var result = new WebCookie[cookies.Length + 1]; - Array.Copy(cookies, 0, result, 0, index); - result[index] = cookie; - Array.Copy(cookies, index, result, index + 1, cookies.Length - index); - return result; + cookies.Add(cookie); } } } From 79ea869ec13ce294261e9e0e14d1818965b9d774 Mon Sep 17 00:00:00 2001 From: Michael Thyregod <2386496+michaelthyregod@users.noreply.github.com> Date: Mon, 9 Nov 2026 15:21:06 +0100 Subject: [PATCH 6/6] Revert "Keep the list and copy it on change, instead of an own array" This reverts commit c2d8f5a9977befe9361712d9bdea240f48bd981c. --- .../Cookie/AdvancedCookieProvider.cs | 136 +++++++++++------- 1 file changed, 85 insertions(+), 51 deletions(-) diff --git a/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs b/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs index 5abad80..65ce7a5 100644 --- a/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs +++ b/src/AngleSharp.Io/Cookie/AdvancedCookieProvider.cs @@ -20,9 +20,9 @@ public class AdvancedCookieProvider : ICookieProvider private readonly ICookieFileHandler _handler; private readonly Boolean _forceParse; private readonly Boolean _httpOnlyExtension; - // cookies are read far more often than changed, and the resources of a page are loaded in - // parallel: a change copies the list and swaps the copy in, so readers never see a half change - private List _cookies; + // the resources of a page are loaded in parallel, so the cookies are an array that is + // never changed in place: a change swaps in a new array, and readers keep the one they got + private WebCookie[] _cookies; private Int32 _pendingWrites; private Int32 _writing; @@ -36,7 +36,7 @@ public AdvancedCookieProvider(ICookieFileHandler handler, AdvancedCookieProvider _handler = handler; _forceParse = options.IsForceParse; _httpOnlyExtension = options.IsHttpOnlyExtension; - _cookies = ReadCookies(); + _cookies = ReadCookies().ToArray(); } String ICookieProvider.GetCookie(Url url) @@ -138,8 +138,12 @@ void ICookieProvider.SetCookie(Url url, String value) /// The path of the cookie. /// The key of the cookie. /// If matching cookie, if any. - public WebCookie FindCookie(String domain, String path, String key) => - Find(Volatile.Read(ref _cookies), domain, path, key); + public WebCookie FindCookie(String domain, String path, String key) + { + var cookies = Volatile.Read(ref _cookies); + var index = IndexOf(cookies, domain, path, key); + return index >= 0 ? cookies[index] : null; + } /// /// Finds all cookies that match the given domain and optional path. @@ -150,15 +154,7 @@ public WebCookie FindCookie(String domain, String path, String key) => public IEnumerable FindCookies(String domain, String path = null) { domain = CanonicalDomain(domain); - var enumerable = EnumerateCookies().Where(cookie => cookie.Domain.Is(domain)); - - if (!String.IsNullOrEmpty(path)) - { - enumerable = enumerable - .Where(cookie => CheckPaths(path, cookie.Path)); - } - - return enumerable; + return EnumerateCookies().Where(cookie => Matches(cookie, domain, path)); } /// @@ -169,11 +165,7 @@ public void AddCookie(WebCookie cookie) { if (cookie != null) { - Change(cookies => - { - cookies.Remove(Find(cookies, cookie.Domain, cookie.Path, cookie.Key)); - InsertCookie(cookies, cookie); - }); + Change(cookies => Insert(Without(cookies, cookie), cookie)); WriteCookies(); } } @@ -185,12 +177,17 @@ public void AddCookie(WebCookie cookie) /// The updated cookie content. public void UpdateCookie(WebCookie oldCookie, WebCookie newCookie) { - if (oldCookie != null) + if (newCookie != null) { - Change(cookies => cookies.Remove(Find(cookies, oldCookie.Domain, oldCookie.Path, oldCookie.Key))); + // one change, such that no reader sees the cookie missing in between + Change(cookies => Insert(Without(Without(cookies, oldCookie), newCookie), newCookie)); + WriteCookies(); + } + else if (oldCookie != null) + { + // as before, a removal without a replacement is not written to the file + Change(cookies => Without(cookies, oldCookie)); } - - AddCookie(newCookie); } /// @@ -203,10 +200,12 @@ public void UpdateCookie(WebCookie oldCookie, WebCookie newCookie) public WebCookie RemoveCookie(String domain, String path, String key) { var cookie = default(WebCookie); + Change(cookies => { - cookie = Find(cookies, domain, path, key); - cookies.Remove(cookie); + var index = IndexOf(cookies, domain, path, key); + cookie = index >= 0 ? cookies[index] : null; + return index >= 0 ? RemoveAt(cookies, index) : cookies; }); if (cookie != null) @@ -225,16 +224,21 @@ public WebCookie RemoveCookie(String domain, String path, String key) /// The removed cookies. public IEnumerable RemoveCookies(String domain, String path = null) { - // taken before the removal, as the lazy query would find nothing afterwards - var cookies = FindCookies(domain, path).ToArray(); + domain = CanonicalDomain(domain); + var removed = Array.Empty(); - if (cookies.Length > 0) + Change(cookies => + { + removed = cookies.Where(cookie => Matches(cookie, domain, path)).ToArray(); + return removed.Length > 0 ? cookies.Where(cookie => !Matches(cookie, domain, path)).ToArray() : cookies; + }); + + if (removed.Length > 0) { - Change(list => list.RemoveAll(cookie => cookies.Contains(cookie))); WriteCookies(); } - return cookies; + return removed; } /// @@ -243,14 +247,14 @@ public IEnumerable RemoveCookies(String domain, String path = null) /// The removed cookies. public IEnumerable RemoveAllCookies() { - var cookies = Interlocked.Exchange(ref _cookies, new List()); + var cookies = Interlocked.Exchange(ref _cookies, Array.Empty()); WriteCookies(); - return cookies.ToArray(); + return cookies; } private List ReadCookies() => Deserialize(_handler.ReadFile(), _forceParse, _httpOnlyExtension); - // reads the list when enumerated, not when called, such that Cookies stays a live view + // reads the array when enumerated, not when called, such that Cookies stays a live view private IEnumerable EnumerateCookies() { foreach (var cookie in Volatile.Read(ref _cookies)) @@ -259,15 +263,14 @@ private IEnumerable EnumerateCookies() } } - private void Change(Action> change) + private void Change(Func change) { while (true) { var current = Volatile.Read(ref _cookies); - var next = new List(current); - change(next); + var next = change(current); - if (Interlocked.CompareExchange(ref _cookies, next, current) == current) + if (ReferenceEquals(next, current) || Interlocked.CompareExchange(ref _cookies, next, current) == current) { return; } @@ -278,8 +281,8 @@ private void WriteCookies() { Interlocked.Increment(ref _pendingWrites); - // one thread writes the file at a time; a change made meanwhile is written in its - // next round, so no thread waits for the file + // a single thread writes the file; a change made meanwhile only raises the counter + // and is written in that thread's next round, so nobody waits for the file while (Interlocked.CompareExchange(ref _writing, 1, 0) == 0) { try @@ -296,7 +299,7 @@ private void WriteCookies() Volatile.Write(ref _writing, 0); } - // a change made after the last round but before the release must not be lost + // a change raised after the last round but before the release would be lost otherwise if (Volatile.Read(ref _pendingWrites) == 0) { break; @@ -304,24 +307,55 @@ private void WriteCookies() } } - private static WebCookie Find(List cookies, String domain, String path, String key) + private static Boolean Matches(WebCookie cookie, String domain, String path) => + cookie.Domain.Is(domain) && (String.IsNullOrEmpty(path) || CheckPaths(path, cookie.Path)); + + private static Int32 IndexOf(WebCookie[] cookies, String domain, String path, String key) { domain = CanonicalDomain(domain); - return cookies.FirstOrDefault(cookie => cookie.Domain.Is(domain) && cookie.Path.Is(path) && cookie.Key.Is(key)); - } - private static void InsertCookie(List cookies, WebCookie cookie) - { - for (var i = 0; i < cookies.Count; i++) + for (var i = 0; i < cookies.Length; i++) { - if (cookie.CompareTo(cookies[i]) > 0) + var cookie = cookies[i]; + + if (cookie.Domain.Is(domain) && cookie.Path.Is(path) && cookie.Key.Is(key)) { - cookies.Insert(i, cookie); - return; + return i; } } - cookies.Add(cookie); + return -1; + } + + private static WebCookie[] Without(WebCookie[] cookies, WebCookie cookie) + { + var index = cookie != null ? IndexOf(cookies, cookie.Domain, cookie.Path, cookie.Key) : -1; + return index >= 0 ? RemoveAt(cookies, index) : cookies; + } + + private static WebCookie[] RemoveAt(WebCookie[] cookies, Int32 index) + { + var result = new WebCookie[cookies.Length - 1]; + Array.Copy(cookies, 0, result, 0, index); + Array.Copy(cookies, index + 1, result, index, result.Length - index); + return result; + } + + // keeps the order of the former list: before the first cookie it compares greater than + private static WebCookie[] Insert(WebCookie[] cookies, WebCookie cookie) + { + var index = 0; + + while (index < cookies.Length && cookie.CompareTo(cookies[index]) <= 0) + { + index++; + } + + var result = new WebCookie[cookies.Length + 1]; + Array.Copy(cookies, 0, result, 0, index); + result[index] = cookie; + Array.Copy(cookies, index, result, index + 1, cookies.Length - index); + return result; } } }