Skip to content

Data Race between Batch.tryAddFirstEvent and Batch.sealIfFlushNeeded #97

Open
@van-closed

Description

@van-closed

Short Description

It is possible for the thread calling Emitter.emit and the EmittingThread to race on the
firstEventTimestamp field. If the Emitter.emit thread was running Batch.tryAddFirstEvent
and gets context switched out after writing the event to the buffer
and incrementing the event count but before setting firstEventTimestamp, this can cause
timeSinceFirstEvent to be inaccurate, leading to the batch not being sealed within the configured
flushMillis.

  private boolean tryAddFirstEvent(byte[] event)
  {
    if (!tryReserveFirstEventSizeAndLock(event)) {
      return false;
    }
    try {
      firstEventLock.writeLock().lock();
      int bufferOffset = emitter.batchingStrategy.writeBatchStart(buffer);
      writeEvent(event, bufferOffset);
      eventCount.incrementAndGet();
      firstEventTimestamp = System.currentTimeMillis();
      return true;
    }
    finally {
      firstEventLock.writeLock().unlock();
      unlock();
    }
  }
  void sealIfFlushNeeded() {
    long timeSinceFirstEvent = System.currentTimeMillis() - firstEventTimestamp;
    if (firstEventTimestamp > 0 && timeSinceFirstEvent > emitter.config.getFlushMillis()) {
      seal();
    }
  }

Stack Trace

Stack Trace

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