From 3f222448eeb6880c982c0fe683836b6a40e03b59 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 20 Oct 2026 10:30:11 +0200 Subject: [PATCH] Sessione CDP di WebView2: un errore di chi ascolta la ripartenza non disfa l'avvio MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WebView2CdpSession.Attach avvisa chi ascolta Reconnected (cattura delle risposte, vista remota) dentro l'avvio del nuovo WebView, sul thread dell'interfaccia: un'eccezione di uno di loro usciva da Attach e trasformava un avvio riuscito in un avvio fallito, da riprovare, con lo stesso esito ogni volta. Ora ogni gestore è chiamato da solo: il suo errore va nel registro (tipo e prima riga) e gli altri lo ricevono comunque. L'avviso resta sincrono, come prima. Co-Authored-By: Claude Opus 5.5 --- .../WebView2/WebView2CdpSession.cs | 27 ++++++++- .../WebView2CdpSessionAttachTests.cs | 55 +++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 tests/Febe.Tests.Windows/WebView2CdpSessionAttachTests.cs diff --git a/src/Febe.Browser/WebView2/WebView2CdpSession.cs b/src/Febe.Browser/WebView2/WebView2CdpSession.cs index 5f641ed..80f3fd2 100644 --- a/src/Febe.Browser/WebView2/WebView2CdpSession.cs +++ b/src/Febe.Browser/WebView2/WebView2CdpSession.cs @@ -113,7 +113,7 @@ public sealed class WebView2CdpSession : ICdpSession, ICdpConnectionEvents, IAsy _log.Write(BrowserLogLevel.Debug, "CDP: WebView2 collegato."); if (reconnect) { - Reconnected?.Invoke(this, EventArgs.Empty); + RaiseReconnected(); } } @@ -275,6 +275,31 @@ public sealed class WebView2CdpSession : ICdpSession, ICdpConnectionEvents, IAsy return JsonElement.ParseValue(ref reader); } + /// + /// , on the thread of (WebView2's UI thread), one handler at a time: an + /// error of a handler is logged and never reaches , so it cannot undo the start of the new + /// WebView that called it. Handlers only queue their work (re-enabling a domain is a command, never sent from here). + /// + private void RaiseReconnected() + { + if (Reconnected is not { } handlers) + { + return; + } + + foreach (var handler in handlers.GetInvocationList().Cast()) + { + try + { + handler(this, EventArgs.Empty); + } + catch (Exception ex) when (ex is not OutOfMemoryException) + { + _log.Write(BrowserLogLevel.Warning, "CDP: errore in chi ascolta il nuovo collegamento con WebView2.", ex); + } + } + } + private IDisposable SubscribeNative(IWebView2DevTools devTools, string eventName) => devTools.Subscribe(eventName, json => _events.Writer.TryWrite(new QueuedEvent(eventName, json ?? string.Empty, null))); diff --git a/tests/Febe.Tests.Windows/WebView2CdpSessionAttachTests.cs b/tests/Febe.Tests.Windows/WebView2CdpSessionAttachTests.cs new file mode 100644 index 0000000..349508a --- /dev/null +++ b/tests/Febe.Tests.Windows/WebView2CdpSessionAttachTests.cs @@ -0,0 +1,55 @@ +using System.Collections.Concurrent; +using Febe.Browser.Diagnostics; +using Febe.Browser.WebView2; + +namespace Febe.Tests.Windows; + +/// +/// The attach of a new WebView to the session (after a crash or «Scollega»), without WebView2: an error of whoever +/// listens to must not undo a start that worked. +/// +[TestClass] +public sealed class WebView2CdpSessionAttachTests +{ + [TestMethod] + public async Task AFailingReconnectionHandlerDoesNotUndoTheAttach() + { + var log = new ListLog(); + await using var session = new WebView2CdpSession(log); + var later = 0; + session.Reconnected += (_, _) => throw new InvalidOperationException("guasto di chi ascolta"); + session.Reconnected += (_, _) => later++; + + session.Attach(new SilentDevTools()); + session.Attach(new SilentDevTools()); + + Assert.IsTrue(session.IsAttached, "the new WebView stays attached"); + Assert.AreEqual(1, later, "the other handlers still hear of it"); + Assert.IsTrue(log.Lines.Any(l => l.StartsWith("Warning ", StringComparison.Ordinal) && l.Contains("InvalidOperationException", StringComparison.Ordinal)), string.Join('\n', log.Lines)); + Assert.AreEqual("{}", (await session.SendAsync("Page.enable", ct: CancellationToken.None)).GetRawText()); + } + + private sealed class SilentDevTools : IWebView2DevTools + { + public Task CallMethodAsync(string methodName, string parametersAsJson) => Task.FromResult("{}"); + + public IDisposable Subscribe(string eventName, Action onEvent) => new Nothing(); + + private sealed class Nothing : IDisposable + { + public void Dispose() + { + } + } + } + + private sealed class ListLog : IBrowserLog + { + private readonly ConcurrentQueue _lines = new(); + + public IReadOnlyList Lines => [.. _lines]; + + public void Write(BrowserLogLevel level, string message, Exception? exception = null) => + _lines.Enqueue($"{level} {message} {exception?.GetType().Name}"); + } +}