United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6701459 Synchronization bug pattern found in javax.management.relation.RelationService
JDK-6701459 : Synchronization bug pattern found in javax.management.relation.RelationService

Details
Type:
Bug
Submit Date:
2008-05-13
Status:
Closed
Updated Date:
2011-03-08
Project Name:
JDK
Resolved Date:
2011-03-08
Component:
core-svc
OS:
generic
Sub-Component:
javax.management
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports
Relates:

Sub Tasks

Description
William Pugh reports that FindBugs flags the following code from RelationService:

    private Long myNtfSeqNbrCounter = new Long(0);
    ...
    private Long getNotificationSequenceNumber() {
	Long result = null;
	synchronized(myNtfSeqNbrCounter) {
	    result = new Long(myNtfSeqNbrCounter.longValue() + 1);
	    myNtfSeqNbrCounter = new Long(result.longValue());
	}
	return result;
    }

This is a well-known bug pattern.  If two threads arrive at the synchronized block at the same time then they will both synchronize on the same Long object.  Whichever of them gets the lock on it first will then update myNtfSeqNbrCounter with a new Long object, but the Java Memory Model does not guarantee that the other one will then see the updated field when it calls myNtfSeqNbrCounter.longValue().

                                    

Comments
SUGGESTED FIX

Use an AtomicLong.
                                     
2008-05-13
EVALUATION

In practice this bug will have zero effect because the Relation Service is very little used; even when it is used there is very little likelihood of parallel updates; and even if the bug is triggered it just means that sequence numbers might repeat.  (Almost no clients depend on sequence numbers.)  However the fact that this bug is flagged by FindBugs means that we should fix it to avoid adding noise that might hide a more serious bug.
                                     
2008-05-13



Hardware and Software, Engineered to Work Together