Repository navigation
fix(windows): clear IsRunning when the watcher reports Stopped - #78
Merged
Merged
Conversation
IsRunning was refreshed only by the periodic tick, so StopScanningAsync still read it as true after the Stopped callback and threw ScannerFailedToStopException. Disposing a running scanner failed the same way.
framinosona
requested review from
TheRDavid
and
a balanced review from Copilot
November 5, 2026 20:33
There was a problem hiding this comment.
🟡 Changes recommended
A race between the callback and cached-status polling can overwrite the stopped state, causing missed watcher restarts or ScannerFailedToStopException.
1 open issue
What changed in this PR
This PR updates the Windows BLE scanner to clear its running state when the watcher reports a successful stop, addressing false stop failures and disposal crashes.
Changes:
- Sets
IsRunning = falsebefore explicitly callingOnStopSucceeded(). - Adds a comment explaining why waiting for the periodic state refresh causes stop failures.
- Leaves shared scanner code and other platforms unchanged.
| File | Description |
|---|---|
| Bluetooth.Maui.Platforms.Win/Scanning/WindowsBluetoothScanner.cs | Clears running state in the successful watcher-stop callback. |
Review effort: Balanced
|
|
||
| // IsRunning is otherwise refreshed only by the periodic tick. Until then it would still read true, and the | ||
| // StopScanningAsync that is waiting for this callback would fail with ScannerFailedToStopException. | ||
| IsRunning = false; |
This was referenced Nov 5, 2026
Member
Author
Member
Author
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.

Summary
StopScanningAsyncandStopScanningIfNeededAsyncthrewScannerFailedToStopExceptionon Windows although the scan had stopped, and disposing a running scanner crashed the process with an unhandled exception. The scanner now setsIsRunningto false when the watcher reportsStopped.Why
IsRunningis refreshed only by the 2-second ticker. The watcher'sStoppedcallback completed the stop task right away, butStopScanningAsyncthen checkedIsRunning, which still read true until the next tick, and threw. Disposing a running scanner stops it first and hit the same exception.Change Type
refa)Affected Areas
Bluetooth.Maui)Behavior And Compatibility
Stopping a running scanner on Windows no longer throws. Nothing else changes for callers.
Platform Notes
WindowsBluetoothScanner. The base class is untouched, so the other platforms behave as before.Tests
Validation notes:
StopScanningIfNeededAsync.Test method, for reference. A throwaway headless .NET console program (not part of this PR and not published) ran on a Windows 11 PC with an Intel AX201 Bluetooth adapter. It was built from snapshots of the trees being compared, so the same scenario ran against each build, against one real peripheral at a time. No pairing or bonding was done. Scenario: start scanning, wait 6 s, call
StopScanningIfNeededAsync; and exit the process while the scanner is still running.ScannerFailedToStopException3 of 3Documentation
No documented behavior changes.
Logging / Diagnostics
Risks And Follow-ups
Risk level:
Known limitations or deferred follow-ups:
Stoppedevent also fires when the watcher is stopped by the system with an error; that path is unchanged and still reportsOnStopFailed.Checklist
type (scope): short imperativeand is <= 72 chars