Correzione: il browser di Febe per Windows parte al primo tentativo

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 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-10-20 10:10:41 +02:00
1 parent a25f60970d
commit a6623e5736
2 files changed
+21 -6

No files matched your search

+9 -4
View File
@@ -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;
}
}
+12 -2
View File
@@ -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
/// <summary>True while a WebView is attached (commands can be sent).</summary>
public bool IsAttached => _session.IsAttached;
/// <summary>The version of the WebView2 runtime in use (for the log).</summary>
public string? Version => _environment?.BrowserVersionString;
/// <summary>
/// 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.
/// </summary>
public string? Version => _version;
/// <summary>Raised on the UI thread when the browser process of WebView2 went away by itself (the owner restarts).</summary>
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)
{