Skip to content

Migrate MacFileSystemFFM to AutoCloseable try-with-resources - #3294

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:migrate-mac-disk-filesystem
May 24, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:migrate-mac-disk-filesystem

Conversation

@dbwiddis

@dbwiddis dbwiddis commented May 24, 2026 •

Copy link
Copy Markdown
Member

Replace manual finally blocks with try-with-resources for CF/IOKit object lifecycle management in MacFileSystemFFM.

Changes

  • Outer: try (session) and try (daVolumeNameKey)
  • Inner: try (DADiskRef disk) and try (CFDictionaryRef diskInfo) for DiskArbitration lookups
  • IOKit: try (IOIterator fsIter) and try (IORegistryEntry fsEntry) for UUID lookup

Bug fix: IORegistryEntry leak (JNA + FFM)

The AutoCloseable migration revealed a pre-existing resource leak in both the JNA and FFM implementations: when fsEntry.conformsTo("IOMedia") returned false, the IORegistryEntry was never released. This was invisible in the original code because the release was inside the if block. The try-with-resources refactoring made the missing cleanup path obvious.

Fixed in both:

  • MacFileSystemFFM.java — try (IORegistryEntry fsEntry = ...) ensures unconditional release
  • MacFileSystemJNA.java — moved fsEntry.release() outside the conformsTo check

Deferred

MacHWDiskStoreFFM — deeply nested (18 finally blocks across 3 methods), requires dedicated careful attention in a separate PR.

Summary by CodeRabbit

  • Refactor

    • Improved macOS file system handling to ensure native resources are cleaned up reliably, reducing risk of leaks and intermittent failures.
    • Simplified logic for reading volume identifiers and names to make resource management more predictable.
  • Bug Fixes

    • Ensures consistent release of native handles, improving stability when detecting and accessing mounted volumes.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 24, 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: 50cbbc9c-62bc-447d-b8ed-a458a6e92b19

📥 Commits

Reviewing files that changed from the base of the PR and between 4ac81d7 and 73e1659.

📒 Files selected for processing (2)
  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacFileSystemFFM.java
  • oshi-core/src/main/java/oshi/software/os/mac/MacFileSystemJNA.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). (11)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: JMH Benchmarks, windows-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 11, macos-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: JMH Benchmarks, macos-latest
  • GitHub Check: JMH Benchmarks, ubuntu-latest
  • GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (1)
📚 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/software/os/mac/MacFileSystemJNA.java
🔇 Additional comments (4)
oshi-core-ffm/src/main/java/oshi/software/os/mac/MacFileSystemFFM.java (3)

91-93: LGTM!


141-153: LGTM!


158-172: LGTM!

oshi-core/src/main/java/oshi/software/os/mac/MacFileSystemJNA.java (1)

145-154: LGTM!


📝 Walkthrough

Walkthrough

MacFileSystemFFM#getFileStoreMatching and the JNA equivalent are refactored to scope DiskArbitration, CoreFoundation, and IOKit handles with try-with-resources; manual release()/finally cleanup is removed and the JNA UUID extraction null/conformsTo checks were restructured.

Changes

DiskArbitration and IOKit Resource Management Refactoring

Layer / File(s) Summary
Session and CFString key resource initialization
oshi-core-ffm/src/main/java/oshi/software/os/mac/MacFileSystemFFM.java
DASessionRef session is created in try (session), and the DAVolumeName CFStringRef key is scoped in nested try blocks; the explicit finally that released the key/session is removed.
Disk/UUID lookup resource cleanup refactoring
oshi-core-ffm/src/main/java/oshi/software/os/mac/MacFileSystemFFM.java
In the bsdName.startsWith("disk") path, DADiskRef and copied disk description CFDictionaryRef are wrapped in try-with-resources, and IOKit IOIterator/IORegistryEntry iteration now uses scoped try-resources; manual release() calls were removed while keeping volume name and UUID extraction logic.
JNA UUID extraction and release restructuring
oshi-core/src/main/java/oshi/software/os/mac/MacFileSystemJNA.java
UUID extraction was split into a fsEntry != null guard then conformsTo("IOMedia") check; fsEntry.release() is now executed whenever fsEntry is non-null, changing release timing for non-conforming entries.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • oshi/oshi#3292: Adds AutoCloseable to CoreFoundation and IOKit handle types, enabling try-with-resources usage used in this PR.
  • oshi/oshi#3104: Prior changes to MacFileSystemF F M getFileStoreMatching related to MacOSFileStore instantiation in the same method.

