Issue Details (XML | Word | Printable)

Key: OSGI-453
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Costin Leau
Reporter: Rob Harrop
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Spring OSGi

Deadlock in DynamicCollection

Created: 22/Apr/08 05:18 AM   Updated: 22/Apr/08 09:08 AM
Component/s: CORE
Affects Version/s: 1.1 M1
Fix Version/s: 1.1 M2

Time Tracking:
Not Specified


 Description  « Hide
DynamicCollection.remove and DynamicCollection$DynamicIterator.hasNext() are deadlock prone. I expect this might be the case for some more interleaving code paths.

public boolean hasNext() {
synchronized (iteratorsLock) {
hasNext = (cursor < storage.size() ? Boolean.TRUE : Boolean.FALSE);
}

return hasNext.booleanValue();
}

public boolean remove(Object o) {
synchronized (storage) {
int index = storage.indexOf(o);

if (index == -1)
return false;

remove(index);
return true;
}
}

hasNext() is acquiring the monitor for iteratorsLock and then storage (since storage is a Collections.synchronizedList which by default using the collection's own monitor). remove() acquires storage and then iteratorsLock (via the call to remove(int)).

A point to be wary of - using a Collections.synchronizedList (or any class that 'supports' a client locking protocol) and then locking on it also. This leads to acquire ordering that is not apparent.

The solution to this is to enforce an acquistion order. So if you are going to call a method on storage while holding iteratorsLock, you'll want to acquire the storage lock *before* acquiring iteratorsLock. Of course, this assumes that you are expecting that operations on storage lock on 'this'.

 All   Comments   Work Log   Change History   FishEye   Builds      Sort Order: Ascending order - Click to sort in descending order
Costin Leau added a comment - 22/Apr/08 09:08 AM
I've revised the class so the raw collection is used with explicit locking and acquisition.