Skip to content

Migrate remaining macOS AutoCloseable call sites - #3293

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:migrate-remaining-macos-autocloseable
May 23, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:migrate-remaining-macos-autocloseable

Conversation

@dbwiddis

@dbwiddis dbwiddis commented May 23, 2026 •

Copy link
Copy Markdown
Member

Continue the AutoCloseable migration for macOS FFM classes. Eliminates the releaseCFObjects helper entirely and migrates additional call sites.

Changes

  • IOKit.java — Replace all 6 releaseCFObjects usages with CFTypeRef try-with-resources; delete the now-unused helper method
  • IOReportClientFFM — Migrate create() and sampleGpuTicks() to try-with-resources
  • MacDisplayFFM — Restructure loop: outer try (serviceIterator; cfEdid), inner try (IORegistryEntry current), separate childEntry variable for conditional release
  • MacPowerSourceFFM — Create CFStringRef keys upfront, nested try-with-resources for powerSourcesInfo and cfList
  • MacUsbDeviceFFM — Multi-resource try (iter; locationIDKey; ioPropertyMatchKey)
  • MacNetworkParamsFFM — NativeHandle for freeaddrinfo

Intentionally left

  • MacHWDiskStoreFFM / MacFileSystemFFM — queued for a dedicated follow-up PR
  • MacSensorsFFM / MacGpuStatsFFM — smcClose(int) not a MemorySegment handle
  • MacCentralProcessorFFM — vm_deallocate (raw memory, not CF/IOKit)
  • IOReportClientFFM (4 remaining) — stateful cross-call sample lifecycle
  • WhoFFM — endutxent() no-arg cleanup, not a handle

Summary by CodeRabbit

  • Refactor
    • Improved resource management and lifecycle handling across macOS platform modules to enhance code safety and stability.

Eliminate releaseCFObjects helper in IOKit.java by wrapping raw
MemorySegments as CFTypeRef in try-with-resources.

Additional migrations:
- IOReportClientFFM.create() and sampleGpuTicks()
- MacDisplayFFM (restructured loop with separate childEntry)
- MacPowerSourceFFM (keys created upfront, nested try-with-resources)
- MacUsbDeviceFFM (multi-resource try-with-resources)
- MacNetworkParamsFFM (NativeHandle for freeaddrinfo)
@coderabbitai

coderabbitai Bot commented May 23, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 9f2296bb-eab2-4e41-8fa3-a2dea10ad880

📥 Commits

Reviewing files that changed from the base of the PR and between 3889424 and 179b594.

📒 Files selected for processing (6)
  • oshi-core-ffm/src/main/java/oshi/driver/mac/IOReportClientFFM.java
  • oshi-core-ffm/src/main/java/oshi/ffm/mac/IOKit.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacDisplayFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacPowerSourceFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacNetworkParamsFFM.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). (9)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: JMH Benchmarks, ubuntu-latest
  • GitHub Check: JMH Benchmarks, macos-latest
  • GitHub Check: JMH Benchmarks, windows-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Analyze (java)
🔇 Additional comments (6)
oshi-core-ffm/src/main/java/oshi/software/os/mac/MacNetworkParamsFFM.java (1)

29-30: LGTM!

Also applies to: 73-75

oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java (1)

82-82: LGTM!

oshi-core-ffm/src/main/java/oshi/driver/mac/IOReportClientFFM.java (1)

19-19: LGTM!

Also applies to: 67-77, 79-92, 109-122

oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacDisplayFFM.java (1)

50-50: LGTM!

Also applies to: 53-69, 72-72

oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacPowerSourceFFM.java (1)

24-24: LGTM!

Also applies to: 77-77, 171-180, 183-187, 189-214, 217-217

oshi-core-ffm/src/main/java/oshi/ffm/mac/IOKit.java (1)

43-44: LGTM!

Also applies to: 262-263, 277-278, 296-297, 315-316, 334-335, 351-352


📝 Walkthrough

Walkthrough

This PR refactors macOS FFM driver classes to adopt Java try-with-resources for resource lifecycle management, replacing manual finally blocks with CFTypeRef and NativeHandle wrappers for CoreFoundation and native objects across GPU reporting, IOKit property access, display enumeration, USB iteration, power source traversal, and hostname resolution.

Changes

