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

Avoid taking an exclusive lock on Litra devices on macOS #93

Merged
merged 1 commit into from
Oct 3, 2024

Conversation

timrogers
Copy link
Owner

@timrogers timrogers commented Oct 3, 2024

Currently, when a Litra handle device is opened with open(), on macOS we take an exclusive lock on the device, which stops it being managed by other applications.

This isn't a problem if the device handle is quickly closed again (e.g. if the application exits), but it can be annoying if you have a long-running application that holds onto the device handle for a long time (e.g. litra auto-toggle).

As an example, if you run litra auto-toggle and then try to turn the device on or off with litra on or litra off in another shell, it would fail with an error:

HID error occurred: hidapi error: hid_open_path: failed to open IOHIDDevice from mach entry: (0xE00002C5) (iokit/common) exclusive access and device already open

This switches to taking a non-exclusive lock on macOS, affecting use of this code as a Rust library and our long-running auto-toggle command.


Important

On macOS, Litra::new() now avoids exclusive locks, allowing concurrent device access.

  • Behavior:
    • On macOS, Litra::new() now sets set_open_exclusive(false) to avoid exclusive locks on devices.
    • Updates open() docstring in Device to indicate non-exclusive mode on macOS.
  • Code:
    • Adds hidapi.set_open_exclusive(false); in Litra::new() for macOS.

This description was created by Ellipsis for a945f6c. It will automatically update as commits are pushed.

Summary by CodeRabbit

  • New Features

    • Enhanced support for macOS, allowing multiple applications to access devices simultaneously.
  • Documentation

    • Improved documentation for the open method, clarifying non-exclusive mode behavior on macOS.

Currently, when a Litra handle device is opened with `open()`,
on macOS we take an exclusive lock on the device, which stops
it being managed by other applications.

This isn't a problem if the device handle is quickly closed
again (e.g. if the application exits), but it can be annoying if
you have a long-running application that holds onto the device
handle for a long time (e.g. `litra auto-toggle`).

As an example, if you run `litra auto-toggle` and then try to
turn the device on or off with `litra on` or `litra off` in
another shell, it would fail with an error:

> HID error occurred: hidapi error: hid_open_path: failed to open IOHIDDevice from mach entry: (0xE00002C5) (iokit/common) exclusive access and device already open

This switches to taking a non-exclusive lock on macOS, affecting
use of this code as a Rust library and our long-running
`auto-toggle` command.
Copy link

coderabbitai bot commented Oct 3, 2024

Walkthrough

The changes in this pull request involve updates to the Litra library, specifically enhancing the initialization of the Litra context and device operations for macOS. The new method now includes a conditional compilation directive that allows devices to be opened in non-exclusive mode on macOS, enabling simultaneous access by multiple applications. Additionally, the documentation for the open method has been improved to clarify this behavior. No public method signatures or core functionality have been altered.

Changes

File Change Summary
src/lib.rs - Updated new method in Litra to support non-exclusive mode for macOS.
- Enhanced documentation for open method in Device to specify non-exclusive mode on macOS.

Possibly related PRs

  • Add macOS support for the auto-toggle command #86: The changes in this PR also focus on enhancing functionality for macOS, specifically adding support for the auto-toggle command, which aligns with the main PR's updates for macOS device handling in the Litra library.

🐇 In the land of code where rabbits play,
The Litra hops in a brand new way!
With macOS now sharing its device,
Multiple friends can access—oh, how nice!
Documentation shines, guiding the way,
A joyful leap into a brighter day! 🌟


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

Copy link

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

👍 Looks good to me! Reviewed everything up to a945f6c in 9 seconds

More details
  • Looked at 25 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. src/lib.rs:183
  • Draft comment:
    The documentation comment for the open method should be updated to reflect that the device is opened in non-exclusive mode on macOS. This is important for users to understand the behavior change.
  • Reason this comment was not posted:
    Confidence changes required: 50%
    The PR changes the lock mode to non-exclusive on macOS, which is correctly implemented using conditional compilation. However, the documentation comment for the open method should be updated to reflect this change.

Workflow ID: wflow_DXUwniNG0ARIfBij


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
src/lib.rs (1)

183-183: Approved: Clear documentation update for macOS behavior

The added note about non-exclusive mode on macOS is accurate and consistent with the implementation change. This provides valuable information to users about platform-specific behavior.

Consider rephrasing slightly for even more clarity:

- /// device status. On macOS, this will open the device in non-exclusive mode.
+ /// device status. Note: On macOS, the device will be opened in non-exclusive mode.

This minor change emphasizes the platform-specific nature of the behavior.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 6f3db81 and a945f6c.

📒 Files selected for processing (1)
  • src/lib.rs (2 hunks)
🔇 Additional comments (2)
src/lib.rs (2)

52-55: Excellent implementation of non-exclusive mode for macOS

This change directly addresses the PR objective by setting the HidApi to open devices in non-exclusive mode on macOS. The use of the conditional compilation directive ensures that this behavior is specific to macOS, maintaining compatibility with other platforms.


Line range hint 1-483: Summary: Successful implementation of non-exclusive locks on macOS

The changes in this file effectively address the PR objective of avoiding exclusive locks on Litra devices on macOS. The implementation is clean, well-integrated, and properly documented. These modifications enhance the usability of the code as a Rust library and improve the functionality of the auto-toggle command without affecting other platforms or the overall structure of the library.

Key points:

  1. Non-exclusive mode is set for macOS in the Litra::new() method.
  2. The Device::open() method documentation is updated to reflect the macOS-specific behavior.
  3. The changes are minimal and focused, maintaining the integrity of the existing codebase.

@timrogers timrogers merged commit 02624a2 into main Oct 3, 2024
11 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Oct 3, 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.

1 participant