Skip to content

fix multiple issues in the crates - #154

Merged
luytan merged 17 commits into
mainfrom
fix-multiple-issues
Aug 7, 2026
Merged

fix multiple issues in the crates#154
luytan merged 17 commits into
mainfrom
fix-multiple-issues

Conversation

@luytan

@luytan luytan commented Aug 6, 2026

Copy link
Copy Markdown
Member

Description

  • Broken config no longer crash-loops the daemon
  • CW_DIRENT now keyed by tid instead of pid
  • Switcheroo shim now emits PropertiesChanged when GPU list changes
  • CLI now exits with code 1 on error
  • set battery auto switch mode validates input before storing
  • GUI GPU List now refreshes on changes
  • GUI Setting toggles are now fetched on startup
  • Removed leftover println! in the GUI
  • remove unwrap in lsof

TODO

  • Copy-Paste this line

Checklist:

  • My code follows the style guidelines of this project (cargo fmt)
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the mdBook documentation
  • My changes generate no new warnings (clippy/clang)
  • New and existing unit tests pass locally with my changes (either use nix flake check or wait for the ci)

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • New Features
    • GPU lists and power information now refresh automatically when graphics hardware is added or removed.
    • Initial configuration values load immediately when connecting.
    • GPU property changes are reported automatically to connected components.
  • Bug Fixes
    • Invalid battery auto-switch modes are rejected safely.
    • Configuration updates use safer atomic writes and handle concurrent saves reliably.
    • Invalid configuration files fall back to defaults with a warning.
    • CLI failures now display an error and exit.
    • Existing GPU information is preserved when refreshes fail.
    • Process and GPU monitoring reliability has improved.

Walkthrough

The changes strengthen configuration and CLI error handling, add atomic configuration writes, emit daemon GPU-list signals, rebuild GUI GPU streams on hotplug events, preserve stale GUI state on fetch errors, and correct eBPF thread-key usage.

Changes

Runtime reliability and device updates

Layer / File(s) Summary
Configuration and error handling
crates/cardwire-daemon/src/file/config.rs, crates/cardwire-daemon/src/interface/config.rs, crates/cardwire-cli/src/main.rs, crates/cardwire-gui/src/helpers/dbus.rs, crates/cardwire-gui/src/subscription.rs, crates/cardwire-gui/src/app.rs
Invalid TOML uses default settings with a warning. Saves use temporary files and atomic replacement. Invalid battery modes return InvalidArgs. CLI D-Bus failures exit with status 1. Startup settings are emitted to the GUI. GPU fetch failures preserve the existing list.
Daemon GPU change signaling
crates/cardwire-daemon/src/interface/switcheroo.rs, crates/cardwire-daemon/src/daemon.rs, crates/cardwire-daemon/src/models.rs, crates/cardwire-daemon/src/interface/debug.rs, crates/cardwire-daemon/src/tasks/monitor_udev.rs, crates/cardwire-daemon/src/interface/gpu.rs
The daemon stores a SignalEmitter, centralizes GPU property calculations, and emits GPU property changes after successful GPU refreshes.
GUI GPU hotplug subscriptions
crates/cardwire-gui/src/subscription.rs
The GUI observes GPU object additions and removals, refreshes managed objects, emits initial power states, and rebuilds per-GPU streams.
eBPF thread keying
crates/cardwire-ebpf/src/main.rs
The getdents64 handlers use the current thread identifier for CW_DIRENT map operations.
GPU mode CI expectations
nix/ci-15gpu.nix, nix/ci-2gpu.nix, nix/ci-3gpu.nix
The CI scenarios now expect invalid integrated-mode and default-GPU blocking commands to fail, while hybrid mode succeeds.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ObjectManager
  participant GPUSubscription
  participant DebugInterface
  participant SwitcherooInterface
  participant SignalEmitter
  ObjectManager->>GPUSubscription: report GPU object change
  GPUSubscription->>GPUSubscription: refresh GPU list and rebuild streams
  DebugInterface->>SwitcherooInterface: emit_gpu_list_changed()
  SwitcherooInterface->>SignalEmitter: emit PropertiesChanged
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 3 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title is related to the changes but is too broad to identify the primary fixes. Use a specific title that names the main changes, such as daemon configuration handling and GPU list updates.
✅ Passed checks (3 passed)
Check name Status Explanation
Description check ✅ Passed The description summarizes the main changes and includes the required template sections and checklist.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

@luytan
luytan marked this pull request as ready for review August 6, 2026 17:09

