From 765be9c11256b2fbd2c7c44830da0b71c4c82123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Thu, 5 Nov 2026 14:39:51 -0300 Subject: [PATCH 1/3] Fix two races in the web engine that made tests flaky (#1130, #1132) A frame that goes between the count and the read is FRAME_NOT_FOUND, not an IndexOutOfRangeException or a wait for the frame. A deadline that fires with Playwright's file-chooser timeout reads as NOT_ACTIONABLE. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Ls2e1yaZFgZ3FEJkYScfkK --- src/E2E/Engine/WebEngine.cs | 39 ++++++++++++++++--- .../Actions/DispatchLocatorActionTests.cs | 24 +++++++++++- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/E2E/Engine/WebEngine.cs b/src/E2E/Engine/WebEngine.cs index c01368e7f..5b0d5245e 100644 --- a/src/E2E/Engine/WebEngine.cs +++ b/src/E2E/Engine/WebEngine.cs @@ -1404,12 +1404,20 @@ private async Task FrameAsync(IReadOnlyList frames, OperationBud throw new EngineException(EngineErrorCodes.FrameAmbiguous, $"{count} frames match {selector}", retryable: false); } - refs.Add((await budget.WithinAsync(element.EvaluateAllAsync(PageScript.RefsOrForce, forcing.Mark)).ConfigureAwait(false))[0]); + // The iframe can go between the count and this read: it then lists nothing, as a count of zero says. + var listed = await budget.WithinAsync(element.EvaluateAllAsync(PageScript.RefsOrForce, forcing.Mark)).ConfigureAwait(false); + refs.Add(listed.Length > 0 ? listed[0] : throw new EngineException(EngineErrorCodes.FrameNotFound, $"no frame matches {selector}", retryable: true)); scope = scope is null ? Page.FrameLocator(selector) : scope.FrameLocator(selector); } - // A selector counts one element; only an iframe is a frame boundary. - if (!await budget.WithinAsync(element!.EvaluateAsync(PageScript.IsFrameElement, null, new LocatorEvaluateOptions { Timeout = budget.PlaywrightTimeout })).ConfigureAwait(false)) + // A selector counts one element; only an iframe is a frame boundary. Read without waiting for the element: an iframe that went since the count is no frame, not an element to wait for. + var isFrame = await budget.WithinAsync(element!.EvaluateAllAsync($"(els) => els.map({PageScript.IsFrameElement})")).ConfigureAwait(false); + if (isFrame.Length == 0) + { + throw new EngineException(EngineErrorCodes.FrameNotFound, $"no frame matches {frames[^1]}", retryable: true); + } + + if (!isFrame[0]) { throw new EngineException(EngineErrorCodes.FrameNotFound, $"{frames[^1]} matches an element that is not an iframe", retryable: true); } @@ -3619,17 +3627,22 @@ private async Task UploadFilesAsync(IElementHandle element, IReadOnlyList NoFileChooser(null)).ConfigureAwait(false); } catch (System.TimeoutException ex) { - throw new EngineException(EngineErrorCodes.NotActionable, "upload clicked the control but it opened no file chooser", retryable: false, ex); + throw NoFileChooser(ex); } budget.ThrowIfExpired(); await chooser.SetFilesAsync(paths, new FileChooserSetFilesOptions { Timeout = budget.PlaywrightTimeout }).ConfigureAwait(false); } + private static EngineException NoFileChooser(Exception? cause) => + new(EngineErrorCodes.NotActionable, "upload clicked the control but it opened no file chooser", retryable: false, cause); + /// How often a check or uncheck reads the control's state after the click. private static readonly TimeSpan CheckPollInterval = TimeSpan.FromMilliseconds(100); @@ -4162,6 +4175,7 @@ internal sealed class OperationBudget private readonly string _label; private readonly CancellationToken _cancellationToken; private readonly TimeProvider _clock; + private volatile Func? _atDeadline; // startedAt is when the operation began, on the clock, when engine work such as a reconnect already spent part of its budget. public OperationBudget(TimeSpan timeout, string label, CancellationToken cancellationToken, TimeProvider? clock = null, long? startedAt = null) @@ -4184,6 +4198,19 @@ public float PlaywrightTimeout } } + /// + /// Awaits a call whose own timeout is the lead ahead of the deadline. If the deadline passes first, because both fired in one stall + /// of a loaded machine, it ends as answers, not as OPERATION_TIMEOUT. The answer stays once + /// the call fails, and goes when it succeeds, so a later deadline is a plain timeout again. + /// + public async Task AwaitingAsync(Task call, Func atDeadline) + { + _atDeadline = atDeadline; + var result = await call.ConfigureAwait(false); + _atDeadline = null; + return result; + } + /// Waits for until the deadline, then abandons it as OPERATION_TIMEOUT. public async Task WithinAsync(Task call) { @@ -4243,7 +4270,7 @@ private TimeSpan Remaining() return left > TimeSpan.Zero ? left : throw Timeout(); } - private EngineException Timeout() => new(EngineErrorCodes.OperationTimeout, _label + " timed out", retryable: false); + private EngineException Timeout() => _atDeadline?.Invoke() ?? new(EngineErrorCodes.OperationTimeout, _label + " timed out", retryable: false); } /// diff --git a/tests/E2E.Tests/Actions/DispatchLocatorActionTests.cs b/tests/E2E.Tests/Actions/DispatchLocatorActionTests.cs index 085eba3ad..dbc074e6f 100644 --- a/tests/E2E.Tests/Actions/DispatchLocatorActionTests.cs +++ b/tests/E2E.Tests/Actions/DispatchLocatorActionTests.cs @@ -155,7 +155,21 @@ public async Task Reports_a_failed_click_without_leaving_the_chooser_wait_unhand { // A disabled button never becomes clickable, so the click fails while the chooser wait is still pending. var unobserved = new List(); - void OnUnobserved(object? sender, UnobservedTaskExceptionEventArgs args) => unobserved.Add(args.Exception.ToString()); + void OnUnobserved(object? sender, UnobservedTaskExceptionEventArgs args) + { + lock (unobserved) + { + unobserved.Add(args.Exception.ToString()); + } + } + + // The listener is process-wide: finalize what earlier tests left behind before it starts, so only this test's tasks reach it. + for (var round = 0; round < 2; round++) + { + GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true); + GC.WaitForPendingFinalizers(); + } + TaskScheduler.UnobservedTaskException += OnUnobserved; try { @@ -182,7 +196,13 @@ await RunAsync( TaskScheduler.UnobservedTaskException -= OnUnobserved; } - Assert.DoesNotContain(unobserved, text => text.Contains("filechooser", StringComparison.OrdinalIgnoreCase)); + List leaked; + lock (unobserved) + { + leaked = unobserved.Where(text => text.Contains("filechooser", StringComparison.OrdinalIgnoreCase)).ToList(); + } + + Assert.True(leaked.Count == 0, "An unobserved task exception names the file chooser:\n" + string.Join("\n---\n", leaked)); } private static async Task FindByNameAsync(IEngineSession session, string name) From b5f50f1625e9f48cfaeb4c2f2dc1f912f2b8661c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Thu, 5 Nov 2026 14:55:19 -0300 Subject: [PATCH 2/3] Scope the deadline answer to the chooser wait and name the frame check's script Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Ls2e1yaZFgZ3FEJkYScfkK --- src/E2E/Engine/PageScript.cs | 6 +++--- src/E2E/Engine/WebEngine.cs | 34 +++++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/E2E/Engine/PageScript.cs b/src/E2E/Engine/PageScript.cs index a07273b36..4d5003cfb 100644 --- a/src/E2E/Engine/PageScript.cs +++ b/src/E2E/Engine/PageScript.cs @@ -1199,9 +1199,9 @@ internal static class PageScript } """; - // Whether an element is a frame, the only element a frame locator can enter. - public const string IsFrameElement = """ - (el) => el.tagName === "IFRAME" || el.tagName === "FRAME" + // Whether each element is a frame, the only element a frame locator can enter, read without waiting for any: an element that has gone since it was counted lists nothing. + public const string AreFrameElements = """ + (els) => els.map((el) => el.tagName === "IFRAME" || el.tagName === "FRAME") """; // Where an iframe element's content box, the box its document's viewport diff --git a/src/E2E/Engine/WebEngine.cs b/src/E2E/Engine/WebEngine.cs index 5b0d5245e..158724f9a 100644 --- a/src/E2E/Engine/WebEngine.cs +++ b/src/E2E/Engine/WebEngine.cs @@ -1396,7 +1396,7 @@ private async Task FrameAsync(IReadOnlyList frames, OperationBud var count = await budget.WithinAsync(element.CountAsync()).ConfigureAwait(false); if (count == 0) { - throw new EngineException(EngineErrorCodes.FrameNotFound, $"no frame matches {selector}", retryable: true); + throw NoFrame(selector); } if (count > 1) @@ -1406,15 +1406,15 @@ private async Task FrameAsync(IReadOnlyList frames, OperationBud // The iframe can go between the count and this read: it then lists nothing, as a count of zero says. var listed = await budget.WithinAsync(element.EvaluateAllAsync(PageScript.RefsOrForce, forcing.Mark)).ConfigureAwait(false); - refs.Add(listed.Length > 0 ? listed[0] : throw new EngineException(EngineErrorCodes.FrameNotFound, $"no frame matches {selector}", retryable: true)); + refs.Add(listed.Length > 0 ? listed[0] : throw NoFrame(selector)); scope = scope is null ? Page.FrameLocator(selector) : scope.FrameLocator(selector); } // A selector counts one element; only an iframe is a frame boundary. Read without waiting for the element: an iframe that went since the count is no frame, not an element to wait for. - var isFrame = await budget.WithinAsync(element!.EvaluateAllAsync($"(els) => els.map({PageScript.IsFrameElement})")).ConfigureAwait(false); + var isFrame = await budget.WithinAsync(element!.EvaluateAllAsync(PageScript.AreFrameElements)).ConfigureAwait(false); if (isFrame.Length == 0) { - throw new EngineException(EngineErrorCodes.FrameNotFound, $"no frame matches {frames[^1]}", retryable: true); + throw NoFrame(frames[^1]); } if (!isFrame[0]) @@ -1437,6 +1437,8 @@ private async Task FrameAsync(IReadOnlyList frames, OperationBud return refs[^1]!; } + private static EngineException NoFrame(string selector) => new(EngineErrorCodes.FrameNotFound, $"no frame matches {selector}", retryable: true); + /// /// The refs of the nodes the elements names have, inside the frame the chain names or the page. An element /// the observation does not list is marked in the page and counted in : the next observation lists it. @@ -3627,8 +3629,8 @@ private async Task UploadFilesAsync(IElementHandle element, IReadOnlyList NoFileChooser(null)).ConfigureAwait(false); } catch (System.TimeoutException ex) @@ -4200,15 +4202,25 @@ public float PlaywrightTimeout /// /// Awaits a call whose own timeout is the lead ahead of the deadline. If the deadline passes first, because both fired in one stall - /// of a loaded machine, it ends as answers, not as OPERATION_TIMEOUT. The answer stays once - /// the call fails, and goes when it succeeds, so a later deadline is a plain timeout again. + /// of a loaded machine, the deadline that wraps this call () ends it as + /// answers, not as OPERATION_TIMEOUT. The answer stays when the call fails on its timeout, and goes on success or another + /// failure, so a later deadline is a plain timeout again. /// public async Task AwaitingAsync(Task call, Func atDeadline) { _atDeadline = atDeadline; - var result = await call.ConfigureAwait(false); - _atDeadline = null; - return result; + try + { + var result = await call.ConfigureAwait(false); + _atDeadline = null; + return result; + } + catch (Exception ex) when (ex is not System.TimeoutException) + { + // Another failure ends the operation on its own answer; a deadline after it is a plain timeout. + _atDeadline = null; + throw; + } } /// Waits for until the deadline, then abandons it as OPERATION_TIMEOUT. From d56da0599df407e13bcf6e680d8fd658a4310b91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Thu, 5 Nov 2026 14:56:46 -0300 Subject: [PATCH 3/3] Shorten the AreFrameElements comment Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Ls2e1yaZFgZ3FEJkYScfkK --- src/E2E/Engine/PageScript.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/E2E/Engine/PageScript.cs b/src/E2E/Engine/PageScript.cs index 4d5003cfb..afd89abe7 100644 --- a/src/E2E/Engine/PageScript.cs +++ b/src/E2E/Engine/PageScript.cs @@ -1199,7 +1199,7 @@ internal static class PageScript } """; - // Whether each element is a frame, the only element a frame locator can enter, read without waiting for any: an element that has gone since it was counted lists nothing. + // Whether each element is a frame, the only element a frame locator can enter. An element gone since the count lists nothing. public const string AreFrameElements = """ (els) => els.map((el) => el.tagName === "IFRAME" || el.tagName === "FRAME") """;