Fix Windows BSOD crash when USB UART devices are disconnected during port enumeration - #145
Draft
mrdevrobot with Copilot wants to merge 2 commits into
Draft
Fix Windows BSOD crash when USB UART devices are disconnected during port enumeration#145mrdevrobot with Copilot wants to merge 2 commits into
mrdevrobot with Copilot wants to merge 2 commits into
Conversation
…ort scanning Co-authored-by: lucafabbri <12503462+lucafabbri@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Windows Crash while connecting UART USB
Fix Windows BSOD crash when USB UART devices are disconnected during port enumeration
Sep 27, 2025
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.
This PR fixes critical Windows Blue Screen of Death (BSOD) crashes that occur when USB UART devices are physically disconnected and reconnected while
SerialPort.availablePortsis being called.Problem
Users reported system crashes on Windows when:
SerialPort.availablePortsThe crashes were traced to race conditions and unsafe operations in the Windows-specific libserialport implementation (
third_party/libserialport/windows.c) when accessing device handles that became invalid during hardware disconnection.Root Cause
The crashes occurred due to several unsafe operations:
Solution
Native Code Fixes (windows.c)
Handle Validation:
Buffer Initialization:
Registry Safety:
Device Instance Validation:
Dart-Level Safety Improvements
Added
SafePortScannerclass that provides:Testing
The fixes have been validated to ensure:
Backward Compatibility
All changes are fully backward compatible:
Files Changed
third_party/libserialport/windows.c- Core native fixes for Windowsexample/lib/safe_port_scanner.dart- Safer Dart implementationexample/lib/safe_main.dart- Example demonstrating safer patternsWINDOWS_CRASH_FIX.md- Comprehensive documentation of all fixesThis fix resolves a critical stability issue that could cause complete system crashes on Windows when working with USB serial devices.
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.