Repository navigation
Guarantee getCurrentProcess() never returns null - #3583
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (4)
📜 Recent review details⏰ Context from checks skipped due to timeout. (7)
📝 WalkthroughWalkthrough
ChangesCurrent process fallback
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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
🤖 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-common/src/main/java/oshi/software/os/OperatingSystem.java`:
- Around line 257-263: Update the default current-process method in
OperatingSystem to enforce its documented non-null contract when
getProcess(getProcessId()) returns null, using the specified fallback process
with the current PID, RUNNING state, and unknown defaults. Add a regression test
using a direct OperatingSystem implementation that does not extend
AbstractOperatingSystem and verifies the method never returns null.
🪄 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: CHILL
Plan: Pro Plus
Run ID: e4b0acfb-15b8-44c4-8f0a-a3df88689217
📒 Files selected for processing (5)
CHANGELOG.mdoshi-common/src/main/java/oshi/software/common/AbstractOperatingSystem.javaoshi-common/src/main/java/oshi/software/common/CurrentProcessStub.javaoshi-common/src/main/java/oshi/software/os/OperatingSystem.javaoshi-common/src/test/java/oshi/software/common/CurrentProcessStubTest.java
📜 Review details
⏰ Context from checks skipped due to timeout. (7)
- 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 25, ubuntu-latest (+ JNA==FFM)
- GitHub Check: Test JDK 26, ubuntu-latest
- GitHub Check: Analyze (java)
- GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
🧰 Additional context used
📓 Path-based instructions (8)
oshi-common/src/main/java/**/*.java
📄 CodeRabbit inference engine (AGENTS.md)
oshi-common/src/main/java/**/*.java: Keeposhi-commonpure Java: do not reference JNA, FFM (java.lang.foreign), orSystem.load/loadLibrary; place parsing, conversion, validation, caching, and shared implementation logic here when it is not native-API-specific.
Before adding a general parsing helper, checkoshi.util.ParseUtil; use its existing parsing methods or add shared primitives there rather than duplicating parsers in platform drivers.
Read runtime configuration fromoshi-common/src/main/resources/oshi.propertiesthroughGlobalConfigor Java system properties; do not introduce a separate configuration mechanism.
Files:
oshi-common/src/main/java/oshi/software/os/OperatingSystem.javaoshi-common/src/main/java/oshi/software/common/AbstractOperatingSystem.javaoshi-common/src/main/java/oshi/software/common/CurrentProcessStub.java
{oshi-common,oshi-core}/src/main/java/**/*.java
📄 CodeRabbit inference engine (AGENTS.md)
Use Java 8 language and API compatibility: do not use
var, records, switch expressions, text blocks,List.of,Stream.toList(), or other post-Java-8 features.
Files:
oshi-common/src/main/java/oshi/software/os/OperatingSystem.javaoshi-common/src/main/java/oshi/software/common/AbstractOperatingSystem.javaoshi-common/src/main/java/oshi/software/common/CurrentProcessStub.java
**/*.java
📄 CodeRabbit inference engine (AGENTS.md)
**/*.java: Do not use banned numeric parsing APIs such asInteger.parseInt,Long.parseLong,Double.parseDouble, orInteger.decode; use the corresponding tolerantParseUtilmethods.
Do not importorg.slf4j.event.**; useoshi.util.LogLevelinstead.
Files:
oshi-common/src/main/java/oshi/software/os/OperatingSystem.javaoshi-common/src/test/java/oshi/software/common/CurrentProcessStubTest.javaoshi-common/src/main/java/oshi/software/common/AbstractOperatingSystem.javaoshi-common/src/main/java/oshi/software/common/CurrentProcessStub.java
**/src/main/java/**/*.java
📄 CodeRabbit inference engine (AGENTS.md)
**/src/main/java/**/*.java: Keep external imports synchronized withconfig/import-control.xml, the module'smodule-info.java, and, when necessary, its Maven configuration.
Provide Javadoc for every public and protected type and method; use imported simple class names rather than fully qualified names in code and Javadoc links.
Use the repository's concurrency annotation (@ThreadSafe,@Immutable,@NotThreadSafe, or@GuardedBy) on new classes, matching the modeled class where appropriate.
Memoize constant or expensive, slowly changing system values withoshi.util.Memoizer.memoize, generally using aSupplier<T>anddefaultExpiration()when refresh is required.
Do not duplicate non-native logic between platform implementations; hoist parsing, conversion, filtering, caching, validation, and sentinel handling intooshi-commonwhere possible.
Files:
oshi-common/src/main/java/oshi/software/os/OperatingSystem.javaoshi-common/src/main/java/oshi/software/common/AbstractOperatingSystem.javaoshi-common/src/main/java/oshi/software/common/CurrentProcessStub.java
**/*.{java,xml,properties,md}
📄 CodeRabbit inference engine (AGENTS.md)
Do not commit generated output,
target/, or IDE files.
Files:
oshi-common/src/main/java/oshi/software/os/OperatingSystem.javaoshi-common/src/test/java/oshi/software/common/CurrentProcessStubTest.javaoshi-common/src/main/java/oshi/software/common/AbstractOperatingSystem.javaoshi-common/src/main/java/oshi/software/common/CurrentProcessStub.javaCHANGELOG.md
**/src/test/java/**/*Test.java
📄 CodeRabbit inference engine (AGENTS.md)
Write tests against parsing and transformation methods using fixture data; keep acquisition methods thin. Do not add Mockito, and use JUnit 5 with Hamcrest.
Files:
oshi-common/src/test/java/oshi/software/common/CurrentProcessStubTest.java
**/*Test.java
📄 CodeRabbit inference engine (AGENTS.md)
Test hardware-dependent behavior using invariants, plausible ranges, correct types, and ordering rather than fixed machine-specific values; treat zero and empty results as valid where applicable.
Files:
oshi-common/src/test/java/oshi/software/common/CurrentProcessStubTest.java
CHANGELOG.md
📄 CodeRabbit inference engine (AGENTS.md)
Add a
Next Releasechangelog entry only for user-visible changes; omit pure refactors, deduplication, test-only changes, and CI configuration changes.
Files:
CHANGELOG.md
🧠 Learnings (4)
📚 Learning: 2026-04-12T19:22:09.716Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3149
File: oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java:159-163
Timestamp: 2026-04-12T19:22:09.716Z
Learning: When reviewing OSHI hardware platform code that uses or implements `CentralProcessor`/`PhysicalProcessor.getEfficiency()`, remember the API’s counterintuitive semantics: a *higher* efficiency-class value indicates *greater performance* (lower efficiency). P-cores/“performance cores” should therefore have higher values (e.g., 1) than E-cores/“efficiency cores” (e.g., 0), as documented in the `CentralProcessor` Javadoc. Do not flag code as incorrect solely because ARM_P_CORES or Intel P-cores are assigned higher efficiency values.
Applied to files:
oshi-common/src/main/java/oshi/software/os/OperatingSystem.javaoshi-common/src/main/java/oshi/software/common/AbstractOperatingSystem.javaoshi-common/src/main/java/oshi/software/common/CurrentProcessStub.java
📚 Learning: 2026-07-06T19:25:59.225Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3432
File: oshi-common/src/main/java/oshi/util/ExceptionUtil.java:26-56
Timestamp: 2026-07-06T19:25:59.225Z
Learning: In this repo’s SLF4J 1.x-compatible logging code, do not flag cases where a logger call uses SLF4J 1.6+ implicit-cause handling—e.g., `log.error("...{}", value, t)` (or similar with a single `{}` placeholder) where `t` is passed as the final/trailing argument. SLF4J will attach `t` as the log event’s cause with the full stack trace; this is equivalent to SLF4J 2.x’s fluent `atLevel(...).setCause(t).log(...)` and is intentionally used to preserve SLF4J 1.x runtime compatibility.
Applied to files:
oshi-common/src/main/java/oshi/software/os/OperatingSystem.javaoshi-common/src/main/java/oshi/software/common/AbstractOperatingSystem.javaoshi-common/src/main/java/oshi/software/common/CurrentProcessStub.java
📚 Learning: 2026-07-30T00:35:22.638Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3547
File: oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdFirmware.java:82-82
Timestamp: 2026-07-30T00:35:22.638Z
Learning: In OSHI, treat changes as “API-breaking” only when they affect classes (or relevant API surface) annotated with `PublicAPI`. Public classes/members that are not annotated with `PublicAPI` should be reviewed as internal implementation details, even if they are public (e.g., classes like `oshi.hardware.common.platform.unix.BsdFirmware`). Also, respect intentional visibility decisions used for tests—e.g., methods like `BsdFirmware.parseDmesg(List<String>)` may be intentionally package-private to support tests within the same package; avoid widening visibility or altering signatures unless the related `PublicAPI` contract requires it.
Applied to files:
oshi-common/src/main/java/oshi/software/os/OperatingSystem.javaoshi-common/src/main/java/oshi/software/common/AbstractOperatingSystem.javaoshi-common/src/main/java/oshi/software/common/CurrentProcessStub.java
📚 Learning: 2026-08-03T00:46:14.953Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3566
File: oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdNetworkParams.java:18-22
Timestamp: 2026-08-03T00:46:14.953Z
Learning: In the OSHI Java codebase, do not require duplicate Javadoc on public methods annotated with Override when the inherited Javadoc from the parent declaration adequately documents the method. The general public-member Javadoc requirement should not flag these overrides solely because they lack redundant documentation.
Applied to files:
oshi-common/src/main/java/oshi/software/os/OperatingSystem.javaoshi-common/src/test/java/oshi/software/common/CurrentProcessStubTest.javaoshi-common/src/main/java/oshi/software/common/AbstractOperatingSystem.javaoshi-common/src/main/java/oshi/software/common/CurrentProcessStub.java
🔇 Additional comments (4)
oshi-common/src/main/java/oshi/software/common/CurrentProcessStub.java (1)
1-116: LGTM!oshi-common/src/main/java/oshi/software/common/AbstractOperatingSystem.java (1)
105-114: LGTM!oshi-common/src/test/java/oshi/software/common/CurrentProcessStubTest.java (1)
27-163: LGTM!CHANGELOG.md (1)
17-17: LGTM!
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3583 +/- ##
============================================
- Coverage 79.82% 79.72% -0.11%
+ Complexity 4100 4099 -1
============================================
Files 709 710 +1
Lines 24220 24242 +22
Branches 4051 4052 +1
============================================
- Hits 19334 19327 -7
- Misses 2736 2762 +26
- Partials 2150 2153 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
988e1e7 to
626872e
Compare
|
Note GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer. |
OperatingSystem#getCurrentProcess() is documented to return "the current
process", with no mention of null, but it delegated to getProcess(int),
which is documented to return null when the process is not found. The
contract was already the stronger one; the implementation quietly broke it.
That is not theoretical. Every non-Linux getCurrentThread() dereferences it
without a guard -- AIX, FreeBSD, OpenBSD, NetBSD, DragonFly and macOS all
do
OSProcess proc = getCurrentProcess();
return proc.getThreadDetails().stream()...
and AIX CI produced exactly that NullPointerException when a truncated
perfstat list omitted the calling JVM. Adding six null guards would treat
the symptom; the current process exists by definition, so a null is a
failed read -- a truncated native list, a denied /proc entry, a race with
the query -- and the fix belongs in the one method that promises otherwise.
Substitute CurrentProcessStub in the default method when the platform query
comes up empty. The stand-in carries the two facts still known to be true,
the process ID and a RUNNING state, and reports the unknown/zero/empty
defaults elsewhere that callers already handle from a live process. Its
empty thread list is what lets each platform's getCurrentThread() fall
through to its existing orElse(new XxxOSThread(pid, tid)) branch instead of
throwing.
The stand-in lives in oshi.software.os, package-private, so the default
itself can enforce the guarantee rather than leaving it to an override in
AbstractOperatingSystem -- which would have left a direct implementor of
the interface, reachable through the SystemInfoProvider SPI, still getting
null from a method whose javadoc says otherwise. Placing it there means
oshi.software.os imports oshi.software.common for the first time; that is a
package cycle, and an acceptable one, since oshi.util and oshi.hardware
already import each other.
OperatingSystemTest.testDefaultGetCurrentProcess covers this through
MINIMAL, which implements the interface directly rather than extending
AbstractOperatingSystem. It previously asserted the null and failed on this
change with "Expected: is null but: was OSProcess[processID=1]"; it now
asserts the contract and the stand-in's empty-not-null accessors.
japicmp: 0.0.1. The stand-in is package-private, so no public API is added,
and callers that null-check getCurrentProcess() keep compiling -- their
check simply stops being reachable.
626872e to
c4bdb55
Compare
|
Note GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer. |
AIX's perfstat_process count-then-fill is a documented, load-dependent race (#3582) where a bounded retry reduces but cannot fully eliminate a miss on the current process under heavy process churn on a shared host; that miss degrades to CurrentProcessStub's zero defaults (#3583) rather than crashing. OshiMetricsTest's process.thread.count/memory.virtual/ uptime assertions didn't account for that degraded-but-valid case, unlike NativeComparisonTest which already tolerates the same class of AIX transient read miss. Extends the existing BSD zero-tolerance carve-out (structural, not a race) to also cover AIX for these three assertions.
Summary
OperatingSystem#getCurrentProcess()is documented as returning "the current process", with no mention of null. It delegated togetProcess(int), which is documented to return null when the process is not found:The contract was already the stronger one. The implementation quietly broke it.
Why this is not theoretical
Every non-Linux
getCurrentThread()dereferences the result unguarded — AIX, FreeBSD, OpenBSD, NetBSD, DragonFly and macOS all do:AIX CI produced precisely that
NullPointerExceptionwhen a truncatedperfstat_processlist omitted the calling JVM. The truncation cause is fixed separately in #3582; this is the other half, since a denied/procentry or a query race would do the same.Adding six null guards would treat the symptom. The current process exists by definition, so a null is a failed read, not a missing process — and the fix belongs in the one method that promises otherwise.
Change
The default method substitutes a package-private
CurrentProcessStubwhen the platform query comes up empty. The stand-in carries the two facts still known to be true — the process ID, and that it is running — and reports the unknown/zero/empty defaults elsewhere that callers already handle from a live process.Its empty thread list is the load-bearing detail: it lets each platform's
getCurrentThread()fall through to its existingorElse(new XxxOSThread(pid, tid))branch rather than throwing, so the degraded result is a real thread object with the correct pid and tid.The stand-in lives in
oshi.software.osso the default itself enforces the guarantee. An earlier revision of this PR put the substitution in anAbstractOperatingSystemoverride instead, which left a direct implementor of the interface — reachable through theSystemInfoProviderSPI — still getting null from a method whose javadoc promised otherwise. Placing it inoshi.software.osmeans that package importsoshi.software.commonfor the first time, a package cycle; that is acceptable here, asoshi.utilandoshi.hardwarealready import each other.Tests
OperatingSystemTest.testDefaultGetCurrentProcesscovers this throughMINIMAL, which implements the interface directly rather than extendingAbstractOperatingSystem, and whosegetProcess(int)returns null for every pid as a failed native read would.That test previously asserted the null, and failed on this change with:
which is the regression coverage for the default itself. It now asserts the contract and the stand-in's empty-not-null accessors.
Compatibility and verification
japicmpreportsSemantic versioning suggestion: 0.0.1— the stand-in is package-private, so no public API is added, and callers that null-checkgetCurrentProcess()keep compiling; their check simply stops being reachable.Full
./mvnw clean spotless:apply sortpom:sort checkstyle:check install forbiddenapis:check javadoc:javadocgreen on JDK 26: 1311/119/96 tests, 0 failures. Live-checked on macOS that the normal path is unaffected — the real process is still returned, arguments and environment unchanged.Note: the CHANGELOG entry will conflict with #3582's if that merges first; trivial to resolve.
Summary by CodeRabbit
Bug Fixes
Documentation