Repository navigation
fix: Hold the local candidates lock in Component.findLocalCandidate - #324
Conversation
findLocalCandidate iterated over the localCandidates LinkedList without the lock that all other accesses use. When two connectivity check responses for the same component are processed at the same time and one of them adds a peer reflexive candidate, the other one can fail with a ConcurrentModificationException, and its response is dropped. Iterate with the lock held, in one pass with the same result as before. Keep the locked section to plain field reads. Log the fallback warning after the lock is released, so that Candidate.toString() and the related candidate lookup that it can do run without the lock held.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #324 +/- ##
============================================
+ Coverage 43.19% 43.68% +0.49%
- Complexity 1409 1427 +18
============================================
Files 166 166
Lines 11207 11209 +2
Branches 1662 1663 +1
============================================
+ Hits 4841 4897 +56
+ Misses 5733 5687 -46
+ Partials 633 625 -8
... and 14 files with indirect coverage changes Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
JonathanLennox
left a comment
There was a problem hiding this comment.
Looks good — the merged loop is equivalent to the old two-loop behavior, and I agree there's no new lock-order risk. Confirmed the new test fails with a CME without the Component change and passes with it.
A few non-blocking nits:
- The comment about not calling
toString()under the lock overstates it: the re-entrantfindLocalCandidate()call is on the same thread/component and the monitor is reentrant (andComponent.toString()already callscand.toString()under this lock). Logging outside is still nicer for keeping the critical section short — maybe say that instead, and soften "do not … call toString()" to a preference. - Since the inline comment describing the fallback is gone, could the javadoc for
findLocalCandidate(address, base)mention that if no candidate matches both, it returns the first (highest-priority) address match and logs a warning? - The test class javadoc only describes the concurrent case; two of the three tests are single-threaded base-match/fallback tests.
- Minor:
awaitAddedOrBlockedaccepts BLOCKED on any monitor, so in principle the regression test could pass against unfixed code if the adder blocked elsewhere first. Probably not worth tightening.
Not for this PR, but related: in ConnectivityCheckClient.processSuccessResponse, two concurrent responses with the same mapped address can both create a peer reflexive candidate; the second addLocalCandidate returns false (redundant) but its candidate is still used as validLocalCandidate, so the valid pair refers to a candidate not in the component. Also Agent.findLocalCandidate and IceMediaStream.findLocalCandidate iterate mediaStreams/components without their locks.
Describe the fallback to an address-only match in the javadoc, explain logging after the lock as a preference that keeps the critical section short, and describe all three tests in the test class javadoc.
|
Thanks! Addressed in 9869c40:
I left the BLOCKED check as is. The redundant peer reflexive candidate in |
Component.findLocalCandidate(address, base)iterated over thelocalCandidatesLinkedListwithout a lock. All other accesses to the list usesynchronized (localCandidates).Two connectivity check responses for the same component can be processed at the same time on different threads. If one of them adds a peer reflexive candidate (
addLocalCandidateadds to the list and sorts it), the other one can fail with aConcurrentModificationException:NetAccessManager.handleFatalErrorcatches the exception, so only that response is lost. We saw this with a client whose responses contained a NAT64 address of the server, so each early response created a peer reflexive candidate.Changes:
Candidate.toString()can look up the related candidate, which callsfindLocalCandidate()again.ComponentFindLocalCandidateTest. One test adds a candidate from a second thread whilefindLocalCandidateiterates; it fails with the exception above without the fix. Two tests cover the base match and the fallback to an address-only match.Lock order: with the lock held,
findLocalCandidatereads only fields (getTransportAddress,getBase,TransportAddress.equals,Candidate.equals) and takes no other lock. The callers in ice4j (ConnectivityCheckClient.processSuccessResponse,Agent.incomingCheckReceived, the relayed candidate constructors,LocalCandidate.findRelatedCandidate) hold none of the locks thatComponent.free()takes underlocalCandidates. So the change adds no lock cycle.