macOS FFM Try-With-Resources Lifecycle Management

Layer / File(s) Summary
IOReportClientFFM GPU/Energy IOReport lifecycle
oshi-core-ffm/src/main/java/oshi/driver/mac/IOReportClientFFM.java
create() and sampleGpuTicks() now use try-with-resources to scope CFTypeRef wrappers for CoreFoundation group/channel segments and per-sample objects, eliminating manual finally-block releases while preserving null-on-failure behavior and sentinel return values on sampling failure.
IOKit registry property accessors
oshi-core-ffm/src/main/java/oshi/ffm/mac/IOKit.java
Six property getter methods (getStringProperty, getLongProperty, getIntegerProperty, getDoubleProperty, getByteArrayProperty, getBooleanProperty) refactored to wrap CoreFoundation key/value objects in CFTypeRef try-with-resources instead of manual blocks; local releaseCFObjects helper removed. Extraction logic and null-checks unchanged; lifecycle now automatic.
MacDisplayFFM EDID extraction from IOKit services
oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacDisplayFFM.java
getDisplaysFromService() switches from nested manual try/finally blocks with explicit release() calls to nested try-with-resources for IOIterator and CFStringRef, plus per-entry try-with-resources for IORegistryEntry. EDID extraction and filtering logic preserved; resource cleanup now automatic.
MacPowerSourceFFM battery enumeration
oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacPowerSourceFFM.java
getPowerSources() refactors smartBattery lookup and CoreFoundation power-sources traversal using try-with-resources for CFStringRef keys, copied info segment (CFTypeRef), and power-sources list (CFArrayRef), replacing prior large try/catch/finally cleanup block. Empty-list error fallback behavior unchanged.
MacUsbDeviceFFM USB device enumeration
oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
queryUsbDevices() iterator loop setup refactored: iter, locationIDKey, and ioPropertyMatchKey now managed in a try-with-resources statement, replacing prior explicit finally block. Device enumeration logic unchanged.
MacNetworkParamsFFM hostname resolution via addrinfo
oshi-core-ffm/src/main/java/oshi/software/os/mac/MacNetworkParamsFFM.java
macOS hostname resolution refactored to use NativeHandle try-with-resources wrapper for the native addrinfo pointer instead of manual try/finally plus explicit freeaddrinfo call. Canonical-name extraction logic unchanged; imports adjusted to add NativeHandle/MacSystemFunctions and remove direct freeaddrinfo static import.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

  • oshi/oshi#3292: Part of the same macOS lifecycle-migration work refactoring IOReportClientFFM and other macOS call sites to rely on new CFTypeRef/IOObject AutoCloseable support.
  • oshi/oshi#3273: Overlaps at IOReportClientFFM's cfRelease cleanup path, which is refactored here to delegate to try-with-resources instead of the prior ExceptionUtil.runSilently approach.
  • oshi/oshi#3287: Both PRs modify MacNetworkParamsFFM hostname resolution, using NativeHandle/try-with-resources in this PR versus callInArenaOrDefault helpers in PR #3287.

Poem

🐰 Ah, resources now close with a scope—
No more finally blocks, just auto-closing hope,
CFTypeRef wraps them so tight,
Try-with-resources shines bright!
Memory leaks? They take their last hop. 🌿✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 23.08% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 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: migrating remaining macOS FFM code to use AutoCloseable patterns and try-with-resources constructs across six files.
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.

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

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

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 May 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 13.33333% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.63%. Comparing base (3889424) to head (179b594).

Files with missing lines Patch % Lines
...java/oshi/hardware/platform/mac/MacDisplayFFM.java 0.00% 13 Missing ⚠️

❌ Your patch check has failed because the patch coverage (13.33%) is below the target coverage (70.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3293      +/-   ##
============================================
- Coverage     75.64%   75.63%   -0.01%     
  Complexity       76       76              
============================================
  Files           410      410              
  Lines         15810    15807       -3     
  Branches       2528     2529       +1     
============================================
- Hits          11959    11956       -3     
- Misses         2382     2385       +3     
+ Partials       1469     1466       -3     
Flag Coverage Δ
linux 40.12% <0.00%> (+<0.01%) ⬆️
macos 40.92% <13.33%> (+0.01%) ⬆️
windows 48.68% <0.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

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