Skip to content

LocalNodeRecordStore possible concurrency issue when updated it on DiscoveryManagerImpl #190

@meldsun0

Description

@meldsun0

Given this method on LocalNodeRecordStore:

    public void onCustomFieldValueChanged(String fieldName, Bytes value) {
        NodeRecord oldRecord = this.latestRecord;
        NodeRecord newRecord = oldRecord.withUpdatedCustomField(fieldName, value, this.secretKey);
        this.latestRecord = newRecord;
        this.recordListener.recordUpdated(oldRecord, newRecord);
    }

There can be a concurrency issue even though latestRecord is declared volatile:


NodeRecord oldRecord = this.latestRecord;
// ...create newRecord based on oldRecord...
this.latestRecord = newRecord;

If two threads call this method at the same time, they may:

Both read the same oldRecord.

Both compute a different newRecord.

Both assign a latestRecord, but the second thread silently overwrites the first one's update.

Suggestions:

  • Use synchronized block
  • Use AtomicReference

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