From fa73371ede85aad26502bafffe67687aaa7e5962 Mon Sep 17 00:00:00 2001 From: Y-Ploni <7353755@gmail.com> Date: Sun, 8 Nov 2026 04:37:15 +0200 Subject: [PATCH] fix(perf): honor the monotonic deadline in Windows idle controls A single Start-Sleep can return before the Stopwatch duration required by the control. Wait for the rounded-up remainder before releasing the child, without changing the 1-second or 35-second assertions or exit evidence. Add isolated clock and early-wake regressions; retain the actual native process, provider-error and cleanup controls. Rebase PR #4503 onto dev after #4428; no source changes. Original implementation: 8e95f580f399e0daa5a92a1f802e2746c069999f. Co-authored-by: palmoni5 --- .../installed_plugin_windows_control.psm1 | 11 +++++- .../test_installed_plugin_windows_control.ps1 | 34 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/tool/perf_probe/installed_plugin_windows_control.psm1 b/tool/perf_probe/installed_plugin_windows_control.psm1 index 1c5959d1fb..c16bfa180b 100644 --- a/tool/perf_probe/installed_plugin_windows_control.psm1 +++ b/tool/perf_probe/installed_plugin_windows_control.psm1 @@ -39,6 +39,15 @@ function Read-InstalledPluginControlProcessTime { return $Process.$Property.ToUniversalTime() } +function Wait-InstalledPluginControlIdle { + param([Parameter(Mandatory)]$Clock, [ValidateRange(0, 40)][int]$Seconds) + # A Windows wait can return before the monotonic deadline. + $deadlineMs = $Seconds * 1000.0 + while (($remainingMs = $deadlineMs - $Clock.Elapsed.TotalMilliseconds) -gt 0) { + Start-Sleep -Milliseconds ([int][Math]::Ceiling($remainingMs)) + } +} + function Invoke-InstalledPluginExitControl { param([Parameter(Mandatory)][string]$Directory, [Parameter(Mandatory)][string]$ReportPath, [ValidateRange(0, 40)][int]$IdleSeconds = 0, @@ -89,7 +98,7 @@ exit 37 Wait-InstalledPluginProcessEvent -Trace $result.trace -Kind Start -ProcessId $child.Id $result.idle.startedAtUtc = [datetime]::UtcNow $idleClock = [Diagnostics.Stopwatch]::StartNew() - if ($IdleSeconds -gt 0) { Start-Sleep -Seconds $IdleSeconds } + Wait-InstalledPluginControlIdle -Clock $idleClock -Seconds $IdleSeconds $result.idle.elapsedMs = $idleClock.Elapsed.TotalMilliseconds $result.idle.finishedAtUtc = [datetime]::UtcNow [IO.File]::WriteAllText($releasePath, 'observed') diff --git a/tool/perf_probe/test_installed_plugin_windows_control.ps1 b/tool/perf_probe/test_installed_plugin_windows_control.ps1 index 10453116f1..af56ab213e 100644 --- a/tool/perf_probe/test_installed_plugin_windows_control.ps1 +++ b/tool/perf_probe/test_installed_plugin_windows_control.ps1 @@ -11,6 +11,40 @@ function Assert-Equal($Expected, $Actual, [string]$Message) { } try { + $timingModule = Import-Module $modulePath -Force -PassThru + try { + $timingResults = & $timingModule { + function script:Start-Sleep { + param([int]$Milliseconds) + if ($Milliseconds -le 0) { throw 'The idle deadline must not busy-wait.' } + $script:idleSleepRequests.Add($Milliseconds) + if ($script:idleWakeTimes.Count -eq 0) { throw 'The control slept after its deadline.' } + $script:idleClock.Elapsed.TotalMilliseconds = $script:idleWakeTimes.Dequeue() + } + foreach ($idleCase in @( + @{ name = 'zero'; seconds = 0; wakes = @(); expected = '' }, + @{ name = 'early'; seconds = 1; wakes = @(984.5, 999.75, 1000.125); expected = '1000,16,1' }, + @{ name = 'overshoot'; seconds = 1; wakes = @(1040.0); expected = '1000' }, + @{ name = 'native-idle'; seconds = 35; wakes = @(34999.5, 35000.25); expected = '35000,1' } + )) { + $script:idleClock = @{ Elapsed = @{ TotalMilliseconds = 0.0 } } + $script:idleSleepRequests = [Collections.Generic.List[int]]::new() + $script:idleWakeTimes = [Collections.Generic.Queue[double]]::new() + foreach ($wake in $idleCase.wakes) { $script:idleWakeTimes.Enqueue($wake) } + Wait-InstalledPluginControlIdle -Clock $script:idleClock -Seconds $idleCase.seconds + @{ + name = $idleCase.name; seconds = $idleCase.seconds; expected = $idleCase.expected + requests = $script:idleSleepRequests -join ','; elapsedMs = $script:idleClock.Elapsed.TotalMilliseconds + remainingWakes = $script:idleWakeTimes.Count + } + } + } + } finally { Remove-Module $timingModule } + foreach ($timing in $timingResults) { + Assert-Equal $timing.expected $timing.requests "The $($timing.name) deadline waits only for the rounded-up remainder" + Assert-Equal $true ($timing.elapsedMs -ge $timing.seconds * 1000) "The $($timing.name) deadline cannot release its child early" + Assert-Equal 0 $timing.remainingWakes "The $($timing.name) deadline consumes every early wake" + } foreach ($mode in @('pass', 'idle', 'missing-stop', 'provider-error', 'throw-stop', 'invalid-report', 'foreign-report', 'startup-error', 'cleanup-failure', 'oversized', 'save-error', 'primary-and-save-error')) { $directory = Join-Path $temporary $mode New-Item -ItemType Directory -Path $directory | Out-Null