From a6623e57368f1828aebcd6b9470319cdee97b519 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 20 Oct 2026 10:10:41 +0200 Subject: [PATCH] Correzione: il browser di Febe per Windows parte al primo tentativo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WebView2Host avviava WebView2 correttamente sul thread dell'interfaccia, ma WebView2Browser aspettava l'avvio con ConfigureAwait(false) e, su un thread del pool, scriveva nel registro «Browser di Febe pronto: WebView2 {Host.Version}»: Version leggeva BrowserVersionString dall'ambiente di WebView2 fuori dal thread dell'interfaccia, WebView2 lanciava InvalidOperationException, il ciclo la prendeva per un avvio fallito, chiudeva il WebView che funzionava e riprovava per sempre (5, 15, 30, 60, 300 s). La finestra «Browser di Febe» non diventava mai disponibile e l'accesso a Instagram non poteva nemmeno cominciare. Ora la versione si legge sul thread dell'interfaccia durante l'avvio e Version restituisce un valore semplice, leggibile da qualunque thread; e quello che segue un avvio riuscito (stato e riga del registro) sta fuori dal blocco dei tentativi, così non può più disfarlo: si riprova solo quando fallisce l'avvio stesso. La prova di regressione del commit precedente ora passa. Co-Authored-By: Claude Opus 5.5 --- src/Febe.Desktop/Browser/WebView2Browser.cs | 13 +++++++++---- src/Febe.Desktop/Browser/WebView2Host.cs | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Febe.Desktop/Browser/WebView2Browser.cs b/src/Febe.Desktop/Browser/WebView2Browser.cs index f717b11..a68effc 100644 --- a/src/Febe.Desktop/Browser/WebView2Browser.cs +++ b/src/Febe.Desktop/Browser/WebView2Browser.cs @@ -68,10 +68,6 @@ internal sealed class WebView2Browser : IFebeBrowser try { await Host.StartAsync(_options, ct).ConfigureAwait(false); - _started = true; - _lastError = null; - _context.Log.Info("browser", $"Browser di Febe pronto: WebView2 {Host.Version}, pagina {WindowWidth.ToString(CultureInfo.InvariantCulture)}×{WindowHeight.ToString(CultureInfo.InvariantCulture)}, {BrowserHostingNames.Name(_settings.Hosting)}, fuori vista."); - return; } catch (OperationCanceledException) when (ct.IsCancellationRequested) { @@ -91,7 +87,16 @@ internal sealed class WebView2Browser : IFebeBrowser { return; } + + continue; } + + // Outside the retries: what follows a start that worked never undoes it. This runs on a thread of the + // composition, so it reads only plain values of the host (the version was read on the UI thread). + _started = true; + _lastError = null; + _context.Log.Info("browser", $"Browser di Febe pronto: WebView2 {Host.Version}, pagina {WindowWidth.ToString(CultureInfo.InvariantCulture)}×{WindowHeight.ToString(CultureInfo.InvariantCulture)}, {BrowserHostingNames.Name(_settings.Hosting)}, fuori vista."); + return; } } diff --git a/src/Febe.Desktop/Browser/WebView2Host.cs b/src/Febe.Desktop/Browser/WebView2Host.cs index 22d4b95..8fe5919 100644 --- a/src/Febe.Desktop/Browser/WebView2Host.cs +++ b/src/Febe.Desktop/Browser/WebView2Host.cs @@ -57,6 +57,9 @@ internal sealed class WebView2Host : IBrowserHost private bool _closing; private bool _restarting; + // Read on the UI thread during the start: a plain value that any thread may read. + private volatile string? _version; + public WebView2Host(Dispatcher ui, WebView2HostOptions settings, FebeLog log) { _ui = ui ?? throw new ArgumentNullException(nameof(ui)); @@ -79,8 +82,11 @@ internal sealed class WebView2Host : IBrowserHost /// True while a WebView is attached (commands can be sent). public bool IsAttached => _session.IsAttached; - /// The version of the WebView2 runtime in use (for the log). - public string? Version => _environment?.BrowserVersionString; + /// + /// The version of the WebView2 runtime of the browser in use (for the log), read on the UI thread when it started; + /// null before the first start. Any thread: it is a plain value, never a member of a WebView2 object. + /// + public string? Version => _version; /// Raised on the UI thread when the browser process of WebView2 went away by itself (the owner restarts). public event EventHandler? BrowserProcessFailed; @@ -200,6 +206,10 @@ internal sealed class WebView2Host : IBrowserHost _exited = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); _environment.BrowserProcessExited += OnBrowserProcessExited; + // Read here, on the UI thread, for the log line the composition writes from another thread: a member of the + // environment read there throws «CoreWebView2Environment members can only be accessed from the UI thread». + _version = _environment.BrowserVersionString; + var hwnd = _window.Handle; if (_settings.Hosting == BrowserHosting.Composition) {