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
@@ -79,6 +79,10 @@ internal sealed class BrowserBed : IAsyncDisposable
|
||||
/// <summary>Reads a value of the UI thread (the window, its position) through the dispatcher.</summary>
|
||||
public Task<T> OnUiAsync<T>(Func<T> read) => Ui.InvokeAsync(read);
|
||||
|
||||
/// <summary>The class names of the child windows of «Browser di Febe»: one per WebView hosted in it.</summary>
|
||||
public Task<IReadOnlyList<string>> ChildWindowsAsync() =>
|
||||
OnUiAsync(() => Browser.Host.Window is { } window ? ChildWindows.Of(window.Handle) : (IReadOnlyList<string>)[]);
|
||||
|
||||
/// <summary>
|
||||
/// Starts the browser from a thread-pool thread, like <c>FebeApp.BringUpBrowserAsync</c>, and waits until
|
||||
/// <see cref="WebView2Browser.RunAsync"/> returns. Fails at once when the log reports a failed start: the loop of
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Febe.Tests.Windows.Support;
|
||||
|
||||
/// <summary>
|
||||
/// The direct child windows of a window, by class name. WebView2 hosted in a window («finestra») puts one child window
|
||||
/// per controller into its parent: two of them in «Browser di Febe» mean two WebViews, one of them orphaned.
|
||||
/// </summary>
|
||||
internal static class ChildWindows
|
||||
{
|
||||
/// <summary>The class names of the direct children of <paramref name="parent"/>, in Z order.</summary>
|
||||
public static IReadOnlyList<string> Of(IntPtr parent)
|
||||
{
|
||||
var names = new List<string>();
|
||||
var buffer = new char[256];
|
||||
var child = IntPtr.Zero;
|
||||
while ((child = FindWindowEx(parent, child, null, null)) != IntPtr.Zero)
|
||||
{
|
||||
var length = GetClassName(child, buffer, buffer.Length);
|
||||
names.Add(new string(buffer, 0, Math.Max(length, 0)));
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", EntryPoint = "FindWindowExW", CharSet = CharSet.Unicode)]
|
||||
private static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childAfter, string? className, string? windowName);
|
||||
|
||||
[DllImport("user32.dll", EntryPoint = "GetClassNameW", CharSet = CharSet.Unicode)]
|
||||
private static extern int GetClassName(IntPtr window, [Out] char[] className, int maxCount);
|
||||
}
|
||||
@@ -132,6 +132,60 @@ public sealed class WebView2LifecycleTests
|
||||
Assert.IsFalse(bed.Lines.Any("ERRORE"), bed.Lines.Text);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Regression (review of 20 October 2026): «Scollega» while the first start waits for its next attempt started the
|
||||
/// browser, then the waiting attempt started a second WebView in the same window without closing the first. The
|
||||
/// orphan kept its browser process alive on the profile: the next «Scollega» waited 15 s for nothing and could not
|
||||
/// delete the profile (the Instagram session stayed on disk), and exiting waited 15 s more.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
[Timeout(180_000, CooperativeCancellation = true)]
|
||||
public async Task ScollegaWhileTheFirstStartWaitsToRetryLeavesOneWebViewAndTheNextScollegaWorks()
|
||||
{
|
||||
var ct = TestContext.CancellationToken;
|
||||
await using var bed = await BrowserBed.CreateAsync("scollega-nei-tentativi");
|
||||
|
||||
// A file where the profile folder must go: the first start fails, and fails again 5 s later.
|
||||
var profile = bed.Browser.ProfileDirectory;
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(profile)!);
|
||||
await File.WriteAllTextAsync(profile, "occupa il posto della cartella del profilo", ct);
|
||||
var run = Task.Run(() => bed.Browser.RunAsync(ct), ct);
|
||||
await bed.WaitUntilAsync(() => bed.Lines.Any("Nuovo tentativo fra 15 s."), TimeSpan.FromSeconds(40), "il secondo avvio non riuscito", ct);
|
||||
File.Delete(profile);
|
||||
|
||||
// «Scollega» while RunAsync waits 15 s for its third attempt: the browser starts with it.
|
||||
await Task.Run(() => bed.Browser.RestartWithEmptyProfileAsync(ct), ct);
|
||||
Assert.IsTrue(bed.Browser.IsAvailable, bed.Lines.Text);
|
||||
var pid = bed.Browser.Host.BrowserProcessId;
|
||||
|
||||
// The waiting attempt finds the browser running: no second WebView, the one of «Scollega» untouched.
|
||||
await run.WaitAsync(TimeSpan.FromSeconds(40), ct);
|
||||
var children = await bed.ChildWindowsAsync();
|
||||
Assert.HasCount(1, children, "one WebView in the window: " + string.Join(", ", children));
|
||||
Assert.AreEqual(pid, bed.Browser.Host.BrowserProcessId, "the browser process of «Scollega» is still the one in use");
|
||||
Assert.IsTrue(bed.Lines.Any("Browser di Febe pronto: WebView2 "), bed.Lines.Text);
|
||||
Assert.IsTrue(bed.Browser.IsAvailable);
|
||||
|
||||
// The next «Scollega»: the only browser process exits at once, the profile goes, the browser comes back.
|
||||
var marker = Path.Combine(profile, "segno-della-prova");
|
||||
await File.WriteAllTextAsync(marker, "x", ct);
|
||||
var watch = Stopwatch.StartNew();
|
||||
await Task.Run(() => bed.Browser.RestartWithEmptyProfileAsync(ct), ct);
|
||||
TestContext.WriteLine($"Secondo «Scollega»: {watch.Elapsed.TotalSeconds:0.0} s.");
|
||||
Assert.IsLessThan(10, watch.Elapsed.TotalSeconds, "no wait for a browser process that never exits");
|
||||
Assert.IsFalse(File.Exists(marker), "the profile with the session was deleted");
|
||||
Assert.HasCount(1, await bed.ChildWindowsAsync());
|
||||
Assert.IsTrue(bed.Browser.IsAvailable);
|
||||
|
||||
// Exiting closes the only WebView and its process exits: no wait in vain.
|
||||
watch.Restart();
|
||||
await bed.Browser.DisposeAsync();
|
||||
TestContext.WriteLine($"Arresto: {watch.Elapsed.TotalSeconds:0.0} s.");
|
||||
TestContext.WriteLine(bed.Lines.Text);
|
||||
Assert.IsLessThan(10, watch.Elapsed.TotalSeconds, "exiting waits for nothing");
|
||||
Assert.HasCount(2, bed.Lines.Lines.Where(l => l.Contains("ERRORE", StringComparison.Ordinal)), "only the two failed starts: " + bed.Lines.Text);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[Timeout(180_000, CooperativeCancellation = true)]
|
||||
public async Task WhenItsBrowserProcessDiesTheBrowserStartsAgainByItself()
|
||||
|
||||
Reference in new issue
Block a user