Repository navigation
Add NativeHandle AutoCloseable wrapper for native resource cleanup - #3290
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (6)
📜 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)
🔇 Additional comments (6)
📝 WalkthroughWalkthroughA new ChangesNativeHandle Wrapper and Adoption
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
oshi-core-ffm/src/test/java/oshi/ffm/NativeHandleTest.java (1)
18-60: ⚡ Quick winAdd regression tests for idempotent close and null closer contract.
Current tests miss two high-value guards: repeated
close()should not re-run the closer, and null closer should fail fast at construction.Suggested test additions
+import static org.junit.jupiter.api.Assertions.assertThrows; @@ import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; @@ void testCloserExceptionIsSuppressed() { @@ assertThat(closedWith.get(), is(fakeHandle)); } + + `@Test` + void testCloseIsIdempotent() { + AtomicInteger closeCount = new AtomicInteger(0); + MemorySegment fakeHandle = MemorySegment.ofAddress(0x4321); + NativeHandle h = NativeHandle.of(fakeHandle, seg -> closeCount.incrementAndGet()); + h.close(); + h.close(); + assertThat(closeCount.get(), is(1)); + } + + `@Test` + void testNullCloserThrows() { + MemorySegment fakeHandle = MemorySegment.ofAddress(0x9999); + assertThrows(NullPointerException.class, () -> NativeHandle.of(fakeHandle, null)); + } }🤖 Prompt for 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. In `@oshi-core-ffm/src/test/java/oshi/ffm/NativeHandleTest.java` around lines 18 - 60, Tests are missing checks for two contracts: NativeHandle.close() must be idempotent (calling close() multiple times should only invoke the provided closer once) and NativeHandle.of(...) must reject a null closer immediately (throw at construction). Add a test that creates a NativeHandle via NativeHandle.of(handle, closer) where the closer increments an AtomicInteger, call close() on the handle multiple times (or exit try-with-resources then call close() again) and assert the counter == 1; and add a test that calls NativeHandle.of(someHandle, null) and asserts that the constructor throws (e.g., NullPointerException) rather than allowing a null closer.
🤖 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/ffm/NativeHandle.java`:
- Around line 48-51: The NativeHandle constructor and close logic must fail fast
on a null ThrowingCloser and make close idempotent to prevent double-closing
native resources: in the NativeHandle(MemorySegment handle, ThrowingCloser
closer) constructor validate closer != null and throw
NPE/IllegalArgumentException immediately if null, store it otherwise; in the
close() method on class NativeHandle add a volatile/atomic boolean (e.g., closed
or closedFlag as AtomicBoolean) and only invoke closer.close(handle) when
transitioning from false to true so subsequent close() calls are no-ops, and
ensure any exceptions from closer.close are propagated or wrapped consistently;
update references to the closer field and close() method accordingly.
---
Nitpick comments:
In `@oshi-core-ffm/src/test/java/oshi/ffm/NativeHandleTest.java`:
- Around line 18-60: Tests are missing checks for two contracts:
NativeHandle.close() must be idempotent (calling close() multiple times should
only invoke the provided closer once) and NativeHandle.of(...) must reject a
null closer immediately (throw at construction). Add a test that creates a
NativeHandle via NativeHandle.of(handle, closer) where the closer increments an
AtomicInteger, call close() on the handle multiple times (or exit
try-with-resources then call close() again) and assert the counter == 1; and add
a test that calls NativeHandle.of(someHandle, null) and asserts that the
constructor throws (e.g., NullPointerException) rather than allowing a null
closer.
🪄 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: 6e72586d-f6b3-4da0-b41f-bc6eb8fbed88
📒 Files selected for processing (6)
oshi-core-ffm/src/main/java/oshi/ffm/NativeHandle.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsCentralProcessorFFM.javaoshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsFileSystemFFM.javaoshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOSProcessFFM.javaoshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOperatingSystemFFM.javaoshi-core-ffm/src/test/java/oshi/ffm/NativeHandleTest.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). (8)
- 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: Analyze (java)
- GitHub Check: JMH Benchmarks, macos-latest
- GitHub Check: JMH Benchmarks, windows-latest
- GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
- GitHub Check: JMH Benchmarks, ubuntu-latest
🔇 Additional comments (4)
oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsCentralProcessorFFM.java (1)
38-38: LGTM!Also applies to: 124-130, 404-410, 426-432
oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsFileSystemFFM.java (1)
34-35: LGTM!Also applies to: 161-162
oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOSProcessFFM.java (1)
52-53: LGTM!Also applies to: 86-90
oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOperatingSystemFFM.java (1)
52-53: LGTM!Also applies to: 102-117, 155-156
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3290 +/- ##
============================================
- Coverage 75.73% 75.67% -0.06%
Complexity 76 76
============================================
Files 410 410
Lines 15817 15814 -3
Branches 2515 2518 +3
============================================
- Hits 11979 11968 -11
- Misses 2373 2379 +6
- Partials 1465 1467 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Introduce NativeHandle, a lightweight AutoCloseable wrapper around native MemorySegment handles that ensures proper cleanup via try-with-resources. This eliminates error-prone manual finally blocks for native handle lifecycle management. NativeHandle: - Wraps a MemorySegment with a ThrowingCloser callback - Skips close if handle is null or zero-address - Logs and suppresses exceptions from the closer - Fully tested with unit tests Migrate simple single-handle Windows FFM methods as reference: - WindowsFileSystemFFM.getLocalVolumes() (FindVolumeClose) - WindowsCentralProcessorFFM registry methods (RegCloseKey) - WindowsOperatingSystemFFM.enableDebugPrivilege() (CloseHandle) - WindowsOperatingSystemFFM.queryServicesFFM() (CloseServiceHandle) - WindowsOSProcessFFM.getAffinityMask() (CloseHandle)
f403b28 to
f348629
Compare
Introduce
NativeHandle, a lightweightAutoCloseablewrapper around nativeMemorySegmenthandles that ensures proper cleanup via try-with-resources. This eliminates error-prone manualfinallyblocks for native handle lifecycle management.NativeHandle
MemorySegmentwith aThrowingClosercallbackReference migrations (Windows)
Simple single-handle cases migrated as examples:
WindowsFileSystemFFM.getLocalVolumes()—FindVolumeCloseWindowsCentralProcessorFFMregistry methods —RegCloseKeyWindowsOperatingSystemFFM.enableDebugPrivilege()—CloseHandleWindowsOperatingSystemFFM.queryServicesFFM()—CloseServiceHandleWindowsOSProcessFFM.getAffinityMask()—CloseHandleFuture PRs
IOObject/CFTypeRef→AutoCloseable)Summary by CodeRabbit
Refactor
Tests