Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 326886 - Potential deadlock due synchronization on ConcurrentHashMap
Summary: Potential deadlock due synchronization on ConcurrentHashMap
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL: https://fisheye2.atlassian.com/browse...
Whiteboard: submitted_patch, concurrency
Keywords:
Depends on:
Blocks: 455043
  Show dependency tree
 
Reported: 2010-10-04 02:36 EDT by Martin JANDA CLA
Modified: 2022-06-09 10:31 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin JANDA CLA 2010-10-04 02:36:19 EDT
Build Identifier: 2.2.0 nightly 2010-10-02

There is synchronization on 'Map<String, Queue> preallocatedSequences' (line 38)
that is initialized as ConcurrentHashMap (line 68).
  This can lead to deadlock because ConcurrentHashMap has own synchronization mechanism. 

  Suggested fix:
add new synchronization field:
    protected Map<String, Queue> preallocatedSequences;
+    private final Object sync = new Object();

   if (sequences == null) {
-            synchronized (preallocatedSequences) {
+            synchronized (sync) {

Reproducible: Always
Comment 1 Michael OBrien CLA 2010-10-04 07:46:47 EDT
>code coordinate is org.eclipse.persistence.internal.sequencing.PreallocationHandler
https://fisheye2.atlassian.com/browse/eclipselink/trunk/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/sequencing/PreallocationHandler.java?r=HEAD#l38

>Q1: Why not use the existing intrinsic lock (this) of PreallocationHandler 
>instead of a new Object (sync) as replacement for the locking object in ConcurrentHashMap?
It may be that the lock is protected from use by other threads by making it private

>We should also investigate use of the new 1.5 Lock interface which allows us to interrupt locked threads
Comment 2 Tom Ware CLA 2010-10-04 08:24:48 EDT
Removing simple_fix annotation.  Simple fixes should not require a new regression test.

If possible, please give steps that will actually create a deadlock.
Comment 3 Martin JANDA CLA 2010-10-05 03:14:59 EDT
This is only potential deadlock. So I think it is not possible to reproduce it. Current code depends on internal implementation of ConcurrentHashMap.

I use several rules from my experience with multithread programming.

a) it is not recommended to synchronize on java.util.concurrent instances
   - I got deadlock for several times
   - FindBugs has warning about it
   - I think there will be some changes in java 7 in j.u.c

b) intrinsic lock (this) of PreallocationHandler is dangerous 
   because it is used for internal synchronization of preallocatedSequences but anybody can use instance of PreallocationHandler for synchronization.

c) I use 'private final Object sync...'
   i have 100% control on synchronization
   -> it would be better to have 'preallocatedSequences' with private access. Because class is package local and not final. It means anybody from package can extend the class and add object to Map with wrong synchronization.

---
 - I think there should be no specific test for this problem (standard test suite should cover this change)
 - PreallocationHandler is used only in SequencingManager
 - I tried to suggest better implementation (small change) to avoid problem that can be very difficult to locate and reproduce
 - new 1.5 Lock interface isn't necessary here
Comment 4 Tom Ware CLA 2010-10-05 08:21:36 EDT
Setting initial target and priority.  See the following page for details of the meanings of these fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines

Note: Since there is a submitted patch there is a reasonable chance that this bug will move up in the queue.
Comment 5 Eclipse Webmaster CLA 2022-06-09 10:15:28 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink
Comment 6 Eclipse Webmaster CLA 2022-06-09 10:31:16 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink