Skip to content

False positive AT_NONATOMIC_OPERATIONS_ON_SHARED_VARIABLE for long in private method called by synchronized method #3428

@garydgregory

Description

@garydgregory

Hi All:
CC @JuditKnoll

I think I am getting a false positive AT_NONATOMIC_OPERATIONS_ON_SHARED_VARIABLE for a long instance varibale update in a private method only called by synchronized methods.

To reproduce:

git clone https://github.com/apache/commons-pool.git
cd commons-pool
git checkout 5d41a5185a301e3d135a0b611e5cbc283a367654
mvn clean compile spotbugs:check -Dcommons.spotbugs.impl.version=4.9.3 -Dcommons.spotbugs.plugin.version=4.9.3.0

Produces:

[INFO] --- spotbugs:4.9.3.0:check (default-cli) @ commons-pool3 ---
[INFO] BugInstance size is 1
[INFO] Error size is 0
[INFO] Total bugs: 1
[ERROR] Medium: Operation on the "destroyCount" shared variable in "SoftReferenceObjectPool" class is not atomic [org.apache.commons.pool3.impl.SoftReferenceObjectPool] At SoftReferenceObjectPool.java:[line 269] AT_NONATOMIC_OPERATIONS_ON_SHARED_VARIABLE
[INFO] 

The instance variable org.apache.commons.pool3.impl.SoftReferenceObjectPool.destroyCount is updated at line 269 in the private method org.apache.commons.pool3.impl.SoftReferenceObjectPool.destroy(PooledSoftReference<T>), and that private method is only called from synchronized methods:

  • addObject()
  • borrowObject()
  • invalidateObject((T)
  • returnObject(T)

Once the synchronized methods exit, the updated destroyCount value is visible to other threads, which is why this looks like an FP.

The toString() method only reads the destroyCount value.

TY.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions