WebPanel: publish Failed together with the browser-fallback outcome

A plugin polling between Failed and OpenedInBrowser saw Failed with no
flag and, per the docs, opened the page itself: a second open with a
spent code. While the fallback is pending the status keeps its
pre-failure value; after OpenInBrowser (outside the lock) one lock sets
Failed, the error and, when the browser opened, the flag. The
no-fallback path is unchanged. Harness: OpenInBrowser delay mode and a
continuous poll in CreateFailureFallsBack. Show comments reworded as
defensive, not observed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-11-07 16:22:33 -08:00
commit 7e2cf67e30
3 changed files with 49 additions and 11 deletions

View file

@ -855,15 +855,20 @@ internal sealed unsafe class WebPanelHost
}
else
{
_status = (int)WebPanelStatusCode.Failed;
_error = (int)code;
WebPanelRequest? r = _req;
if (allowFallback && r != null && (code == WebPanelCode.RuntimeMissing || code == WebPanelCode.CreateFailed)
&& (r.Flags & FlagFallback) != 0 && _navCount == 0 && WebPanelUrlPolicy.IsBrowserSafe(r.Url))
{
// Failed is published only together with the fallback's outcome (below): a plugin polling in
// between must never see Failed without OpenedInBrowser and open the page a second time.
fallback = true;
url = r.Url;
}
else
{
_status = (int)WebPanelStatusCode.Failed;
_error = (int)code;
}
}
}
if (ignored) { SafeLog($"panel failure after close or supersede, panel left as it is: {code} ({reason})"); return; }
@ -872,8 +877,17 @@ internal sealed unsafe class WebPanelHost
if (!fallback) return;
bool ok = false;
try { ok = _env.OpenInBrowser(url); } catch { }
// The flag reports what happened, so it is set only once the browser opened, and only for the panel that failed.
if (ok) lock (_sync) if (handle == _live) _flags |= StateOpenedInBrowser;
// One lock: Failed, the error and (only when the browser opened) the flag become visible together, and only
// for the panel that failed; a Close or a newer Open in the meantime wins.
lock (_sync)
{
if (handle == _live && _status != (int)WebPanelStatusCode.Closed)
{
_status = (int)WebPanelStatusCode.Failed;
_error = (int)code;
if (ok) _flags |= StateOpenedInBrowser;
}
}
SafeLog((ok ? "fallback: opened in the system browser: " : "fallback: the system browser did not open: ") + WebPanelUrlPolicy.Redact(url));
string why = code == WebPanelCode.RuntimeMissing ? "WebView2 runtime not installed" : "it failed to start";
SafeChat(ok
@ -888,12 +902,13 @@ internal sealed unsafe class WebPanelHost
// Focus the panel only from within this process (the game, or a pop-out): a plugin opening it while the
// player is in another program must not pull the foreground away (the PID rule keeps it hidden meanwhile).
// Never while the foreground is null (mid alt-tab): that would grab focus on the way to another program.
// Only this process: a foreground in the WebView2 browser process is the page's own dialog, which keeps
// it (IsOurPid, which includes the browser, is for the visibility rule only).
// Only this process, defensively: a foreground in the WebView2 browser process is assumed (not observed) to
// be the page's own dialog, which should keep it (IsOurPid, which includes the browser, is for the
// visibility rule only).
bool inProcess = ForegroundIsThisProcess();
if (inProcess) SetForegroundWindow(_staHwnd);
if (_ctl != null) { try { _ctl.IsVisible = true; } catch { } }
if (inProcess) FocusPage(); // same rule: moving WebView2 focus can activate the host window
if (inProcess) FocusPage(); // same rule, defensively: moving WebView2 focus might activate the host window (not observed)
if (!_timerOn) { SetTimer(_staHwnd, TimerId, 50, IntPtr.Zero); _timerOn = true; }
UpdateStateFlags();
}

View file

