Details
-
Bug
-
Status: Open
-
Major
-
Resolution: Unresolved
-
6.0M4, 5.0M16
-
None
-
None
Description
If you call ConcurrentHashMap.keySet().toArray() as done in trunks Lucene's RAMDirectory#fileMap and another thread modifies the collecton, you get NoSuchElementExceptions or AIOOBEs. The reason is that AbstractCollection.toArray methods allocate the array using the current size of the collection and then either iterate on the values (which is oficially supported by ConcurrentHashMap, its iterator is safe for concurrent modification!) and set them on the target without checking bounds. On the other hand if there are too few entries in the collection, the array is nulled at the end. The bad thing is that the toArray() and toArray(T[]) methods behave differently, so depending on the modification they throw different exceptions (one impl simply drops entries that dont fit into the array).
Since Java SE 6, its officially documented, how toArray must behave: http://download.oracle.com/javase/6/docs/api/java/util/AbstractCollection.html#toArray(T[]); This is violated by harmony and even the fix in HARMONY-6236 does not respect it, leading to such bugs.
Java SE 5 does not document that, but ConcurrentHashMap.keySet(), values() and entrySet() have own implementations of toArray() (which can be found out by reflection), the root AbstractCollection.toArray() is not behaving fine there.
For harmony the fix would be to correct this issue "correctly" by changing the base AbstractCollection implementation (I would recommend that).
This is also the root cause for HARMONY-6236 and similar issues, the fix there is invalid, but I cannot reopen the issue.
Attachments
Issue Links
- breaks
-
HARMONY-6236 [classlib][luni] ArrayList breaks when given concurrent collections as input
-
- Closed
-
- is related to
-
HARMONY-6678 [classlib][concurrent]ArrayIndexOutofBounds exception reported in ConcurrentSkipListSet during load test
-
- Open
-
- relates to
-
LUCENE-2779 Use ConcurrentHashMap in RAMDirectory
-
- Closed
-