Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
van-closed opened this issue Mar 23, 2025 · 0 comments
Open

Comments

@van-closed
Copy link

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant