Browser di Febe per Windows: all'uscita l'avvio non resta appeso a WebView2

Il limite di 30 s all'arresto copriva solo un «Scollega» in corso (rilievo R3
della revisione). FebeApp aspetta RunAsync prima di chiudere il browser, e
RunAsync e il ciclo di ripartenza aspettavano WebView2 senza limite: con un
WebView2 che non risponde, «Esci» restava fermo.

Ora WebView2Host.StartAsync onora il suo token. Chi chiama smette subito di
aspettare, un avvio non ancora cominciato non comincia più, uno in corso finisce
da sé e chiude ciò che ha creato se l'arresto è già passato. Il ciclo di
ripartenza usa lo stesso avvio, e WebView2Browser.DisposeAsync lo aspetta al
più 30 s. «Scollega», una volta cominciato, va invece fino in fondo: il profilo
con la sessione deve sparire e il motore deve ripartire.

Nuova prova: con il thread dell'interfaccia occupato, RunAsync torna subito
alla cancellazione e dopo l'uscita non si apre nessuna finestra. Rossa prima
della correzione (ancora in attesa dopo 10 s), verde dopo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-10-20 11:27:22 +02:00
1 parent a144d611ac
commit 7aaa93fe3b
4 files changed
+106 -6

No files matched your search

+14 -3
View File
@@ -25,6 +25,9 @@ internal sealed class WebView2Browser : IFebeBrowser
{
private static readonly TimeSpan[] Retry = [TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(5)];
/// <summary>How long stopping waits for the restart loop to end before closing WebView2 anyway.</summary>
private static readonly TimeSpan StopWait = TimeSpan.FromSeconds(30);
private readonly WebView2BrowserSettings _settings;
private readonly FebePlatformContext _context;
private readonly IClock _wallClock;
@@ -133,8 +136,9 @@ internal sealed class WebView2Browser : IFebeBrowser
}
/// <summary>
/// Stops the retries, waits for a restart in progress, then closes WebView2 and its window. <c>_lifetime</c> stays
/// undisposed on purpose: a late caller may still read its (cancelled) token.
/// Stops the retries, waits for the restart loop to notice (it stops waiting for WebView2 as soon as
/// <c>_lifetime</c> is cancelled; at most <see cref="StopWait"/> in any case), then closes WebView2 and its window.
/// <c>_lifetime</c> stays undisposed on purpose: a late caller may still read its (cancelled) token.
/// </summary>
public async ValueTask DisposeAsync()
{
@@ -147,7 +151,14 @@ internal sealed class WebView2Browser : IFebeBrowser
if (restarting is not null)
{
await restarting.ConfigureAwait(false);
try
{
await restarting.WaitAsync(StopWait).ConfigureAwait(false);
}
catch (TimeoutException)
{
_context.Log.Warn("browser", "WebView2: la ripartenza del browser non si è fermata in tempo; Febe si chiude lo stesso.");
}
}
await Host.DisposeAsync().ConfigureAwait(false);
+37 -3
View File
@@ -123,13 +123,20 @@ internal sealed class WebView2Host : IBrowserHost
/// <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.
/// and the start counts as done. A failed start leaves nothing open. When <paramref name="ct"/> is cancelled (Febe
/// is exiting) the caller stops waiting at once, even while WebView2 has not answered: a start not yet begun does not
/// begin, one under way on the UI thread ends by itself, and stopping closes what it leaves.
/// </summary>
public Task StartAsync(BrowserOptions options, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(options);
_options = options;
return OnUiAsync(() => ExclusiveAsync(StartCoreAsync));
var start = OnUiAsync(() => ExclusiveAsync(() =>
{
ct.ThrowIfCancellationRequested();
return StartCoreAsync();
}));
return UntilCancelledAsync(start, ct);
}
public Task ShowToUserAsync() => OnUiAsync(() =>
@@ -170,7 +177,9 @@ 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, like a failed <see cref="StartAsync"/>, leaves nothing open.
/// start throws its own error and, like a failed <see cref="StartAsync"/>, leaves nothing open. Once begun it goes
/// to the end whatever <paramref name="ct"/> says (the profile with the Instagram session must go, and the engine
/// starts again after it); stopping waits for it at most <see cref="StopWait"/>.
/// </summary>
public Task RestartAsync(Func<Task>? whileStopped, CancellationToken ct) => OnUiAsync(() => ExclusiveAsync(async () =>
{
@@ -253,6 +262,10 @@ internal sealed class WebView2Host : IBrowserHost
{
throw new InvalidOperationException("Il processo del browser WebView2 si è chiuso durante l'avvio.");
}
// Stopping gave up waiting for this start (WebView2 took longer than StopWait) and closed what there was:
// what this start created goes too.
ObjectDisposedException.ThrowIf(_closing, this);
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
@@ -566,6 +579,27 @@ internal sealed class WebView2Host : IBrowserHost
}
}
/// <summary>
/// Waits for <paramref name="work"/> until <paramref name="ct"/> is cancelled. Then the caller goes on and the work
/// is left to end by itself; its outcome is observed here, so a late error is never an unobserved task.
/// </summary>
private static async Task UntilCancelledAsync(Task work, CancellationToken ct)
{
try
{
await work.WaitAsync(ct).ConfigureAwait(false);
}
catch (OperationCanceledException) when (ct.IsCancellationRequested)
{
_ = work.ContinueWith(
static done => _ = done.Exception,
CancellationToken.None,
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously,
TaskScheduler.Default);
throw;
}
}
/// <summary>
/// Runs <paramref name="action"/> on the UI thread — at once when already there — and returns its task: an exception
/// is always in the task, never thrown to the caller.