Skip to content

fix: Hold the local candidates lock in Component.findLocalCandidate - #324

Merged
bgrozev merged 3 commits into
masterfrom
fix-find-local-candidate-lock
Nov 17, 2026
Merged

bgrozev merged 3 commits into
masterfrom
fix-find-local-candidate-lock

Conversation

@bgrozev

@bgrozev bgrozev commented Nov 16, 2026

Copy link
Copy Markdown
Member

Component.findLocalCandidate(address, base) iterated over the localCandidates LinkedList without a lock. All other accesses to the list use synchronized (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 (addLocalCandidate adds to the list and sorts it), the other one can fail with a ConcurrentModificationException:

java.util.ConcurrentModificationException
	at java.base/java.util.LinkedList$ListItr.next(LinkedList.java:899)
	at org.ice4j.ice.Component.findLocalCandidate(Component.java:1000)
	at org.ice4j.ice.IceMediaStream.findLocalCandidate(IceMediaStream.java:530)
	at org.ice4j.ice.Agent.findLocalCandidate(Agent.java:1467)
	at org.ice4j.ice.ConnectivityCheckClient.processSuccessResponse(ConnectivityCheckClient.java:572)

NetAccessManager.handleFatalError catches 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:

  • Iterate with the lock held. The two loops become one pass with the same result: the first candidate that matches the address and the base, or else the first candidate that matches the address.
  • Log the fallback warning after the lock is released. Candidate.toString() can look up the related candidate, which calls findLocalCandidate() again.
  • Add ComponentFindLocalCandidateTest. One test adds a candidate from a second thread while findLocalCandidate iterates; 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, findLocalCandidate reads 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 that Component.free() takes under localCandidates. So the change adds no lock cycle.

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

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 43.68%. Comparing base (172f341) to head (9869c40).

Additional details and impacted files

Impacted file tree graph

@@             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     
Files with missing lines Coverage Δ
src/main/java/org/ice4j/ice/Component.java 60.47% <100.00%> (+2.53%) ⬆️

... and 14 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 172f341...9869c40. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JonathanLennox JonathanLennox left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-entrant findLocalCandidate() call is on the same thread/component and the monitor is reentrant (and Component.toString() already calls cand.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: awaitAddedOrBlocked accepts 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.
@bgrozev

bgrozev commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

Thanks! Addressed in 9869c40:

  • The comment now says that logging after the lock is a preference that keeps the critical section short.
  • The javadoc of findLocalCandidate(address, base) describes the fallback to the first (highest-priority) address match and the warning.
  • The test class javadoc describes all three tests.

I left the BLOCKED check as is. The redundant peer reflexive candidate in processSuccessResponse and the unlocked mediaStreams/components lookups will go in a separate PR.

@bgrozev
bgrozev merged commit 05f5c66 into master Nov 17, 2026
7 checks passed
@bgrozev
bgrozev deleted the fix-find-local-candidate-lock branch November 17, 2026 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants