Skip to content

ConcurrentModificationException accessing discovered results #20

@farble1670

Description

@farble1670
java.util.ConcurrentModificationException
	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1441)
	at java.util.HashMap$KeyIterator.next(HashMap.java:1465)
	at java.util.Collections$UnmodifiableCollection$1.next(Collections.java:1089)
	at com.clover.nsd_stress.ZeroconfStressActivity.discover$lambda-5(ZeroconfStressActivity.kt:159)

I see the backing services field is a synchronized set, but according to the docs:

It is imperative that the user manually synchronize on the returned set when iterating over it.

However when the field is returned, it's wrapped in a Collections.unmodifiableCollection which just restricts write but not read operations on the wrapped collection. The caller has no idea they need to synchronize because they have no idea the result is a wrapped SynchronizedSet.

One option would be to have the getService() method synchronize on the source collection, making a copy, then return the copy. Or you could back it with a java.util.concurrent collection implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions