Skip to content

packet: copy zero copy data on the Packets channel path - #177

Open
hsdfat wants to merge 1 commit into
gopacket:masterfrom
hsdfat:fix/zerocopy-channel-copy-74
Open

hsdfat wants to merge 1 commit into
gopacket:masterfrom
hsdfat:fix/zerocopy-channel-copy-74

Conversation

@hsdfat

@hsdfat hsdfat commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #172, which was reverted in 11ee6a1. This takes the direction from the revert comment: never fail at runtime, and copy on the channel path instead.

Fixes #74

Problem

NewZeroCopyPacketSource never sets zeroCopy. With NoCopy set, Packets()/PacketsCtx() queue packets that alias the buffer the source reuses. Once the reader falls behind, those packets show the data (and AncillaryData) of a later read.

Change

  • NewZeroCopyPacketSource sets zeroCopy.
  • packetsToChannel decodes with NoCopy cleared when the source is zero copy. NewPacket then copies the data, and the existing ancillary-data copy in NextPacket applies too. This goes through a small nextPacket(opts) helper, so NextPacket itself is unchanged.
  • The panic in PacketsCtx is removed. Existing NoCopy channel callers keep working; their packets now own their data. The PacketsCtx doc comment says so.
  • Synchronous NextPacket with NoCopy still returns packets that use the source buffer directly.

Tests

  • TestZeroCopyPacketSourcePacketsCopiesData: a source that reuses one data buffer and one AncillaryData slice queues 5 packets before any are read, then checks each packet's data and ancillary data. On master every packet reads 5; with this change they read 1..5 and none aliases the buffer.
  • TestZeroCopyPacketSourceNextPacketNoCopy: NextPacket with NoCopy still aliases the source buffer.

go test -race . and go vet . pass, and gofmt is clean.

NewZeroCopyPacketSource never set zeroCopy, so the PacketsCtx check for
NoCopy on a zero copy source never ran. Turning that check on (gopacket#172)
made existing callers panic and was reverted.

Set zeroCopy, and on the Packets/PacketsCtx path decode with NoCopy
cleared for zero copy sources, so each queued packet owns its data and
ancillary data instead of aliasing the buffer the source reuses on the
next read. NextPacket keeps its zero copy behaviour. Remove the panic.

Fixes gopacket#74
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.

PacketSource zeroCopy field always false

1 participant