Skip to content

fix(binding-kafka): fold produce entry claim block into trailing padding - #3540

Merged
jfallows merged 1 commit into
developfrom
fix/kafka-cache-compaction-clean-unmapped-access
Nov 24, 2026
Merged

jfallows merged 1 commit into
developfrom
fix/kafka-cache-compaction-clean-unmapped-access

Conversation

@jfallows

@jfallows jfallows commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

KafkaCachePartition.writeProduceEntryStart reserves a second trailersSizeMax block after an entry's trailers, used as scratch by the transform envelope. writeProduceEntryFin did not account for it in the entry's trailing paddingLen, so a produce entry's sizeof() ended trailersSizeMax bytes before the next entry, leaving a zero-filled gap between entries in the log.

Code that walks the log by limit() lands in that gap. KafkaCachePartition.Node.clean read a zeroed entry there, saw deltaPosition == 0 rather than the -1 sentinel, and read the delta file at position 0. That file is empty, so its mapping has address 0 and the JVM crashed with SIGSEGV (UnsafeBufferEx.getInt <- KafkaCacheDeltaFW.wrap <- KafkaCacheFile.readBytes <- Node.clean). findAndAbortProducerId walks the log the same way.

This is not a mapped region read after IoUtil.unmap(): the segment was alive and mapped, and the entry boundaries were wrong.

The fix folds the claim block into the trailing paddingLen/padding in writeProduceEntryFin, as writeEntryFinish already does for the populate path, so every entry is contiguous with the next. The on-disk layout is unchanged: trailers are still length + bytes + paddingLen + padding, and the padding is now accounted for.

Evidence

  • Reproduced 4 times locally (1 worker pinned to one vCPU, 8,000 publishers at 1 msg/s, compacted topics with small segment.ms / min.cleanable.dirty.ratio), crashing 110-135 s after the publishers connected.
  • An in-process dump at the crash showed the first produce entry with size=1491 limit=1491, zero bytes from 1491 to 1746, and the next real entry at 1747. The 256-byte gap equals trailersSizeMax.
  • After the fix, 12m54s under the same load with no crash. Produce-side segments were compacted by clean three times in that window (base offsets 0, 1223426 and 3058535).

Tests

  • KafkaCachePartitionTest.shouldDelimitProduceEntriesByLimit writes two produce entries and walks them by limit(). Against develop it fails on a deterministic assertion, with no JVM crash (expected:<360> but was:<296>, a 32-byte gap per entry); with the fix it passes.
  • shouldWriteProduceEntryWithPinnedFileContents pinned the old, mis-delimited padding length (0x18). It now pins 0x38, which includes the claim block.
  • ./mvnw clean verify passes for runtime/binding-kafka and specs/binding-kafka.spec.

Existing segments on disk

Segments written before this change are never read back, so no handling for them is added. The cache does not reload segment files at startup:

  • A fetch or produce KafkaCachePartition starts with an empty segment chain and its constructor only creates the directory (KafkaCachePartition.java:161-172 for fetch, :212-226 for produce). Nothing in the module lists or scans the cache directory.
  • Every segment is created through KafkaCachePartition.append (:283), whose KafkaCacheFile constructor calls mapCreateAppend, which deletes any file at that path before creating it (KafkaCacheFile.java:359-368). The read-only KafkaCacheSegment constructor is used only by freeze() on files just written (KafkaCacheSegment.java:216).
  • Only the share-staging constructor wipes its directory (KafkaCachePartition.java:196). Fetch and produce directories are not wiped, so leftover files with other base offsets stay on disk unread until overwritten.

Introduced by

91144fa5d (#2457, 2026-08-30) added the claim block to writeProduceEntryStart for every produce entry (not only entries with a transform). It left writeProduceEntryFin unchanged apart from a new flag. trailersClaimMark is present in tag 2.4.0 and absent from 2.3.0 and 1.3.4.

Fixes #3536

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoYAZzKbDRpztRerQUz

writeProduceEntryStart reserves a second trailersSizeMax block after the
entry's trailers as scratch for the transform envelope, but
writeProduceEntryFin only accounted for the trailers region in the entry's
trailing paddingLen. The entry's sizeof() therefore ended short of the next
entry by trailersSizeMax bytes, leaving a zero-filled gap between entries.

Anything that walks the log by entry limit() (KafkaCachePartition.Node.clean,
findAndAbortProducerId) landed inside the gap and read a zeroed entry whose
deltaPosition of 0 is not the -1 sentinel. Compaction then read the empty
delta file at position 0, a zero-length mapping, and the JVM crashed with
SIGSEGV.

Fold the claim block into the trailing paddingLen/padding in
writeProduceEntryFin, as writeEntryFinish does for the populate path, so each
entry is contiguous with the next. Add a test that walks produce entries by
limit() and update the pinned produce entry bytes to the new padding length.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoYAZzKbDRpztRerQUz
@jfallows
jfallows merged commit b932740 into develop Nov 24, 2026
53 checks passed
jfallows pushed a commit to aklivity/mqtt-benchmark that referenced this pull request Nov 24, 2026
…ess and evidence

Repro scripts, crash logs and before/after results for the produce-side
segment compaction crash fixed by aklivity/zilla#3540.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoYAZzKbDRpztRerQUz
jfallows pushed a commit to aklivity/mqtt-benchmark that referenced this pull request Nov 24, 2026
* Run Zilla 2.8.0-rc8 by default

2.8.0-rc8 includes aklivity/zilla#3540, which fixes the JVM crash in the Kafka cache's
produce-side segment compaction (aklivity/zilla#3536), and the QoS 2 fix from
aklivity/zilla#3309.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Remove Zilla issue notes from the README and the QoS 2 delivery check

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Drop the stale shared subscription note and reword the lower connection rate scenarios

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kafka cache: JVM crashes with SIGSEGV in KafkaCachePartition.Node.clean during produce-side segment compaction (2.8.0-rc6)

2 participants