Poem

🐰 I nest my blocks and close each paw,
No finalies now to gnaw and thaw.
Handles sleep sound, all tidy and neat,
UUIDs lowered, releases complete. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly matches the main change: refactoring MacFileSystemFFM to use try-with-resources instead of manual finally blocks for resource cleanup.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 60.00000% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.58%. Comparing base (437822e) to head (73e1659).

Files with missing lines Patch % Lines
...in/java/oshi/software/os/mac/MacFileSystemFFM.java 66.66% 0 Missing and 5 partials ⚠️
...in/java/oshi/software/os/mac/MacFileSystemJNA.java 40.00% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3294      +/-   ##
============================================
- Coverage     75.62%   75.58%   -0.04%     
  Complexity       76       76              
============================================
  Files           410      410              
  Lines         15807    15800       -7     
  Branches       2529     2528       -1     
============================================
- Hits          11954    11943      -11     
- Misses         2381     2385       +4     
  Partials       1472     1472              
Flag Coverage Δ
linux 40.17% <0.00%> (+0.04%) ⬆️
macos 40.91% <60.00%> (-0.01%) ⬇️
windows 48.64% <0.00%> (-0.02%) ⬇️

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.

@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 `@oshi-core-ffm/src/main/java/oshi/software/os/mac/MacFileSystemFFM.java`:
- Around line 162-170: The code in MacFileSystemFFM currently calls
fsIter.next() into fsEntry and only uses try (fsEntry) when
fsEntry.conformsTo("IOMedia"), causing a resource leak when conformsTo returns
false; to fix, move the try-with-resources to wrap the fsEntry declaration
unconditionally (i.e., obtain fsEntry and immediately enter try (fsEntry) { ...
}), then inside the try check fsEntry != null and fsEntry.conformsTo("IOMedia")
before accessing getStringProperty("UUID") so the resource is always closed
regardless of conformity.
🪄 Autofix (Beta)

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: CHILL

Plan: Pro

Run ID: c3fb7991-351a-45b5-bacc-a122de8f41b9

📥 Commits

Reviewing files that changed from the base of the PR and between 437822e and bb13e0e.

📒 Files selected for processing (1)
  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacFileSystemFFM.java
📜 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). (13)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 11, macos-latest
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: JMH Benchmarks, windows-latest
  • GitHub Check: JMH Benchmarks, macos-latest
  • GitHub Check: JMH Benchmarks, ubuntu-latest
  • GitHub Check: Analyze (java)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
🔇 Additional comments (3)
oshi-core-ffm/src/main/java/oshi/software/os/mac/MacFileSystemFFM.java (3)

91-93: LGTM!


141-153: LGTM!


158-159: LGTM!

@dbwiddis
dbwiddis force-pushed the migrate-mac-disk-filesystem branch from bb13e0e to 4ac81d7 Compare May 24, 2026 00:17
Replace manual finally blocks with try-with-resources for CF/IOKit
object lifecycle management in MacFileSystemFFM.

Also fixes a pre-existing IORegistryEntry leak in both JNA and FFM
implementations: when fsEntry.conformsTo("IOMedia") returned false,
the entry was never released. This bug was discovered through the
AutoCloseable migration which made the missing cleanup path obvious.

MacHWDiskStoreFFM deferred — deeply nested structure requires
dedicated careful attention.
@dbwiddis
dbwiddis force-pushed the migrate-mac-disk-filesystem branch from 4ac81d7 to 73e1659 Compare May 24, 2026 00:21
@dbwiddis
dbwiddis merged commit 443360e into oshi:master May 24, 2026
19 checks passed
@dbwiddis
dbwiddis deleted the migrate-mac-disk-filesystem branch May 24, 2026 00:25
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