fix: replace Thread.Sleep with Task.Delay in async SD paths#226
Merged
Conversation
) The SD interface settle delay inside *Async methods of DaqifiStreamingDevice used Thread.Sleep, which blocked a thread-pool thread for the settle window and ignored the operation's CancellationToken. Added a Func<CancellationToken, Task> overload of ExecuteTextCommandAsync (refactored alongside the existing Action overload into a private async core) so setup lambdas can await Task.Delay(..., ct). Converted the four affected call sites in GetSdCardFilesAsync and DeleteSdCardFileAsync. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Review Summary by QodoReplace Thread.Sleep with Task.Delay in async SD operations
WalkthroughsDescription• Replace Thread.Sleep with Task.Delay in SD card async operations - Eliminates thread-pool thread blocking during 100ms settle delays - Honors CancellationToken during settle window instead of ignoring it • Add async overload of ExecuteTextCommandAsync accepting Func - Refactored both overloads into shared private ExecuteTextCommandCoreAsync core - Eliminates ~200 lines of duplicated consumer-swap/timeout logic • Add regression tests for cancellation during SD settle delays - GetSdCardFilesAsync_HonorsCancellationDuringSettleDelay validates early cancellation - DeleteSdCardFileAsync_HonorsCancellationDuringSettleDelay validates symmetric behavior Diagramflowchart LR
A["ExecuteTextCommandAsync<br/>Action overload"] --> C["ExecuteTextCommandCoreAsync<br/>shared private core"]
B["ExecuteTextCommandAsync<br/>Func async overload"] --> C
C --> D["Awaitable Task.Delay<br/>with CancellationToken"]
E["GetSdCardFilesAsync<br/>setup lambda"] --> D
F["DeleteSdCardFileAsync<br/>setup lambda"] --> D
G["Regression tests"] --> H["Verify cancellation<br/>propagates promptly"]
File Changes1. src/Daqifi.Core/Device/DaqifiDevice.cs
|
Code Review by Qodo
1.
|
Replace the timer-based CancellationTokenSource and <80ms wall-clock upper-bound assertions with deterministic cancellation: start the operation, let it suspend in the settle Task.Delay, then Cancel() and assert the OperationCanceledException propagates. Removes CI scheduler jitter flakiness flagged by Qodo on #226 without weakening the regression guarantee for #221. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced May 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #221.
Summary
DaqifiStreamingDevicehad 4Thread.Sleep(SD_INTERFACE_SETTLE_DELAY_MS)calls inside*Asyncsetup-action lambdas (GetSdCardFilesAsync,DeleteSdCardFileAsync). Each blocked a thread-pool thread for the 100ms settle window and ignored the operation'sCancellationToken.Func<CancellationToken, Task>overload ofExecuteTextCommandAsyncso setup lambdas canawait Task.Delay(..., ct). Refactored the existingActionoverload + new overload into a shared private async core to avoid duplicating the ~200-line consumer-swap/timeout body.async ct => { …; await Task.Delay(SD_INTERFACE_SETTLE_DELAY_MS, ct).ConfigureAwait(false); … }.The
Thread.SleepinWifiBridgeActivator.Activate()and the background-threadThread.Sleepcalls inMessageProducer/StreamMessageConsumerare intentionally untouched (per the issue).Test plan
dotnet test --framework net9.0— 995 passed, 0 failed, 2 skipped (real-hardware integration tests)dotnet test --framework net10.0— same resultdotnet build— 0 warnings, 0 errors (repo enforcesTreatWarningsAsErrors)GetSdCardFilesAsync_HonorsCancellationDuringSettleDelay— cancels the token 20ms in, assertsOperationCanceledExceptionwithin 80ms (well under the 100ms settle window). Would have failed underThread.SleepsinceThread.Sleepcannot be cancelled mid-sleep.DeleteSdCardFileAsync_HonorsCancellationDuringSettleDelay— symmetric.🤖 Generated with Claude Code