Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Windows crash on start or stop scan #143

Merged
merged 4 commits into from
Feb 25, 2025

Conversation

rohitsangwan01
Copy link
Contributor

@rohitsangwan01 rohitsangwan01 commented Feb 24, 2025

PR Type

Bug fix, Enhancement


Description

  • Fixed crashes on Windows during start or stop scan operations.

  • Improved error handling for Bluetooth scanning processes.

  • Added utility functions to convert watcher statuses to strings.

  • Updated CHANGELOG to reflect the fixes and improvements.


Changes walkthrough 📝

Relevant files
Bug fix
universal_ble_plugin.cpp
Fix crashes and improve Bluetooth scan handling                   

windows/src/universal_ble_plugin.cpp

  • Fixed crash when starting scan with unavailable Bluetooth.
  • Enhanced error handling for StartScan and StopScan methods.
  • Added handling for StoppingScan state in StartScan.
  • Improved disposeDeviceWatcher to handle additional watcher states.
  • +59/-19 
    Enhancement
    universal_ble_plugin.h
    Add utility functions and new watcher tokens                         

    windows/src/universal_ble_plugin.h

  • Added new event tokens for watcher states.
  • Introduced utility functions to convert watcher statuses to strings.
  • Updated class members to handle new watcher states.
  • +40/-0   
    Documentation
    CHANGELOG.md
    Update CHANGELOG with crash fixes                                               

    CHANGELOG.md

  • Documented fixes for Windows crashes during scanning.
  • Updated version 0.17.0 with new changes.
  • +1/-0     

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @rohitsangwan01 rohitsangwan01 marked this pull request as ready for review February 24, 2025 13:50
    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Resource Cleanup

    The bluetoothLEWatcherReceivedToken is revoked before stopping the watcher in StopScan(). This order should be reversed to prevent potential race conditions - stop the watcher first, then revoke the token.

    bluetoothLEWatcher.Received(bluetoothLEWatcherReceivedToken);
    bluetoothLEWatcher.Stop();
    Error Handling

    The catch block for StopScan() returns a generic "Failed to Stop" message for unknown exceptions, which may hide important error details needed for debugging. Consider logging more details about the exception.

    catch (...)
    {
      return FlutterError("Failed to Stop");
    }

    Copy link

    codiumai-pr-agent-free bot commented Feb 24, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Add switch statement default case

    Add default case in switch statements to handle potential future enum values and
    avoid undefined behavior.

    windows/src/universal_ble_plugin.h [184-199]

     switch (result)
     {
     case DeviceWatcherStatus::Created:
         return "Created";
     case DeviceWatcherStatus::Aborted:
         return "Aborted";
     case DeviceWatcherStatus::EnumerationCompleted:
         return "EnumerationCompleted";
     case DeviceWatcherStatus::Started:
         return "Started";
     case DeviceWatcherStatus::Stopped:
         return "Stopped";
     case DeviceWatcherStatus::Stopping:
         return "Stopping";
    +default:
    +    return "Unknown";
     }
    -return "";
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    __

    Why: Adding a default case is a good defensive programming practice that helps handle potential future enum values and prevents undefined behavior, improving code robustness and maintainability.

    Low
    Remove commented debug logging code

    Remove commented-out debug logging statements to maintain clean production code.
    If logging is needed, implement proper logging system instead of using commented
    std::cout.

    windows/src/universal_ble_plugin.cpp [143-145]

     DeviceWatcherStatus deviceWatcherStatus = deviceWatcher.Status();
    -// std::cout << "DeviceWatcherState: " << DeviceWatcherStatusToString(deviceWatcherStatus) << std::endl;
     // DeviceWatcher can only start if its in Created, Stopped, or Aborted state
    • Apply this suggestion
    Suggestion importance[1-10]: 2

    __

    Why: While removing commented-out code improves code cleanliness, this is a minor stylistic change with minimal impact on functionality. The commented logs might be useful for future debugging.

    Low
    • Update

    Copy link
    Contributor

    @fotiDim fotiDim left a comment

    Choose a reason for hiding this comment

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

    @rohitsangwan01 works for me. Let's cleanup the comments and merge.

    @rohitsangwan01
    Copy link
    Contributor Author

    @fotiDim I left those comments intentionally, those are needed for debugging if required in future

    @fotiDim fotiDim merged commit f0c38f7 into main Feb 25, 2025
    1 check passed
    @fotiDim fotiDim deleted the fix-windows-crash-on-start-or-stop-scan branch February 25, 2025 12:43
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants