Skip to content

Data Race between Batch.tryAddFirstEvent and Batch.unlockAndSealIfNeeded #96

Open
@van-closed

Description

@van-closed

Short Description

When Emitter.emitAndReturnBatch is called concurrently, it is possible that the thread running Batch.tryAddFirstEvent
gets context switched out after writing the event to buffer and incrementing the event count but
before Batch.firstEventTimeStamp is set. 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();
    }
  }
  private void unlockAndSealIfNeeded()
  {
    if (eventCount.incrementAndGet() >= emitter.config.getFlushCount()) {
      unlockAndSeal();
    } else {
      firstEventLock.readLock().lock();
      long firstEventTimestampValue = firstEventTimestamp;
      firstEventLock.readLock().unlock();
      long timeSinceFirstEvent = System.currentTimeMillis() - firstEventTimestampValue;
      if (firstEventTimestampValue > 0 && timeSinceFirstEvent > emitter.config.getFlushMillis()) {
        unlockAndSeal();
      } else {
        unlock();
      }
    }
  }

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