@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: 8

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/cardwire-daemon/src/daemon.rs`:
- Around line 69-77: Update the SwitcherooInterface lookup in the daemon
initialization flow to log an error when interface() fails, while preserving the
existing signal_emitter assignment on success. Ensure the failure log includes
the lookup error and clearly indicates that GPU change notifications cannot be
emitted.

In `@crates/cardwire-daemon/src/file/config.rs`:
- Around line 83-101: Update Config::save_config to generate a unique temporary
path for each save, create it with exclusive create_new semantics, and use that
path for writing, syncing, renaming, and failure cleanup instead of the shared
cardwire.toml.tmp. Serialize concurrent saves when required to preserve update
order in the final configuration file.

In `@crates/cardwire-daemon/src/interface/switcheroo.rs`:
- Around line 28-43: Update emit_gpu_list_changed to acquire self.gpu_list’s
read guard once and derive HasDualGpu, NumGPUs, and GPUs through private helper
methods that accept the held guard. Replace the current independent
has_dual_gpu(), num_gpus(), and gpus() calls while preserving the existing
payload conversion and error handling.
- Around line 45-55: Update the PropertiesChanged emission in the switcheroo
interface to preserve a single grouped signal for all changed properties, while
invoking zbus::fdo::Properties::properties_changed instead of emitter.emit.
Retain the existing interface name, changed-property collection, and warning
behavior for emission failures.

In `@crates/cardwire-daemon/src/tasks/monitor_udev.rs`:
- Line 25: Correct the typo in the error message emitted by the udev monitoring
task, changing “reresh” to “refresh” while leaving the logging behavior and
error interpolation unchanged.
- Around line 22-27: Update the refresh_gpu handling so every successful or
failed refresh path emits the GPU-list-changed signal, not just the udev task’s
Ok branch. Locate the shared DebugInterface::refresh_gpu D-Bus handling and
invoke switcheroo.emit_gpu_list_changed after the refresh result is processed,
preserving the existing error logging.

In `@crates/cardwire-gui/src/subscription.rs`:
- Around line 345-355: Replace the final-failure handling at
crates/cardwire-gui/src/subscription.rs:345-355 with a shared bounded-delay
retry helper that repeatedly calls build_gpu_streams until it succeeds, rather
than returning from the startup subscription. Apply the same helper at
crates/cardwire-gui/src/subscription.rs:430-439 after the warning so refresh
retries rebuilding the cleared dbus_streams and dbus_properties instead of
continuing with empty maps.
- Around line 415-440: Update the needs_refresh handling around the subscription
event loop to coalesce the burst of ObjectManager signals before rebuilding:
drain both ObjectManager streams non-blockingly until no pending events remain,
or apply a short debounce delay, then perform GetManagedObjects and
build_gpu_streams only once for the batch. Preserve the existing device-fetch
and error-reporting behavior while avoiding repeated rebuilds for each
InterfacesRemoved or InterfacesAdded event.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: f0c9d4e8-f26b-42f3-8531-c85306fdb8a2

📥 Commits

Reviewing files that changed from the base of the PR and between 5e7fbe6 and c1b419c.

📒 Files selected for processing (10)
  • crates/cardwire-cli/src/main.rs
  • crates/cardwire-daemon/src/daemon.rs
  • crates/cardwire-daemon/src/file/config.rs
  • crates/cardwire-daemon/src/interface/config.rs
  • crates/cardwire-daemon/src/interface/switcheroo.rs
  • crates/cardwire-daemon/src/models.rs
  • crates/cardwire-daemon/src/tasks/monitor_udev.rs
  • crates/cardwire-ebpf/src/main.rs
  • crates/cardwire-gui/src/helpers/dbus.rs
  • crates/cardwire-gui/src/subscription.rs
💤 Files with no reviewable changes (1)
  • crates/cardwire-gui/src/helpers/dbus.rs

Comment thread crates/cardwire-daemon/src/daemon.rs Outdated
Comment thread crates/cardwire-daemon/src/file/config.rs
Comment thread crates/cardwire-daemon/src/interface/switcheroo.rs
Comment thread crates/cardwire-daemon/src/interface/switcheroo.rs
Comment thread crates/cardwire-daemon/src/tasks/monitor_udev.rs
Comment thread crates/cardwire-daemon/src/tasks/monitor_udev.rs Outdated
Comment thread crates/cardwire-gui/src/subscription.rs
Comment thread crates/cardwire-gui/src/subscription.rs

@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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/cardwire-daemon/src/models.rs`:
- Around line 113-115: Share `signal_emitter` across all `SwitcherooInterface`
clones using shared interior state such as
`Arc<OnceLock<SignalEmitter<'static>>>`. In
`crates/cardwire-daemon/src/models.rs` lines 113-115, pass the shared-state
interface into `DebugInterface`; in `crates/cardwire-daemon/src/daemon.rs` lines
75-77, initialize that state after interface registration; and in
`crates/cardwire-daemon/src/interface/debug.rs` line 161, emit through the
shared state so the debug clone observes the initialized emitter.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 8bb38dcc-c1dd-4ef5-af61-62a56e0af538

📥 Commits

Reviewing files that changed from the base of the PR and between c1b419c and f4a6083.

📒 Files selected for processing (9)
  • crates/cardwire-daemon/src/daemon.rs
  • crates/cardwire-daemon/src/file/config.rs
  • crates/cardwire-daemon/src/interface/config.rs
  • crates/cardwire-daemon/src/interface/debug.rs
  • crates/cardwire-daemon/src/interface/gpu.rs
  • crates/cardwire-daemon/src/interface/switcheroo.rs
  • crates/cardwire-daemon/src/models.rs
  • crates/cardwire-daemon/src/tasks/monitor_udev.rs
  • crates/cardwire-gui/src/app.rs

Comment thread crates/cardwire-daemon/src/models.rs

@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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@nix/ci-15gpu.nix`:
- Around line 77-79: Rename the subtest label around machine.succeed("cardwire
set manual") and the cardwire gpu 0 rejection assertion to describe that
blocking the default GPU is rejected; do not claim it blocks 14 GPUs unless
assertions for the remaining GPUs are restored.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: a0350cfd-fab7-4c5b-bb40-d9ae6e7f925e

📥 Commits

Reviewing files that changed from the base of the PR and between 98cd768 and da75d33.

📒 Files selected for processing (3)
  • nix/ci-15gpu.nix
  • nix/ci-2gpu.nix
  • nix/ci-3gpu.nix

Comment thread nix/ci-15gpu.nix
@luytan
luytan merged commit 8859d3c into main Aug 7, 2026
7 checks passed
@luytan
luytan deleted the fix-multiple-issues branch August 7, 2026 09:30
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