Repository navigation
Synchronize the disk and file store refresh paths - #3653
Conversation
…itten
setSpace assigned each volatile and then read it back to bound the next one, so
the method that exists to guarantee an ordering was itself a read-modify-write.
Two concurrent refreshes could interleave and leave a triple violating it:
A: total = 100
A: free = min(50, 100) = 50
B: total = 10
A: usable = min(30, this.freeSpace = 50) = 30
-> total=10, free=50, usable=30, and free > total
Compute the three into locals and assign once. The clamps then bound values from
a single reading, whatever else is writing concurrently.
Continues the updateAttributes() consistency arc from #3586, which established the pattern on the BSD process and thread implementations: the lock goes on the method that writes the fields, and no suppression is kept alongside it. Coverity and Sonar are both clean on that shape now, so it is the standard rather than a proposal. The file store side needed only one file. #3651 made AbstractOSFileStore the sole writer of the mutable fields -- no platform class assigns them -- so locking its three update entry points covers all ten implementations at once. setSpace stays private and is only reached from them, hence unlocked. HWDiskStore has no such choke point: eight of its platform classes write the counters directly, so each updateAttributes() takes the lock itself. That matches AixHWDiskStore, which has been the only synchronized one since #3586 and whose monotonic clamps are read-modify-writes that the lock now protects everywhere rather than in one place. Verified no implementation acquires a second lock inside the method, and that every caller is a freshly constructed store inside a getDisks() factory holding nothing, so there is no ordering hazard. OSFileStore was one of two interfaces carrying no thread-safety contract; it now states the externally-synchronize guidance the other four have, the space ordering it guarantees, and the prefer-the-full-list advice. PowerSource, the other, comes with the network chunk.
|
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 (12)
Included review availability: Your plan includes up to 8 reviews per rolling hour; 7 remain after this review. 📜 Recent review details⏰ Context from checks skipped due to timeout. (12)
🧰 Additional context used📓 Path-based instructions (5)oshi-core-ffm/src/main/java/**/*.java📄 CodeRabbit inference engine (AGENTS.md)
Files:
**/src/main/java/**/*.java📄 CodeRabbit inference engine (AGENTS.md)
Files:
**📄 CodeRabbit inference engine (AGENTS.md)
Files:
oshi-core/src/main/java/**/*.java📄 CodeRabbit inference engine (AGENTS.md)
Files:
oshi-common/src/main/java/**/*.java📄 CodeRabbit inference engine (AGENTS.md)
Files:
🧠 Learnings (9)📚 Learning: 2026-07-06T19:25:59.225ZApplied to files:
📚 Learning: 2026-07-30T00:35:22.638ZApplied to files:
📚 Learning: 2026-08-02T23:56:23.866ZApplied to files:
📚 Learning: 2026-08-03T00:46:14.953ZApplied to files:
📚 Learning: 2026-08-15T14:25:48.170ZApplied to files:
📚 Learning: 2026-08-15T15:43:29.296ZApplied to files:
📚 Learning: 2026-08-15T15:59:20.875ZApplied to files:
📚 Learning: 2026-04-06T04:45:47.644ZApplied to files:
📚 Learning: 2026-04-12T19:22:09.716ZApplied to files:
🔇 Additional comments (12)
📝 WalkthroughWalkthroughThe change synchronizes file-store and disk-store attribute updates across common, FFM, and JNA platform implementations. File-store space calculations now use local clamped values before publishing fields. The ChangesState refresh synchronization
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to This change synchronizes disk and file-store refresh updates and documents their thread-safety contract; no actionable merge-blocking risk remains beyond normal checks and review. 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 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3653 +/- ##
============================================
- Coverage 80.27% 80.18% -0.10%
- Complexity 4083 4442 +359
============================================
Files 701 701
Lines 24762 24765 +3
Branches 4135 4135
============================================
- Hits 19877 19857 -20
- Misses 2731 2747 +16
- Partials 2154 2161 +7
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:
|
Third chunk of the updateAttributes() consistency arc, following #3653. Same standard: the lock goes on the method that writes the fields, with no suppression beside it. NetworkIF has no shared writer, so six platforms take the lock on their own updateAttributes(), which is where they assign the counters. macOS is the exception and shows why the writer is the right target rather than the public method. Its fields are written by updateNetworkStats(Map), reached both from updateAttributes() and directly from the MacNetworkIfJNA and MacNetworkIfFFM constructors, which populate an interface from a bulk query. Locking only the public method would leave the constructor path unguarded, so the lock goes on updateNetworkStats. That also keeps the native queryIFdata call outside the lock, since updateAttributes queries first and writes after. Every other platform reaches its writes from a constructor too -- the six oshi-common bases from their own, and AIX and Solaris additionally from their JNA and FFM subclasses -- and all of those go through the method now locked. Verified no body acquires a second lock and no call site sits inside a synchronized block, so there is no ordering hazard. PowerSource was the last interface carrying no thread-safety contract. It gains the externally-synchronize paragraph the other five have, plus the one way it genuinely differs: a failed update leaves the source unchanged, because it assigns nothing unless it finds a source matching this one's name. With this the arc covers every refreshable type except OSProcess and OSThread.
Final chunk of the updateAttributes() consistency arc begun in #3586, after #3653 and #3654. Every refreshable type is now covered. This is the phase the arc flagged as highest risk, for two reasons that both turned out to be answered by locking the writer rather than the public method. Where a class writes its fields in an overload -- AIX process, and Windows process and thread -- the lock goes there, matching what #3586 did for the BSD pair. That is not a stylistic choice: every one of those overloads is called from a subclass constructor doing bulk population, so locking only the public no-arg would leave the list-building path unguarded while appearing to cover it. It also keeps the expensive work outside the lock. The Windows twins do all their registry, performance counter and WTS querying in the public method before delegating, so no WMI or COM call happens under the monitor -- verified, not assumed. The BSD pair likewise spawns ps outside it. Where a class writes inline instead, with no separate writer to target -- Solaris process and thread, Linux process and thread, AIX thread, and both macOS process backends -- the no-arg takes the lock and its queries come with it. macOS interleaves proc_pidinfo, proc_pidpath and getpwuid with the assignments, so there is nothing to separate; this matches the six NetworkIF platforms in #3654. WindowsOSThreadJNA and WindowsOSThreadFFM need nothing: they write no fields and delegate to the base overload now locked. Verified no locked body acquires a second lock, so there is no ordering hazard. Not included: hoisting BsdOSProcess.monotonic to share with AixHWDiskStore's inline clamps, the arc's last open item. Both are now lock-protected, so it is purely a duplication question, and it needs a home neutral to both hierarchies.
Second chunk of the
updateAttributes()consistency arc begun in #3586, taking the disk and file store implementations. Also fixes a latent race introduced with the space invariant in #3651.The standard, now settled
#3586 established the pattern on
BsdOSProcessandBsdOSThread: the lock goes on the method that writes the fields, and no suppression is kept beside it. Its open question was whether Sonar'sjava:S3078was syntactic and would re-fire under the lock. It does not — Sonar reports 0 open issues and Coverity is clean, with noS3078orVOLATILE_ATOMICITYsuppressions left anywhere in the tree. So this is the standard rather than a proposal, and the remaining chunks can follow it without re-deciding.Before this PR, 3 of ~45 implementations used it. After, 14.
The latent race (first commit)
setSpaceassigned each volatile and then read it back to bound the next:So the method that exists to guarantee
usable <= free <= totalwas itself a read-modify-write, and two concurrent refreshes could interleave to violate it:Computing the three into locals and assigning once removes the read-back entirely, so the clamps bound values from a single reading regardless of what else is writing. This stands on its own and is separated as the first commit.
File stores: one file, ten implementations
#3651 made
AbstractOSFileStorethe sole writer of the mutable fields — no platform class assigns them — so locking its threeupdate*entry points covers every implementation at once.setSpacestays private, reached only from those three, and therefore needs no lock of its own.Disks: no such choke point
Eight of the eleven
HWDiskStoreplatform classes write the counters directly, so each takes the lock on its ownupdateAttributes(). That matchesAixHWDiskStore, synchronized since #3586 and the only one until now; its monotonic clamps (if (stats.xfers > getReads())) are read-modify-writes that the lock now protects on every platform rather than one.Locking safety
Two things checked rather than assumed, since this is where the arc could go wrong:
synchronized, no explicitLock.store.updateAttributes()site is a freshly constructed store inside agetDisks()-style factory.So there is no lock-ordering hazard to reason about.
Documentation
OSFileStorewas one of two interfaces with no thread-safety contract at all —OSProcess,OSThread,NetworkIFandHWDiskStorealready carried the externally-synchronize paragraph. It now states that, the space ordering it guarantees, and the prefer-the-full-list advice the others give.PowerSource, the other one, comes with the network chunk.Not included
The shared
monotonic(long, long)hoist, unifyingAixHWDiskStore's inline clamps withBsdOSProcess.monotonic. Those sit in different chunks, and the clamps are now lock-protected either way; it belongs wherever the process/thread work lands.Verification
Full gate and
-Perror-proneclean, all tests passing.synchronizedis not part of the method signature, sojapicmpis unaffected. No CHANGELOG entry: this fixes a race no OSHI-internal caller can reach, and the rest is documentation.Summary by CodeRabbit
Bug Fixes
Documentation