Browser di Febe per Windows: un solo WebView nella finestra, chiunque lo avvii
Se «Scollega» faceva ripartire il browser mentre il primo avvio aspettava il tentativo successivo, quel tentativo creava un secondo WebView nella stessa finestra senza chiudere il primo. Il processo orfano teneva il profilo: il «Scollega» dopo aspettava 20 s e non riusciva a cancellare la sessione di Instagram, e l'uscita aspettava 15 s a vuoto (rilievo R2 della revisione, riprodotto con WebView2 vero). Ora l'avvio, dentro la sezione esclusiva di WebView2Host, non fa nulla se un WebView è già collegato, chiude prima i resti di uno morto e, se fallisce, chiude da sé ciò che ha creato. Il ciclo dei tentativi non chiude più niente da fuori: poteva chiudere un browser appena fatto ripartire da «Scollega». La ripartenza dopo un arresto del processo usa lo stesso avvio, e chiudere un WebView ne cambia la generazione, così i suoi eventi tardivi non contano più. Nuova prova di regressione: «Scollega» durante i tentativi lascia un solo controllo nella finestra, il «Scollega» dopo riesce in meno di un secondo e l'uscita non aspetta. Rossa prima della correzione (due Chrome_WidgetWin_0), verde dopo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7fffa53c90
commit
a144d611ac
8 files changed
+171
-53
No files matched your search
@@ -40,7 +40,10 @@ internal sealed record WebView2HostOptions(BrowserHosting Hosting, string Additi
|
||||
/// receivers) and the window are touched only on the UI thread, through its dispatcher; WebView2 throws on any other
|
||||
/// thread. Callers on other threads (the composition, the engine) get only plain values captured on the UI thread
|
||||
/// during the start (<see cref="Version"/>, <see cref="BrowserProcessId"/>), the CDP session, and tasks. Start,
|
||||
/// restart, reset and stop run one at a time.</item>
|
||||
/// restart and stop run one at a time.</item>
|
||||
/// <item>One WebView at a time, whoever starts it (the first start and its retries, «Scollega», the start after a
|
||||
/// crash): a start finds the WebView running and does nothing, closes what a dead one left before creating a new one,
|
||||
/// and when it fails it closes what it created itself. Events of a closed WebView never count.</item>
|
||||
/// </list>
|
||||
/// </remarks>
|
||||
internal sealed class WebView2Host : IBrowserHost
|
||||
@@ -55,7 +58,7 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
private readonly FebeLog _log;
|
||||
private readonly WebView2CdpSession _session;
|
||||
|
||||
// Start, restart, reset and dispose each await WebView2 on the UI thread: without this they could interleave there.
|
||||
// Start, restart and dispose each await WebView2 on the UI thread: without this they could interleave there.
|
||||
private readonly SemaphoreSlim _lifecycle = new(1, 1);
|
||||
|
||||
// The fields below are touched on the UI thread only.
|
||||
@@ -72,7 +75,6 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
private bool _starting;
|
||||
private bool _crashedWhileStarting;
|
||||
private bool _closing;
|
||||
private bool _restarting;
|
||||
|
||||
// Plain values read on the UI thread during the start: any thread may read them.
|
||||
private volatile string? _version;
|
||||
@@ -118,7 +120,11 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
/// </summary>
|
||||
public event EventHandler? BrowserProcessFailed;
|
||||
|
||||
/// <summary>Creates the window (once), the environment and the controller, and attaches the CDP session.</summary>
|
||||
/// <summary>
|
||||
/// Creates the window (once), the environment and the controller, and attaches the CDP session — unless a WebView
|
||||
/// is already running, because another way in started it («Scollega», the start after a crash): then it does nothing
|
||||
/// and the start counts as done. A failed start leaves nothing open.
|
||||
/// </summary>
|
||||
public Task StartAsync(BrowserOptions options, CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
@@ -164,45 +170,32 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
/// <paramref name="whileStopped"/> — «Scollega» deletes the profile there — and starts it again. <see cref="Cdp"/>
|
||||
/// stays the same object. The browser starts again even when <paramref name="whileStopped"/> fails with an
|
||||
/// <see cref="IOException"/> or <see cref="UnauthorizedAccessException"/>; that error is thrown afterwards. A failed
|
||||
/// start throws its own error and, as after a failed <see cref="StartAsync"/>, the caller closes what it left with
|
||||
/// <see cref="ResetAsync"/> before trying again.
|
||||
/// start throws its own error and, like a failed <see cref="StartAsync"/>, leaves nothing open.
|
||||
/// </summary>
|
||||
public Task RestartAsync(Func<Task>? whileStopped, CancellationToken ct) => OnUiAsync(() => ExclusiveAsync(async () =>
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_closing, this);
|
||||
Exception? failure = null;
|
||||
_restarting = true;
|
||||
try
|
||||
await CloseWebViewAsync().ConfigureAwait(true);
|
||||
if (whileStopped is not null)
|
||||
{
|
||||
await CloseWebViewAsync().ConfigureAwait(true);
|
||||
if (whileStopped is not null)
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
await whileStopped().ConfigureAwait(true);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
failure = ex;
|
||||
}
|
||||
await whileStopped().ConfigureAwait(true);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
failure = ex;
|
||||
}
|
||||
|
||||
await StartCoreAsync().ConfigureAwait(true);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_restarting = false;
|
||||
}
|
||||
|
||||
await StartCoreAsync().ConfigureAwait(true);
|
||||
if (failure is not null)
|
||||
{
|
||||
ExceptionDispatchInfo.Throw(failure);
|
||||
}
|
||||
}));
|
||||
|
||||
/// <summary>Closes what a failed start left, so that the next start begins clean.</summary>
|
||||
public Task ResetAsync() => OnUiAsync(() => ExclusiveAsync(CloseWebViewAsync));
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
try
|
||||
@@ -226,12 +219,29 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
await _session.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>UI thread, one at a time (<see cref="ExclusiveAsync"/>).</summary>
|
||||
/// <summary>True while a WebView is in the window and attached to the session (its browser process did not die).</summary>
|
||||
private bool IsRunning => _controller is not null && _session.IsAttached;
|
||||
|
||||
/// <summary>
|
||||
/// UI thread, one at a time (<see cref="ExclusiveAsync"/>): the only place a WebView is created, so the window never
|
||||
/// holds two. A WebView already running stays (the start is done); what a dead one left is closed first; a failed
|
||||
/// start closes what it created.
|
||||
/// </summary>
|
||||
private async Task StartCoreAsync()
|
||||
{
|
||||
_ui.VerifyAccess();
|
||||
ObjectDisposedException.ThrowIf(_closing, this);
|
||||
var options = _options ?? throw new InvalidOperationException("Opzioni del browser mancanti.");
|
||||
|
||||
// Started already, by «Scollega» or by the start after a crash, while this start waited for its turn or for its
|
||||
// next attempt: a second WebView would orphan the first, whose browser process then holds the profile.
|
||||
if (IsRunning)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// A WebView whose browser process died (the session is detached) is closed before the new one exists.
|
||||
await CloseWebViewAsync().ConfigureAwait(true);
|
||||
_starting = true;
|
||||
_crashedWhileStarting = false;
|
||||
try
|
||||
@@ -244,12 +254,30 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
throw new InvalidOperationException("Il processo del browser WebView2 si è chiuso durante l'avvio.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
// Nothing stays open after a failed start, whoever retries it and whenever: here, inside the same step.
|
||||
await CloseAfterFailedStartAsync().ConfigureAwait(true);
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_starting = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task CloseAfterFailedStartAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
await CloseWebViewAsync().ConfigureAwait(true);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
_log.Warn("browser", "WebView2: quello che l'avvio non riuscito ha lasciato aperto non si è chiuso del tutto.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task CreateWebViewAsync(BrowserOptions options)
|
||||
{
|
||||
if (_window is null)
|
||||
@@ -324,10 +352,14 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
await _session.SendAsync("Page.enable", null, CancellationToken.None).ConfigureAwait(true);
|
||||
}
|
||||
|
||||
/// <summary>Closes the controller and waits for the browser process to exit (the profile is written). UI thread.</summary>
|
||||
/// <summary>
|
||||
/// Closes the controller and waits for the browser process to exit (the profile is written). UI thread. From here on
|
||||
/// the events of the WebView being closed belong to an old generation: they change nothing.
|
||||
/// </summary>
|
||||
private async Task CloseWebViewAsync()
|
||||
{
|
||||
_ui.VerifyAccess();
|
||||
_generation++;
|
||||
_session.Detach();
|
||||
_devTools?.Close();
|
||||
_devTools = null;
|
||||
@@ -378,25 +410,19 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
var kind = e.ProcessFailedKind;
|
||||
_log.Warn("browser", $"WebView2: processo terminato ({kind}).");
|
||||
|
||||
// A closed or replaced WebView is not ours any more.
|
||||
// A WebView that is closing, closed or replaced is not ours any more (a restart, «Scollega», a failed start).
|
||||
if (_closing || generation != _generation)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// During a start the failure is the start's own error (the caller retries); while a restart closes the old
|
||||
// WebView the failure changes nothing.
|
||||
// During a start the failure is the start's own error: the start closes what it created and the caller retries.
|
||||
if (_starting)
|
||||
{
|
||||
_crashedWhileStarting |= kind == CoreWebView2ProcessFailedKind.BrowserProcessExited;
|
||||
return;
|
||||
}
|
||||
|
||||
if (_restarting)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (kind == CoreWebView2ProcessFailedKind.BrowserProcessExited)
|
||||
{
|
||||
// The whole browser is gone: commands fail from now on, and the owner starts it again (same profile), as
|
||||
@@ -511,7 +537,7 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs one step of the life of the WebView (start, restart, reset, stop) after the others. UI thread. With
|
||||
/// Runs one step of the life of the WebView (start, restart, stop) after the others. UI thread. With
|
||||
/// <paramref name="wait"/> the step runs anyway once that time has passed (stopping must not hang behind a start
|
||||
/// that WebView2 never completes).
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user