Skip to content

fix(windows): clear IsRunning when the watcher reports Stopped - #78

Merged
framinosona merged 1 commit into
mainfrom
framinosona/windows-scanner-stop
Nov 5, 2026
Merged

framinosona merged 1 commit into
mainfrom
framinosona/windows-scanner-stop

Conversation

@framinosona

@framinosona framinosona commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Summary

StopScanningAsync and StopScanningIfNeededAsync threw ScannerFailedToStopException on Windows although the scan had stopped, and disposing a running scanner crashed the process with an unhandled exception. The scanner now sets IsRunning to false when the watcher reports Stopped.

Why

IsRunning is refreshed only by the 2-second ticker. The watcher's Stopped callback completed the stop task right away, but StopScanningAsync then checked IsRunning, which still read true until the next tick, and threw. Disposing a running scanner stops it first and hit the same exception.

Change Type

  • Feature
  • Bug fix
  • Refactor (refa)
  • Docs only
  • CI/build/tooling

Affected Areas

  • Abstractions
  • Core
  • Platform: Android
  • Platform: iOS/macOS
  • Platform: Windows
  • Platform: DotNetCore fallback
  • Facade (Bluetooth.Maui)
  • Documentation

Behavior And Compatibility

  • Public API changed
  • Exception behavior changed
  • DI registration behavior changed
  • Capability matrix impact
  • No externally visible behavior change

Stopping a running scanner on Windows no longer throws. Nothing else changes for callers.

Platform Notes

  • Windows only, one callback in WindowsBluetoothScanner. The base class is untouched, so the other platforms behave as before.
  • Not looked at here: on Windows the same tick-only refresh decides when a start completes. I did not measure it.

Tests

  • Unit tests added/updated
  • Integration/smoke tests added/updated
  • Manual platform validation performed
  • Not applicable (explain)

Validation notes:

  • The repository has no test project, so there are no automated tests in this PR.
  • Headless console harness (not in the PR) on a Windows 11 PC with an Intel AX201: scan for 6 s, then 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.

Scenario Before This PR
Scan 6 s, then stop (3 runs each) threw ScannerFailedToStopException 3 of 3 0 of 3
Same on scans of 20 to 45 s, earlier runs on the unfixed code threw 6 of 6 not run
Process exit with the scanner still running (dispose) unhandled exception clean exit

Documentation

  • Docs updated in same PR
  • Capability claims verified against implementation
  • Links validated
  • Not applicable (explain)

No documented behavior changes.

Logging / Diagnostics

  • New logs added
  • EventId range validated
  • No logging changes

Risks And Follow-ups

Risk level:

  • Low
  • Medium
  • High

Known limitations or deferred follow-ups:

  • The Stopped event also fires when the watcher is stopped by the system with an error; that path is unchanged and still reports OnStopFailed.

Checklist

  • I reviewed Docs/Best-Practices/Contribution-DoD.md
  • Commit header follows type (scope): short imperative and is <= 72 chars
  • Commit type is one of: feat, fix, refa, perf, docs, ci, chore, test, build
  • Commit body is 1-2 factual sentences (what/why), no emojis, refs, or co-authors
  • If architectural behavior changed, an ADR was added or updated under Docs/Architecture/ADR

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
framinosona requested review from TheRDavid and a balanced review from Copilot November 5, 2026 20:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 = false before explicitly calling OnStopSucceeded().
  • 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;
@framinosona
framinosona merged commit bd93d6c into main Nov 5, 2026
10 checks passed
@framinosona
framinosona deleted the framinosona/windows-scanner-stop branch November 5, 2026 21:55
@framinosona

Copy link
Copy Markdown
Member Author

Copilot's comment on the stale cached status is addressed in #80 (the scanner now reads the native watcher's status). I could not reproduce the race on hardware (40 start/stop cycles per build, no failure either way), so #80 is a correction from reading the code with a no-regression check.

@framinosona

Copy link
Copy Markdown
Member Author

Update: Copilot's comment above was right. With a 600 ms gap between cycles the race does reproduce on main (3, 3 and 4 of 12 restart cycles delivered nothing in three runs; 0, 0, 0 with #80). Details in #80.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants