-
-
Notifications
You must be signed in to change notification settings - Fork 36
Fix crash when unpairing device while scanning #176
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @fotiDim, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request addresses a critical stability issue by preventing application crashes that could occur when a Bluetooth device is unpaired while a scan is actively in progress. The changes focus on adding defensive programming measures, specifically exception handling and null checks, to ensure the application gracefully handles unexpected states of device and advertisement data.
Highlights
- Crash Prevention: Implemented robust error handling in the
OnDeviceInfoReceivedmethod by wrapping property access in atry-catchblock. This prevents crashes that could occur if device information becomes invalid or stale, particularly during device unpairing while scanning. - Null Pointer Safety: Introduced null checks for
Advertisementdata andDeviceInformationobjects within theOnAdvertisementReceivedmethod. This proactively prevents crashes caused by attempting to access members of invalid or stale data structures during Bluetooth scanning.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request addresses a crash that occurs when a device is unpaired during a scan by introducing try-catch blocks and nullptr checks to handle stale device information. The changes are effective in preventing the crash. My review provides suggestions to enhance the robustness and maintainability of this new error handling code, such as using more specific exception types instead of a general catch(...) and refactoring repeated checks for better readability.
| catch (...) | ||
| { | ||
| std::cout << "DeviceWatcherErrorInParsing" << std::endl; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While adding a try-catch block is a good way to prevent the crash, catching all exceptions with ... can hide other potential issues. It would be better to catch the specific exception type you expect, which is likely winrt::hresult_error when dealing with stale WinRT objects. This makes the code safer and easier to debug. Also, the log message could be more descriptive.
catch (const winrt::hresult_error& e)
{
std::cout << "Failed to process device info, likely due to unpairing: " << to_string(e.message()) << std::endl;
}| std::string name = ""; | ||
| if (args.Advertisement() != nullptr) | ||
| name = to_string(args.Advertisement().LocalName()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are multiple if (args.Advertisement() != nullptr) checks in this function. To improve readability and avoid repeated calls to args.Advertisement(), you could cache the result in a local variable at the beginning of the function and use that variable for all subsequent checks and operations on the advertisement object. The ternary operator can also make this part more concise.
auto advertisement = args.Advertisement();
std::string name = advertisement ? to_string(advertisement.LocalName()) : "";
// TODO: Use `advertisement` in subsequent null checks in this function.| catch (...) | ||
| { | ||
| // Ignore errors from accessing stale DeviceInformation | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using catch (...) is a bit broad and can hide unrelated errors. It's better to catch the specific exception you expect to ignore, which is likely winrt::hresult_error for stale WinRT objects. This makes the code more robust.
catch (const winrt::hresult_error&)
{
// Ignore errors from accessing stale DeviceInformation
}9476e3c to
fa0a2c9
Compare
fa0a2c9 to
4c88a51
Compare
629d4a6 to
b52ca9e
Compare
e8deca3 to
01b4511
Compare
No description provided.