Repository navigation
Synchronize the network interface and power source refresh paths - #3654
Conversation
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.
|
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 (9)
Included review availability: Your plan includes up to 8 reviews per rolling hour; 6 remain after this review. 📜 Recent review details⏰ Context from checks skipped due to timeout. (11)
🧰 Additional context used📓 Path-based instructions (3)oshi-common/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:
🧠 Learnings (6)📚 Learning: 2026-04-12T19:22:09.716ZApplied to files:
📚 Learning: 2026-07-06T19:25:59.225ZApplied to files:
📚 Learning: 2026-07-30T00:35:22.638ZApplied 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:
🔇 Additional comments (9)
📝 WalkthroughWalkthroughThe change documents power-source update behavior and synchronizes power-source and platform-specific network-interface refresh methods. Existing data retrieval, field updates, failure handling, and return values remain unchanged. ChangesThread-safe hardware updates
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to This PR synchronizes network interface and power source refresh writes to prevent concurrent state updates without changing the public behavior contract. No actionable merge-blocking risk remains; it is merge-ready after normal checks and review. 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 |
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.
Third chunk of the
updateAttributes()consistency arc, following #3586 and #3653. Same standard: the lock goes on the method that writes the fields, with no suppression beside it.After this, every refreshable type is covered except
OSProcessandOSThread.NetworkIF: no shared writer, so seven locks
Unlike
OSFileStore— where #3651 had already madeAbstractOSFileStorethe sole writer, so #3653 needed one edit for ten implementations — eachNetworkIFplatform class assigns the counters itself. Six take the lock on their ownupdateAttributes().macOS shows why the writer is the right target
Its fields are written by
updateNetworkStats(Map), which is reached from two places:Locking only
updateAttributes()would leave the constructor path unguarded, so the lock goes onupdateNetworkStats. That also keeps the nativequeryIFdatacall outside the lock, since the public method queries first and writes after.This is the same shape the arc flagged for
OSProcess, whereupdateAttributes(Map)is called from subclass constructors and the public no-arg is the wrong place to lock.Every platform reaches its writes from a constructor
Worth stating because it is easy to miss: the six
oshi-commonbases callupdateAttributes()from their own constructors, and AIX and Solaris additionally call it from their JNA and FFM subclass constructors (AixNetworkIFJNA,AixNetworkIFFFM,SolarisNetworkIFJNA,SolarisNetworkIFFFM). All of those pass through the method now locked. Locking an object that has not been published yet is uncontended and harmless; leaving that path unlocked while locking the public one would not have been.Locking safety
Checked rather than assumed, as in #3653:
synchronized, no explicitLock.synchronizedblock.So there is no ordering hazard.
PowerSource
The last interface carrying no thread-safety contract —
OSProcess,OSThread,NetworkIF,HWDiskStoreand nowOSFileStoreall have it. It gains the externally-synchronize paragraph, plus the one way it genuinely differs from the others: a failed update leaves the source unchanged, because it assigns nothing unless it finds a source matching this one's name. The other refreshable types set a sentinel or partial state on failure.AbstractPowerSource.updateAttributes()is the sole writer and takes the lock.Verification
Full gate and
-Perror-proneclean, all tests passing.synchronizedis not part of the method signature, sojapicmpis unaffected. No CHANGELOG entry: this closes a race no OSHI-internal caller can reach, and the rest is documentation.Summary by CodeRabbit