From cf3f8c8a4ee95497a1771145b37e0f7d41bbae5e Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 1 Dec 2025 01:35:38 -0500 Subject: [PATCH 1/2] [dotnet] Replace `Lazy` with `??=` in `BrowsingContext` --- .../BiDi/BrowsingContext/BrowsingContext.cs | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs index add2a12b9c865..4f1e2a58b56f9 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs @@ -29,19 +29,13 @@ internal BrowsingContext(BiDi bidi, string id) { BiDi = bidi; Id = id; - - _logModule = new Lazy(() => new BrowsingContextLogModule(this, BiDi.Log)); - _networkModule = new Lazy(() => new BrowsingContextNetworkModule(this, BiDi.Network)); - _scriptModule = new Lazy(() => new BrowsingContextScriptModule(this, BiDi.Script)); - _storageModule = new Lazy(() => new BrowsingContextStorageModule(this, BiDi.Storage)); - _inputModule = new Lazy(() => new BrowsingContextInputModule(this, BiDi.InputModule)); } - private readonly Lazy _logModule; - private readonly Lazy _networkModule; - private readonly Lazy _scriptModule; - private readonly Lazy _storageModule; - private readonly Lazy _inputModule; + private BrowsingContextLogModule? _logModule; + private BrowsingContextNetworkModule? _networkModule; + private BrowsingContextScriptModule? _scriptModule; + private BrowsingContextStorageModule? _storageModule; + private BrowsingContextInputModule? _inputModule; internal string Id { get; } @@ -49,19 +43,19 @@ internal BrowsingContext(BiDi bidi, string id) public BiDi BiDi { get; } [JsonIgnore] - public BrowsingContextLogModule Log => _logModule.Value; + public BrowsingContextLogModule Log => _logModule ??= new BrowsingContextLogModule(this, BiDi.Log); [JsonIgnore] - public BrowsingContextNetworkModule Network => _networkModule.Value; + public BrowsingContextNetworkModule Network => _networkModule ??= new BrowsingContextNetworkModule(this, BiDi.Network); [JsonIgnore] - public BrowsingContextScriptModule Script => _scriptModule.Value; + public BrowsingContextScriptModule Script => _scriptModule ??= new BrowsingContextScriptModule(this, BiDi.Script); [JsonIgnore] - public BrowsingContextStorageModule Storage => _storageModule.Value; + public BrowsingContextStorageModule Storage => _storageModule ??= new BrowsingContextStorageModule(this, BiDi.Storage); [JsonIgnore] - public BrowsingContextInputModule Input => _inputModule.Value; + public BrowsingContextInputModule Input => _inputModule ??= new BrowsingContextInputModule(this, BiDi.InputModule); public Task NavigateAsync(string url, NavigateOptions? options = null) { From 7bd295faeaa97960e52f8ef74164e32edfbab18c Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Mon, 1 Dec 2025 18:06:19 +0300 Subject: [PATCH 2/2] Thread-safe --- .../webdriver/BiDi/BrowsingContext/BrowsingContext.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs index 4f1e2a58b56f9..24fdc57b0b1bd 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs @@ -19,6 +19,7 @@ using System; using System.Text.Json.Serialization; +using System.Threading; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.BrowsingContext; @@ -43,19 +44,19 @@ internal BrowsingContext(BiDi bidi, string id) public BiDi BiDi { get; } [JsonIgnore] - public BrowsingContextLogModule Log => _logModule ??= new BrowsingContextLogModule(this, BiDi.Log); + public BrowsingContextLogModule Log => _logModule ?? Interlocked.CompareExchange(ref _logModule, new BrowsingContextLogModule(this, BiDi.Log), null) ?? _logModule; [JsonIgnore] - public BrowsingContextNetworkModule Network => _networkModule ??= new BrowsingContextNetworkModule(this, BiDi.Network); + public BrowsingContextNetworkModule Network => _networkModule ?? Interlocked.CompareExchange(ref _networkModule, new BrowsingContextNetworkModule(this, BiDi.Network), null) ?? _networkModule; [JsonIgnore] - public BrowsingContextScriptModule Script => _scriptModule ??= new BrowsingContextScriptModule(this, BiDi.Script); + public BrowsingContextScriptModule Script => _scriptModule ?? Interlocked.CompareExchange(ref _scriptModule, new BrowsingContextScriptModule(this, BiDi.Script), null) ?? _scriptModule; [JsonIgnore] - public BrowsingContextStorageModule Storage => _storageModule ??= new BrowsingContextStorageModule(this, BiDi.Storage); + public BrowsingContextStorageModule Storage => _storageModule ?? Interlocked.CompareExchange(ref _storageModule, new BrowsingContextStorageModule(this, BiDi.Storage), null) ?? _storageModule; [JsonIgnore] - public BrowsingContextInputModule Input => _inputModule ??= new BrowsingContextInputModule(this, BiDi.InputModule); + public BrowsingContextInputModule Input => _inputModule ?? Interlocked.CompareExchange(ref _inputModule, new BrowsingContextInputModule(this, BiDi.InputModule), null) ?? _inputModule; public Task NavigateAsync(string url, NavigateOptions? options = null) {