Skip to content

Prevent additional attempts at closing HID braille connections#16712

Merged
seanbudd merged 4 commits into
nvaccess:masterfrom
FelixGruetzmacher:FixHidMultipleClose
Jun 18, 2024
Merged

Prevent additional attempts at closing HID braille connections#16712
seanbudd merged 4 commits into
nvaccess:masterfrom
FelixGruetzmacher:FixHidMultipleClose

Conversation

@FelixGruetzmacher

@FelixGruetzmacher FelixGruetzmacher commented Jun 17, 2024

Copy link
Copy Markdown
Contributor

Link to issue number:

Fixes #16218

Summary of the issue:

NVDA could silently crash when a Braille HID device was disconnected/closed.

Description of user facing changes

No more crashes under the above conditions.

Description of development approach

Introduced a flag that tracks if the close() method has already been called, and don't perform the operation again.

Testing strategy:

Tried to reproduce the crash.

Known issues with pull request:

None.

Code Review Checklist:

  • Documentation:
    • Change log entry
    • User Documentation
    • Developer / Technical Documentation
    • Context sensitive help for GUI changes
  • Testing:
    • Unit tests
    • System (end to end) tests
    • Manual testing
  • UX of all users considered:
    • Speech
    • Braille
    • Low Vision
    • Different web browsers
    • Localization in other languages / culture than English
  • API is compatible with existing add-ons.
  • Security precautions taken.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of HID device closing to prevent errors.
    • Added an internal check to ensure the HID device is not closed multiple times.

@FelixGruetzmacher FelixGruetzmacher requested a review from a team as a code owner June 17, 2024 09:39
@coderabbitai

coderabbitai Bot commented Jun 17, 2024

Copy link
Copy Markdown
Contributor

Walkthrough

The update in hid.py involves changes to imports, the addition of an _isClosed attribute during initialization, and improved handling of the closure state in the Hid class. Specifically, the _isClosed attribute ensures the HID device status is tracked, enhancing the close method to prevent redundant operations on already closed devices.

Changes

Files Change Summary
source/hwIo/hid.py Updated imports to include IoBase and _isDebug from base; initialized _isClosed attribute; enhanced close method to check and set _isClosed.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Hid
    Client->>+Hid: new Hid()
    Hid-->>Client: Initialize (self._isClosed = False)
    Client->>+Hid: close()
    alt If self._isClosed is False
        Hid-->>+Client: Close the device (set self._isClosed = True)
    else If self._isClosed is True
        Hid-->>Client: Device already closed
    end
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Comment thread source/hwIo/hid.py
Comment on lines +322 to +328
if self._isClosed:
pass
super(Hid, self).close()
winKernel.closeHandle(self._file)
self._file = None
hidDll.HidD_FreePreparsedData(self._pd)
self._isClosed = True

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The conditional check in the close method effectively prevents multiple closures of the device. However, the use of pass in the conditional block is a bit misleading as it suggests that no action is taken when the device is already closed. It would be clearer to include a log message or a comment explaining that no further action is needed.

if self._isClosed:
-    pass
+    log.debug("Attempted to close an already closed device.")
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if self._isClosed:
pass
super(Hid, self).close()
winKernel.closeHandle(self._file)
self._file = None
hidDll.HidD_FreePreparsedData(self._pd)
self._isClosed = True
if self._isClosed:
log.debug("Attempted to close an already closed device.")
return
super(Hid, self).close()
winKernel.closeHandle(self._file)
self._file = None
hidDll.HidD_FreePreparsedData(self._pd)
self._isClosed = True

The sequence of operations in the close method is logically sound, ensuring resource cleanup and state update. However, consider checking and logging an error if HidD_FreePreparsedData fails, as this could lead to resource leaks.

if not hidDll.HidD_FreePreparsedData(self._pd):
+    log.error("Failed to free preparsed data.")
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if self._isClosed:
pass
super(Hid, self).close()
winKernel.closeHandle(self._file)
self._file = None
hidDll.HidD_FreePreparsedData(self._pd)
self._isClosed = True
if self._isClosed:
pass
super(Hid, self).close()
winKernel.closeHandle(self._file)
self._file = None
if not hidDll.HidD_FreePreparsedData(self._pd):
log.error("Failed to free preparsed data.")
self._isClosed = True

@AppVeyorBot

Copy link
Copy Markdown

See test results for failed build of commit 06715c35d6

@seanbudd seanbudd changed the title Introduced flag to prevent multiple closing of a HID device as this c… @coderabbitai title Jun 18, 2024
Comment thread source/hwIo/hid.py
@seanbudd

Copy link
Copy Markdown
Member

@coderabbitai please update the title

@coderabbitai

coderabbitai Bot commented Jun 18, 2024

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

@seanbudd, I noticed you requested an update to the PR title. Could you please specify the new title you'd like to use? Once you provide that, I can assist further in ensuring it reflects accurately in your documentation or discussions, but please note that I'm unable to directly change the PR title myself. You would need to update it in the GitHub interface.

@seanbudd seanbudd changed the title @coderabbitai title Prevent additional attempts at closing HID braille connections Jun 18, 2024
@seanbudd seanbudd merged commit 113374f into nvaccess:master Jun 18, 2024
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.

NVDA occasionally crashes when USB Braille display is disconnected

3 participants