Repository navigation
Make @ThreadSafe HAL snapshot fields volatile/final for safe publication - #3489
Conversation
The mutable HAL/OS snapshot classes (OSProcess, OSThread, NetworkIF, and the dynamic CentralProcessor frequency/cache fields) are annotated @threadsafe but populated at construction and refreshed by public updateAttributes()/ updateNetworkStats() calls, then read via getters with no memory synchronization. A concurrent refresh and read could observe stale, torn, or half-updated values, violating the @threadsafe contract. Mark each reassigned mutable stat field volatile, and mark the write-once memoize() Supplier fields final. This gives per-field visibility and safe publication without introducing locking on the hot read path. Clears the CodeQL Reliability findings "Not thread-safe", "Safe publication", and "Escaping" for these classes.
|
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 (3)
🚧 Files skipped from review as they are similar to previous changes (1)
📜 Recent review details⏰ Context from checks skipped due to timeout. (12)
🧰 Additional context used🧠 Learnings (3)📚 Learning: 2026-04-12T19:22:09.716ZApplied to files:
📚 Learning: 2026-04-12T19:34:56.879ZApplied to files:
📚 Learning: 2026-07-06T19:25:59.225ZApplied to files:
🔇 Additional comments (2)
📝 WalkthroughWalkthroughThe pull request updates hardware, processor, process, and thread state fields for cross-thread visibility. It also makes selected identity and memoized supplier fields final, and publishes the Windows NUMA mapping only after complete construction. ChangesConcurrency visibility updates
Estimated code review effort: 2 (Simple) | ~15 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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
🧹 Nitpick comments (3)
oshi-common/src/main/java/oshi/hardware/common/AbstractNetworkIF.java (1)
44-53: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winPrefer
finalovervolatilefor fields initialized only in the constructor.These fields are private and are only assigned once during object construction. Declaring them
finalprovides stronger immutability guarantees, ensures safe publication across threads without the overhead ofvolatilereads, and clearly communicates their read-only nature.♻️ Proposed refactor
- private volatile NetworkInterface networkInterface; - private volatile String name; - private volatile String displayName; - private volatile int index; - private volatile long mtu; - private volatile String mac; - private volatile String[] ipv4; - private volatile Short[] subnetMasks; - private volatile String[] ipv6; - private volatile Short[] prefixLengths; + private final NetworkInterface networkInterface; + private final String name; + private final String displayName; + private final int index; + private final long mtu; + private final String mac; + private final String[] ipv4; + private final Short[] subnetMasks; + private final String[] ipv6; + private final Short[] prefixLengths;🤖 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-common/src/main/java/oshi/hardware/common/AbstractNetworkIF.java` around lines 44 - 53, Update the constructor-initialized fields in AbstractNetworkIF—networkInterface, name, displayName, index, mtu, mac, ipv4, subnetMasks, ipv6, and prefixLengths—from volatile to final, preserving their existing constructor assignments and read-only behavior.oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsCentralProcessor.java (1)
59-59: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueRedundant
volatilemodifier.
utilityBaseMultiplieris exclusively read and written within thesynchronized lazilyCalculateMultipliermethod. Because all accesses are already guarded by the same intrinsic lock, the JMM already guarantees visibility and atomicity. Thevolatilemodifier here is redundant and can be omitted.♻️ Proposed fix
- private volatile Long utilityBaseMultiplier; + private Long utilityBaseMultiplier;🤖 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-common/src/main/java/oshi/hardware/common/platform/windows/WindowsCentralProcessor.java` at line 59, Remove the redundant volatile modifier from the utilityBaseMultiplier field. Keep the field’s Long type and rely on the existing synchronized lazilyCalculateMultiplier method for all accesses.oshi-common/src/main/java/oshi/software/common/AbstractOSProcess.java (1)
25-25: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winPrefer
finalfor immutable identity fields.Unlike the other statistical properties that are updated during refresh,
processIDis exclusively set in the constructor and never mutated. Marking it asfinalprovides stronger initialization safety and clearly conveys intent, rendering thevolatilemodifier unnecessary.♻️ Proposed fix
- private volatile int processID; + private final int processID;🤖 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-common/src/main/java/oshi/software/common/AbstractOSProcess.java` at line 25, Change the processID field in AbstractOSProcess from volatile to final, preserving its constructor initialization and all existing read behavior. Remove the unnecessary volatile modifier and ensure no refresh or other code path attempts to reassign this immutable identity field.
🤖 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/hardware/common/platform/windows/WindowsCentralProcessor.java`:
- Line 43: Update buildNumaNodeProcMap() to populate a local HashMap completely
before assigning it to numaNodeProcToLogicalProcMap. Keep the volatile field
assignment as the final step so readers only observe a fully initialized map.
---
Nitpick comments:
In `@oshi-common/src/main/java/oshi/hardware/common/AbstractNetworkIF.java`:
- Around line 44-53: Update the constructor-initialized fields in
AbstractNetworkIF—networkInterface, name, displayName, index, mtu, mac, ipv4,
subnetMasks, ipv6, and prefixLengths—from volatile to final, preserving their
existing constructor assignments and read-only behavior.
In
`@oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsCentralProcessor.java`:
- Line 59: Remove the redundant volatile modifier from the utilityBaseMultiplier
field. Keep the field’s Long type and rely on the existing synchronized
lazilyCalculateMultiplier method for all accesses.
In `@oshi-common/src/main/java/oshi/software/common/AbstractOSProcess.java`:
- Line 25: Change the processID field in AbstractOSProcess from volatile to
final, preserving its constructor initialization and all existing read behavior.
Remove the unnecessary volatile modifier and ensure no refresh or other code
path attempts to reassign this immutable identity field.
🪄 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: d5919c16-1929-4cf5-8620-27b1efd04950
📒 Files selected for processing (14)
oshi-common/src/main/java/oshi/hardware/common/AbstractNetworkIF.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxNetworkIF.javaoshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.javaoshi-common/src/main/java/oshi/hardware/common/platform/mac/MacNetworkIF.javaoshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsCentralProcessor.javaoshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsNetworkIF.javaoshi-common/src/main/java/oshi/software/common/AbstractOSProcess.javaoshi-common/src/main/java/oshi/software/common/AbstractOSThread.javaoshi-common/src/main/java/oshi/software/common/os/linux/LinuxOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/AbstractProcOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/solaris/SolarisOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/windows/WindowsOSProcess.java
📜 Review details
⏰ Context from checks skipped due to timeout. (3)
- GitHub Check: Codacy Static Code Analysis
- GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
- GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
🧰 Additional context used
🧠 Learnings (3)
📚 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/common/os/linux/LinuxOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSProcess.javaoshi-common/src/main/java/oshi/hardware/common/platform/mac/MacNetworkIF.javaoshi-common/src/main/java/oshi/software/common/os/unix/solaris/SolarisOSProcess.javaoshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsNetworkIF.javaoshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsCentralProcessor.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxNetworkIF.javaoshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.javaoshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.javaoshi-common/src/main/java/oshi/software/common/os/windows/WindowsOSProcess.javaoshi-common/src/main/java/oshi/hardware/common/AbstractNetworkIF.javaoshi-common/src/main/java/oshi/software/common/AbstractOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/AbstractProcOSProcess.javaoshi-common/src/main/java/oshi/software/common/AbstractOSThread.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/common/os/linux/LinuxOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSProcess.javaoshi-common/src/main/java/oshi/hardware/common/platform/mac/MacNetworkIF.javaoshi-common/src/main/java/oshi/software/common/os/unix/solaris/SolarisOSProcess.javaoshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsNetworkIF.javaoshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsCentralProcessor.javaoshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxNetworkIF.javaoshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.javaoshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.javaoshi-common/src/main/java/oshi/software/common/os/windows/WindowsOSProcess.javaoshi-common/src/main/java/oshi/hardware/common/AbstractNetworkIF.javaoshi-common/src/main/java/oshi/software/common/AbstractOSProcess.javaoshi-common/src/main/java/oshi/software/common/os/unix/AbstractProcOSProcess.javaoshi-common/src/main/java/oshi/software/common/AbstractOSThread.java
📚 Learning: 2026-04-12T19:34:56.879Z
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:34:56.879Z
Learning: In OSHI’s Java platform implementations, `PhysicalProcessor.getEfficiency()` / `EfficiencyClass` follow the same convention as Windows `PROCESSOR_RELATIONSHIP.EfficiencyClass`: a *higher* numeric efficiency class value represents *greater performance* and *less efficiency* (typically P-cores higher than E-cores). When reviewing, do not treat cases where P-cores are assigned higher efficiency/class values as a bug—this is intentional cross-platform consistency.
Applied to files:
oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsCentralProcessor.javaoshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java
🔇 Additional comments (11)
oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxNetworkIF.java (1)
23-26: LGTM!oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacNetworkIF.java (1)
21-21: LGTM!oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsNetworkIF.java (1)
23-27: LGTM!oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java (1)
83-84: LGTM!oshi-common/src/main/java/oshi/software/common/os/unix/solaris/SolarisOSProcess.java (1)
42-45: LGTM!oshi-common/src/main/java/oshi/software/common/os/windows/WindowsOSProcess.java (1)
58-71: LGTM!oshi-common/src/main/java/oshi/software/common/AbstractOSThread.java (1)
28-50: LGTM!oshi-common/src/main/java/oshi/software/common/os/linux/LinuxOSProcess.java (1)
79-99: LGTM!oshi-common/src/main/java/oshi/software/common/os/unix/AbstractProcOSProcess.java (1)
52-58: LGTM!oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java (1)
63-76: LGTM!oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSProcess.java (1)
44-53: LGTM!
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3489 +/- ##
============================================
+ Coverage 71.57% 74.87% +3.30%
Complexity 76 76
============================================
Files 591 591
Lines 21583 21583
Branches 3614 3614
============================================
+ Hits 15448 16161 +713
+ Misses 4240 3447 -793
- Partials 1895 1975 +80
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:
|
… map - AbstractNetworkIF: the 10 constructor-only fields (networkInterface, name, displayName, index, mtu, mac, ipv4, subnetMasks, ipv6, prefixLengths) are write-once, so mark them final rather than volatile. - AbstractOSProcess.processID: write-once identity field -> final. - WindowsCentralProcessor.utilityBaseMultiplier: drop the redundant volatile; every access is already inside the synchronized lazilyCalculateMultiplier. - WindowsCentralProcessor.buildNumaNodeProcMap: build the map in a local variable and publish it to the volatile field as the final step, so readers never observe a partially populated map.
Summary
Honor the
@ThreadSafecontract on OSHI's mutable-snapshot HAL classes by giving their fields proper cross-thread visibility and safe publication.These classes (
OSProcess,OSThread, andNetworkIFfamilies, plus the dynamicCentralProcessorfrequency/cache fields) are annotated@ThreadSafebut are populated at construction and refreshed by publicupdateAttributes()/updateNetworkStats()calls, then read via getters with no memory synchronization. A concurrent refresh and read could observe stale, torn, or half-updated values — violating the annotation's contract ("no caller synchronization required").volatileon 112 reassigned mutable stat fields — per-field visibility without locking the hot read pathfinalon 12 write-oncememoize(...)Supplier fields (matching e.g.MacCentralProcessor.vendor, which was alreadyfinal)No behavior change and no locks added; this is a pure visibility/publication hardening.
CodeQL findings cleared
Clears three CodeQL Reliability categories for these classes in a single fix (
volatilesatisfies all three,finalcovers the write-once Suppliers):final/non-volatilefield not safely publishedfinalprotectedfield potentially escapingThe
WmiQueryHandlerFFMfindings in these categories are false positives (its state is aConcurrentHashMap-backed set andsynchronized/volatileprimitives) and are dismissed separately.Testing
./mvnw clean install(full reactor) plus spotless, checkstyle, forbiddenapis, and javadoc all pass.🤖 Generated with Claude Code
Summary by CodeRabbit