Skip to content

Log return codes instead of errno for Mach kernel functions - #3147

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:no-errno
Apr 12, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:no-errno

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Apr 12, 2026 •

Copy link
Copy Markdown
Member

Mach kernel functions (host_statistics, host_page_size, host_processor_info) return kern_return_t error codes directly, unlike POSIX/C functions which return -1 and set errno. The existing code incorrectly logged Native.getLastError() (which reads errno) instead of the actual return value.

This fixes 5 call sites across 3 files:

File Function
MacGlobalMemoryJNA host_statistics
MacGlobalMemoryJNA host_page_size
MacVirtualMemoryJNA host_statistics
MacCentralProcessor host_statistics
MacCentralProcessor host_processor_info

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling for macOS CPU load and memory information retrieval.

@coderabbitai

coderabbitai Bot commented Apr 12, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e681ca8d-3822-4ac9-b0fd-1b8b6e744949

📥 Commits

Reviewing files that changed from the base of the PR and between f661f3d and caee80c.

📒 Files selected for processing (3)
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacCentralProcessor.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacGlobalMemoryJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacVirtualMemoryJNA.java
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Test JDK 25, ubuntu-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 21, macos-latest
🧰 Additional context used
🧠 Learnings (4)
📚 Learning: 2026-03-29T20:03:00.830Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3014
File: oshi-core-java25/src/main/java/oshi/util/platform/windows/PdhUtilFFM.java:87-89
Timestamp: 2026-03-29T20:03:00.830Z
Learning: In the OSHI FFM codebase (oshi-core-java25), all PDH/Kernel32/COM wrapper methods in classes like PdhFFM, Kernel32FFM, etc. declare `throws Throwable` solely as a compiler artifact of `MethodHandle.invokeExact`. They are native calls and do not throw Java checked exceptions in practice. The enclosing `getOpenFileDescriptors()` method in PdhUtilFFM already has an outer `catch (Throwable t)` that covers the entire method body including any `finally` blocks, so no exception-masking concern exists for cleanup calls like `PdhCloseQuery` in `finally`.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacVirtualMemoryJNA.java
📚 Learning: 2026-04-06T04:45:47.644Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3121
File: oshi-core/src/main/java/oshi/software/os/linux/LinuxOperatingSystem.java:53-53
Timestamp: 2026-04-06T04:45:47.644Z
Learning: In the oshi/oshi project, only interfaces and classes in the root packages `oshi`, `oshi.hardware`, and `oshi.software.os` are considered the public API. Code in subpackages that represent specific implementations/platforms (e.g., `oshi.software.os.linux`, `oshi.hardware.platform.linux`, and similar subpackages) is implementation detail. When reviewing, do not treat changes to classes in these platform-specific subpackages as public API breaks (for example: making `LinuxOperatingSystem` abstract, narrowing constructor visibility, or adding/removing abstract methods should not be flagged as breaking public API).

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacVirtualMemoryJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacCentralProcessor.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacGlobalMemoryJNA.java
📚 Learning: 2026-04-03T02:07:49.467Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3110
File: oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacHWDiskStoreFFM.java:119-121
Timestamp: 2026-04-03T02:07:49.467Z
Learning: In `oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacHWDiskStoreFFM.java` (and similar FFM/API implementations in OSHI), the `updateAttributes()` method intentionally uses `catch (Throwable e) { return false; }` without logging. This is by design: `updateAttributes()` is a public API method whose contract is to return `false` on failure; callers consume the boolean and would never see log output, so adding a log statement is not desired.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacCentralProcessor.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacGlobalMemoryJNA.java
📚 Learning: 2026-04-06T21:48:11.921Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3122
File: oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java:85-111
Timestamp: 2026-04-06T21:48:11.921Z
Learning: In `oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java` (and `MacUsbDeviceFFM.java`), the `id = 0L` sentinel is intentional when `device.getParentEntry(IOSERVICE)` returns null. It acts as a catch-all controller group for devices whose controller cannot be identified (an edge case that "should never happen" given the IORegistry format). Do not flag this as a bug or suggest skipping such devices; the design intentionally avoids both silently dropping orphaned devices and placing them at the top level without a controller context.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacGlobalMemoryJNA.java
🔇 Additional comments (5)
oshi-core/src/main/java/oshi/hardware/platform/mac/MacVirtualMemoryJNA.java (1)

47-54: Correct switch to return-code-based error logging.

This correctly logs the Mach call return code and avoids errno misuse for host_statistics.

oshi-core/src/main/java/oshi/hardware/platform/mac/MacCentralProcessor.java (2)

206-209: Good error handling update for host_statistics.

Using the direct return code here is the correct behavior for Mach kernel calls.


262-266: Good error handling update for host_processor_info.

Logging the returned kern_return_t value here is correct and more actionable than errno.

oshi-core/src/main/java/oshi/hardware/platform/mac/MacGlobalMemoryJNA.java (2)

29-33: queryVmStats() change is correct.

Capturing and logging the host_statistics return code fixes the prior errno mismatch.


46-54: queryPageSize() now reports the right failure signal.

Using the host_page_size return code in logs is the right fix for Mach APIs.


📝 Walkthrough

Walkthrough

Three macOS platform files were updated to replace Native.getLastError() error reporting with direct capture and use of return codes from JNA system calls (host_statistics and host_processor_info), eliminating the dependency on JNA's error lookup mechanism.

Changes

Cohort / File(s) Summary
macOS Error Handling Refactor
oshi-core/src/main/java/oshi/hardware/platform/mac/MacCentralProcessor.java, MacGlobalMemoryJNA.java, MacVirtualMemoryJNA.java
Replaced Native.getLastError() calls with explicit capture of return codes from JNA system calls. In each method affected, the return value is stored in a local ret variable and used directly for error logging instead of performing a separate error lookup. Removed unused Native import from two files. Control flow and error handling logic remain functionally equivalent.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐰 A rabbit hops through macOS code,
Capturing return codes, lighten the load!
No more getLastError() to chase,
Direct codes logged in their proper place,
Cleaner error paths, a speedier way! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: replacing errno-based error logging with direct return code logging for Mach kernel functions across the pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
✨ Simplify code
  • Create PR with simplified code

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@codecov

codecov Bot commented Apr 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 62.21%. Comparing base (f661f3d) to head (caee80c).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3147      +/-   ##
============================================
+ Coverage     62.17%   62.21%   +0.04%     
  Complexity       12       12              
============================================
  Files            39       39              
  Lines          2329     2329              
  Branches        407      407              
============================================
+ Hits           1448     1449       +1     
+ Misses          774      770       -4     
- Partials        107      110       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbwiddis
dbwiddis merged commit 4d58475 into oshi:master Apr 12, 2026
16 checks passed
dbwiddis added a commit that referenced this pull request Apr 12, 2026
…n; add JNA suffix to MacDisplay and MacGpuStats (#3147) (#3148)
@dbwiddis
dbwiddis deleted the no-errno branch April 12, 2026 18:06
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