Skip to content

Commit 42096ab

Browse files
Reduce CI test timeout and fix busy-spin in ReadScriptLogLineAsync
The `CanAttachScriptWithPathMappings` E2E test intermittently hung `windows-latest` CI for the full six-hour default — three of the last eleven `main` runs died this way, all the same test, interspersed with green runs (a classic flaky race, not a regression). None of the commits whose runs hung touched the debugger attach path. The hang mechanism lived in `ReadScriptLogLineAsync`: at EOF `StreamReader.ReadLineAsync()` completes *synchronously* with `null`, so the `while`/`await` polling loop never actually yielded. It busy-spun one CPU at 100%, which starved the scheduler so none of the existing cooperative safety nets — xUnit's `[SkippableFact(Timeout = 15000)]`, the 30s `debugTaskCts`, or `WaitForExitAsync` — could ever schedule their continuations. A flaky few-second race thus escalated into a six-hour wedge. Ironically the busy-loop landed in #2208, a PR meant to reduce flakiness, and lay dormant until #2251 added a Windows-racy attach test that actually hits the EOF spin. - Back off with `await Task.Delay(100, token)` on EOF so we yield instead of busy-spinning, and cap the whole read with a 15s linked CTS that throws a clear `TimeoutException` naming the log path. - Add `timeout-minutes: 15` to the `ci` job as a backstop so any future hang fails in 15 minutes instead of riding GitHub's 6-hour default. A normal run finishes well under that (Windows, the slowest, is ~12-14m). The underlying attach race (reflection-based wait for `Debug-Runspace` to subscribe) is still worth hardening, but it now fails fast instead of hanging. Drafted by Copilot (Claude Opus 4.8). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c3cd6e2 commit 42096ab

2 files changed

Lines changed: 47 additions & 19 deletions

File tree

‎.github/workflows/ci-test.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ jobs:
1414
matrix:
1515
os: [ windows-latest, macos-latest, ubuntu-latest ]
1616
runs-on: ${{ matrix.os }}
17+
# A normal run finishes in well under 15 minutes (Windows, the slowest, is
18+
# ~12-14 minutes); this caps a hung test at 15 minutes instead of letting it
19+
# ride GitHub's 6-hour default.
20+
timeout-minutes: 15
1721
env:
1822
DOTNET_NOLOGO: true
1923
DOTNET_GENERATE_ASPNET_CERTIFICATE: false

‎test/PowerShellEditorServices.Test.E2E/DebugAdapterProtocolMessageTests.cs‎

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -217,34 +217,58 @@ private string GenerateLoggingScript(params string[] logStatements)
217217
/// <summary>
218218
/// Reads the next output line from the test script log file. Useful in assertions to verify script progress against breakpointing.
219219
/// </summary>
220-
private async Task<string> ReadScriptLogLineAsync()
220+
private async Task<string> ReadScriptLogLineAsync(CancellationToken cancellationToken = default)
221221
{
222-
while (scriptLogReader is null)
222+
// Reading a log line should be near-instant, but the script we read
223+
// from is driven by the debugger and can fail to produce output (for
224+
// example if an attach or breakpoint never lands). Cap the wait so
225+
// the test fails fast with a clear message instead of spinning
226+
// forever -- a busy-spin here previously pegged the CPU and starved
227+
// xUnit's cooperative test timeout, hanging CI for the full six hours.
228+
using CancellationTokenSource timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
229+
timeoutCts.CancelAfter(TimeSpan.FromSeconds(15));
230+
CancellationToken token = timeoutCts.Token;
231+
232+
try
223233
{
224-
try
234+
while (scriptLogReader is null)
225235
{
226-
scriptLogReader = new StreamReader(
227-
new FileStream(
228-
testScriptLogPath,
229-
FileMode.OpenOrCreate,
230-
FileAccess.Read, // Because we use append, its OK to create the file ahead of the script
231-
FileShare.ReadWrite
232-
)
233-
);
236+
try
237+
{
238+
scriptLogReader = new StreamReader(
239+
new FileStream(
240+
testScriptLogPath,
241+
FileMode.OpenOrCreate,
242+
FileAccess.Read, // Because we use append, its OK to create the file ahead of the script
243+
FileShare.ReadWrite
244+
)
245+
);
246+
}
247+
catch (IOException) //Sadly there does not appear to be a xplat way to wait for file availability, but luckily this does not appear to fire often.
248+
{
249+
await Task.Delay(500, token);
250+
}
234251
}
235-
catch (IOException) //Sadly there does not appear to be a xplat way to wait for file availability, but luckily this does not appear to fire often.
252+
253+
// return valid lines only
254+
string nextLine = string.Empty;
255+
while (string.IsNullOrEmpty(nextLine))
236256
{
237-
await Task.Delay(500);
257+
nextLine = await scriptLogReader.ReadLineAsync(token); //Might return null if at EOF because we created it above but the script hasn't written to it yet
258+
if (string.IsNullOrEmpty(nextLine))
259+
{
260+
// At EOF waiting for the script to write more: yield and
261+
// back off so we don't busy-spin the CPU while polling.
262+
await Task.Delay(100, token);
263+
}
238264
}
265+
return nextLine;
239266
}
240-
241-
// return valid lines only
242-
string nextLine = string.Empty;
243-
while (nextLine is null || nextLine.Length == 0)
267+
catch (OperationCanceledException) when (timeoutCts.IsCancellationRequested && !cancellationToken.IsCancellationRequested)
244268
{
245-
nextLine = await scriptLogReader.ReadLineAsync(); //Might return null if at EOF because we created it above but the script hasn't written to it yet
269+
throw new TimeoutException(
270+
$"Timed out waiting for the test script to write a log line to '{testScriptLogPath}'.");
246271
}
247-
return nextLine;
248272
}
249273

250274
[Fact]

0 commit comments

Comments
 (0)