@ -25,6 +25,7 @@ public static unsafe class PayloadEntry
public static void SetEnvironmentDelay(int ms) => _env!.DelayMs = ms;
public static void SetBrowserFolder(string? folder) => _env!.BrowserFolder = folder;
public static void SetBrowserFails(bool fails) => _env!.BrowserFails = fails;
public static void SetBrowserDelay(int ms) => _env!.BrowserDelayMs = ms;
public static void SetWorld(string world) => _env!.World = world;
/// <summary>The engine's WebPanelApi.Open path: read the caller's struct, then the host.</summary>
@ -111,6 +112,7 @@ public static unsafe class PayloadEntry
public volatile int DelayMs;
public volatile string? BrowserFolder;
public volatile bool BrowserFails; // OpenInBrowser returns false without recording a call
public volatile int BrowserDelayMs; // OpenInBrowser sleeps first (a slow ShellExecute)
public volatile string World = "Harness World";
public IntPtr GameHwnd { get; }
@ -122,6 +124,7 @@ public static unsafe class PayloadEntry
public bool OpenInBrowser(string url)
{
if (!WebPanelUrlPolicy.IsBrowserSafe(url)) { _log("HARNESS: OpenInBrowser got an unsafe url"); return false; }
if (BrowserDelayMs > 0) Thread.Sleep(BrowserDelayMs);
if (BrowserFails) { _log("HARNESS: OpenInBrowser set to fail"); return false; }
_browser(url);
return true;

View file

@ -524,12 +524,32 @@ internal static unsafe class Program
Drain(BrowserCalls);
Drain(ChatLines);
s.Call("SetBrowserFolder", Path.Combine(_root, "no-such-browser"));
var (h, _) = Open(s, "https://panel.test/launch.html?code=" + Secret, Allow, key: "beta");
bool failed = WaitStatus(s, h, StatusFailed, 20000, out int[] st);
s.Call("SetBrowserFolder", (string?)null);
s.Call("SetBrowserDelay", 300); // a slow browser launch widens any Failed-before-flag window
int samples = 0, failedNoFlag = 0;
bool failed = false;
int[] st;
try
{
var (h, _) = Open(s, "https://panel.test/launch.html?code=" + Secret, Allow, key: "beta");
// Poll continuously: a plugin that sees Failed without OpenedInBrowser would open the page a second time.
var sw = Stopwatch.StartNew();
st = State(s, h);
while (sw.ElapsedMilliseconds < 20000)
{
st = State(s, h);
samples++;
if (st[2] == StatusFailed && (st[3] & StateOpenedInBrowser) == 0) failedNoFlag++;
if (st[2] == StatusFailed && (st[3] & StateOpenedInBrowser) != 0) { failed = true; break; }
if (st[2] == StatusFailed && failedNoFlag > 400) break; // the flag is not coming
Thread.Sleep(1);
}
if (!failed) failed = st[2] == StatusFailed;
}
finally { s.Call("SetBrowserDelay", 0); s.Call("SetBrowserFolder", (string?)null); }
string[] calls = BrowserCalls.ToArray();
W($"forced create failure: state [{string.Join(",", st)}]; browser calls {calls.Length}; chat: {string.Join(" | ", ChatLines)}");
W($"forced create failure: state [{string.Join(",", st)}]; {samples} samples, {failedNoFlag} showed Failed without OpenedInBrowser; browser calls {calls.Length}; chat: {string.Join(" | ", ChatLines)}");
if (!failed) Fail("CreateFailureFallsBack", "status " + st[2]);
else if (failedNoFlag > 0) Fail("CreateFailureFallsBack", $"{failedNoFlag} of {samples} samples showed Failed without OpenedInBrowser (a polling plugin would open the page twice)");
else if (st[4] != CodeCreateFailed) Fail("CreateFailureFallsBack", "error " + st[4] + ", want CreateFailed");
else if ((st[3] & StateOpenedInBrowser) == 0) Fail("CreateFailureFallsBack", "OpenedInBrowser flag not set");
else if (calls.Length != 1 || Redact(calls[0]) != "https://panel.test/launch.html") Fail("CreateFailureFallsBack", $"{calls.Length} browser call(s)");