Skip to content

fix(arrow/ipc): keep zstd decoders with the reader that made them - #1630

Merged
zeroshade merged 2 commits into
apache:mainfrom
zeroshade:zstd-synctest-bubbles
Nov 20, 2026
Merged

zeroshade merged 2 commits into
apache:mainfrom
zeroshade:zstd-synctest-bubbles

Conversation

@zeroshade

Copy link
Copy Markdown
Member

Rationale for this change

Fixes #1629.

Since #1331, zstd decoders come from a process-wide sync.Pool. A zstd.Decoder owns a channel that every DecodeAll receives from, and a channel made inside a testing/synctest bubble must not be used outside that bubble. When a reader inside a bubble created a decoder, the pool could hand it to a reader outside the bubble or in another bubble, and the runtime aborted with fatal error: receive on synctest channel from outside bubble. A decoder made outside a bubble is safe to use inside one, so the crash only happens after a decoder was created in a bubble.

What changes are included in this PR?

  • Reader and FileReader each keep their own idle zstd decoders, and the process-wide decoder pool is removed. Go's HTTP/2 server made the same change for the same problem (net/http: http2 server use with synctest sometimes causes fatal error "select on synctest channel from outside bubble" golang/go#75674).
  • FileReader.RecordBatchAt may be called concurrently, so a FileReader keeps as many decoders as were in use at once.
  • Encoders stay pooled process-wide. They are only used through the streaming API, which keeps no channel between streams, so an encoder used inside a bubble is safe to reuse outside it. A comment on zstdEncoderPools records why EncodeAll must not be used there.

Cost, from BenchmarkReaderCompressed, which creates a new reader on every iteration: one decoder per reader, about 1.5 KiB and 11 allocations, with no measurable change in time.

Are these changes tested?

Yes.

Are there any user-facing changes?

Reading zstd-compressed IPC data inside testing/synctest bubbles no longer crashes the process. There is no API change.

- A zstd.Decoder owns a channel, and a channel made inside a testing/synctest
  bubble must not be used outside it. The process-wide decoder pool handed
  decoders made in a bubble to readers elsewhere, which aborted the process.
- Reader and FileReader now reuse their own decoders. FileReader keeps as
  many as were in use at once, since RecordBatchAt may run concurrently.
- Encoders stay pooled process-wide: they are only used through the
  streaming API, which keeps no channel between streams.

@amoeba amoeba left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine. Though it looks like there's an opportunity to do a little refactor rather than just the minimal change here. This PR makes readDictionary, loadRecordBatch, etc become aware of the shared Zstd decoders, e.g.,

-                       return readDictionary(&f.memo, msg.meta, msg.body, f.swapEndianness, f.mem)
+                       return readDictionary(&f.memo, msg.meta, msg.body, f.swapEndianness, f.mem, &f.zstd)

Could Reader and FileReader embed and internal IpcDecoder struct like,

type ipcDecoder struct {
	schema         *arrow.Schema
	memo           dictutils.Memo
	mem            memory.Allocator
	swapEndianness bool
	zstd           zstdDecoders
}

Dictionary and record loading could then be methods on it:

func (d *ipcDecoder) readDictionary(meta, body *memory.Buffer) (dictutils.Kind, error)

This simplifies the existing call site (which has already grown) to just r.readDictionary(msg.meta, msg.body).

Reader and FileReader embed an ipcDecoder holding the schema, dictionary
memo, allocator, endianness flag and zstd decoders. readDictionary,
newRecordBatch and loadRecordBatch become methods on it instead of
taking each of those as a parameter.
@zeroshade

Copy link
Copy Markdown
Member Author

Thanks @amoeba, good suggestion. Done in the second commit: Reader and FileReader now embed an ipcDecoder with the schema, memo, allocator, endianness flag and zstd decoders, and readDictionary, newRecordBatch and loadRecordBatch are methods on it. The call sites are down to r.readDictionary(msg.meta, msg.body), and the zstd decoders no longer appear in any signature outside getDecompressor.

@amoeba amoeba left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@zeroshade
zeroshade merged commit 9e9c059 into apache:main Nov 20, 2026
23 checks passed
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.

arrow/ipc: process-global zstd codec sync.Pools are incompatible with testing/synctest bubbles (fatal: receive on synctest channel from outside bubble)

2 participants