This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git
The following commit(s) were added to refs/heads/main by this push:
new 7bb62b71 fix(arrow/ipc): release partially decoded arrays on loader
errors (#1650)
7bb62b71 is described below
commit 7bb62b713e34642260b765883db3425fe07bef0e
Author: Ayaan <[email protected]>
AuthorDate: Wed Nov 25 00:01:08 2026 +0530
fix(arrow/ipc): release partially decoded arrays on loader errors (#1650)
### Rationale for this change
Part of #77. After #1569, three inputs in arrow-testing's
`arrow-ipc-stream` corpus still leave checked-allocator allocations
outstanding. The array loaders register buffer cleanup only after their
last `ctx.buffer()` call, and struct and union loaders register child
cleanup only after all children load. When a later read fails, buffers
and child arrays the loader already holds are never released.
### What changes are included in this PR?
- Every loader that reads more buffers after `loadCommon` (primitive,
binary, binary view, fixed-size binary, map, list, list view, union) now
registers `defer func() { memory.ReleaseBuffers(buffers) }()` right
after `loadCommon`. The closure releases whatever `buffers` holds when
the function returns, including the case where it panics.
- `loadStruct` and `loadUnion` register child cleanup before loading
children and skip children that were never loaded.
- The pre-1.0.0 union validity panic is unchanged. Its validity buffer
is now released by the deferred cleanup.
- `TestLoaderReleasesOnError`: a checked-allocator case for each path
above except binary view.
- `TestStreamFuzzRegression`: reads every `arrow-ipc-stream` input with
a checked allocator and fails on an escaped panic, an unexpected success
or error, or outstanding allocations. It skips when `ARROW_TEST_DATA` is
unset.
Two corpus inputs that Go reads without error are rejected by Arrow C++
(invalid flatbuffer, decimal precision 0). The test records the current
Go behavior for them. I can open a separate issue if Go should reject
them too.
### Are these changes tested?
Yes. Each `TestLoaderReleasesOnError` case fails on main and passes with
this change. `TestStreamFuzzRegression` passes for all 84 inputs at the
pinned arrow-testing (`6a7b02fa`); without the loader change, 3 of them
leak. `go test -race ./arrow/ipc/...`, `go test ./arrow/flight/...` and
`go vet ./arrow/ipc/...` pass.
### Are there any user-facing changes?
No. Readers that hit these decode errors no longer leak memory.
---------
Signed-off-by: Ayaan <[email protected]>
---
arrow/internal/dictutils/dict.go | 4 +-
arrow/ipc/file_reader.go | 47 ++++++++++++----------
arrow/ipc/fuzz_regression_test.go | 79 +++++++++++++++++++++++++++++++++++++
arrow/ipc/loader_leak_test.go | 83 +++++++++++++++++++++++++++++++++++++++
4 files changed, 191 insertions(+), 22 deletions(-)
diff --git a/arrow/internal/dictutils/dict.go b/arrow/internal/dictutils/dict.go
index a90058f1..d8a3a22b 100644
--- a/arrow/internal/dictutils/dict.go
+++ b/arrow/internal/dictutils/dict.go
@@ -277,7 +277,6 @@ func (memo *Memo) reify(id int64, mem memory.Allocator)
(arrow.ArrayData, error)
defer arr.Release()
toCombine = append(toCombine, arr)
- defer data.Release()
}
combined, err := array.Concatenate(toCombine, mem)
@@ -288,6 +287,9 @@ func (memo *Memo) reify(id int64, mem memory.Allocator)
(arrow.ArrayData, error)
combined.Data().Retain()
memo.id2dict[id] = []arrow.ArrayData{combined.Data()}
+ for _, data := range v {
+ data.Release()
+ }
return combined.Data(), nil
}
diff --git a/arrow/ipc/file_reader.go b/arrow/ipc/file_reader.go
index 53a813d0..8fd329d6 100644
--- a/arrow/ipc/file_reader.go
+++ b/arrow/ipc/file_reader.go
@@ -771,6 +771,7 @@ func (ctx *arrayLoaderContext) loadNull() arrow.ArrayData {
func (ctx *arrayLoaderContext) loadPrimitive(dt arrow.DataType)
arrow.ArrayData {
field, buffers := ctx.loadCommon(dt.ID(), 2)
+ defer func() { memory.ReleaseBuffers(buffers) }()
switch field.Length() {
case 0:
@@ -780,15 +781,14 @@ func (ctx *arrayLoaderContext) loadPrimitive(dt
arrow.DataType) arrow.ArrayData
buffers = append(buffers, ctx.buffer())
}
- defer memory.ReleaseBuffers(buffers)
-
return array.NewData(dt, int(field.Length()), buffers, nil,
int(field.NullCount()), 0)
}
func (ctx *arrayLoaderContext) loadBinary(dt arrow.DataType) arrow.ArrayData {
field, buffers := ctx.loadCommon(dt.ID(), 3)
- buffers = append(buffers, ctx.buffer(), ctx.buffer())
- defer memory.ReleaseBuffers(buffers)
+ defer func() { memory.ReleaseBuffers(buffers) }()
+ buffers = append(buffers, ctx.buffer())
+ buffers = append(buffers, ctx.buffer())
return array.NewData(dt, int(field.Length()), buffers, nil,
int(field.NullCount()), 0)
}
@@ -796,27 +796,27 @@ func (ctx *arrayLoaderContext) loadBinary(dt
arrow.DataType) arrow.ArrayData {
func (ctx *arrayLoaderContext) loadBinaryView(dt arrow.DataType)
arrow.ArrayData {
nVariadicBufs := ctx.variadic()
field, buffers := ctx.loadCommon(dt.ID(), 2+int(nVariadicBufs))
+ defer func() { memory.ReleaseBuffers(buffers) }()
buffers = append(buffers, ctx.buffer())
for i := 0; i < int(nVariadicBufs); i++ {
buffers = append(buffers, ctx.buffer())
}
- defer memory.ReleaseBuffers(buffers)
return array.NewData(dt, int(field.Length()), buffers, nil,
int(field.NullCount()), 0)
}
func (ctx *arrayLoaderContext) loadFixedSizeBinary(dt
*arrow.FixedSizeBinaryType) arrow.ArrayData {
field, buffers := ctx.loadCommon(dt.ID(), 2)
+ defer func() { memory.ReleaseBuffers(buffers) }()
buffers = append(buffers, ctx.buffer())
- defer memory.ReleaseBuffers(buffers)
return array.NewData(dt, int(field.Length()), buffers, nil,
int(field.NullCount()), 0)
}
func (ctx *arrayLoaderContext) loadMap(dt *arrow.MapType) arrow.ArrayData {
field, buffers := ctx.loadCommon(dt.ID(), 2)
+ defer func() { memory.ReleaseBuffers(buffers) }()
buffers = append(buffers, ctx.buffer())
- defer memory.ReleaseBuffers(buffers)
sub := ctx.loadChild(dt.Elem())
defer sub.Release()
@@ -826,8 +826,8 @@ func (ctx *arrayLoaderContext) loadMap(dt *arrow.MapType)
arrow.ArrayData {
func (ctx *arrayLoaderContext) loadList(dt arrow.ListLikeType) arrow.ArrayData
{
field, buffers := ctx.loadCommon(dt.ID(), 2)
+ defer func() { memory.ReleaseBuffers(buffers) }()
buffers = append(buffers, ctx.buffer())
- defer memory.ReleaseBuffers(buffers)
sub := ctx.loadChild(dt.Elem())
defer sub.Release()
@@ -837,8 +837,9 @@ func (ctx *arrayLoaderContext) loadList(dt
arrow.ListLikeType) arrow.ArrayData {
func (ctx *arrayLoaderContext) loadListView(dt arrow.VarLenListLikeType)
arrow.ArrayData {
field, buffers := ctx.loadCommon(dt.ID(), 3)
- buffers = append(buffers, ctx.buffer(), ctx.buffer())
- defer memory.ReleaseBuffers(buffers)
+ defer func() { memory.ReleaseBuffers(buffers) }()
+ buffers = append(buffers, ctx.buffer())
+ buffers = append(buffers, ctx.buffer())
sub := ctx.loadChild(dt.Elem())
defer sub.Release()
@@ -861,14 +862,16 @@ func (ctx *arrayLoaderContext) loadStruct(dt
*arrow.StructType) arrow.ArrayData
defer memory.ReleaseBuffers(buffers)
subs := make([]arrow.ArrayData, dt.NumFields())
- for i, f := range dt.Fields() {
- subs[i] = ctx.loadChild(f.Type)
- }
defer func() {
- for i := range subs {
- subs[i].Release()
+ for _, sub := range subs {
+ if sub != nil {
+ sub.Release()
+ }
}
}()
+ for i, f := range dt.Fields() {
+ subs[i] = ctx.loadChild(f.Type)
+ }
return array.NewData(dt, int(field.Length()), buffers, subs,
int(field.NullCount()), 0)
}
@@ -882,6 +885,7 @@ func (ctx *arrayLoaderContext) loadUnion(dt
arrow.UnionType) arrow.ArrayData {
}
field, buffers := ctx.loadCommon(dt.ID(), nBuffers)
+ defer func() { memory.ReleaseBuffers(buffers) }()
if field.NullCount() != 0 && buffers[0] != nil {
panic("arrow/ipc: cannot read pre-1.0.0 union array with
top-level validity bitmap")
}
@@ -901,16 +905,17 @@ func (ctx *arrayLoaderContext) loadUnion(dt
arrow.UnionType) arrow.ArrayData {
}
}
- defer memory.ReleaseBuffers(buffers)
subs := make([]arrow.ArrayData, dt.NumFields())
- for i, f := range dt.Fields() {
- subs[i] = ctx.loadChild(f.Type)
- }
defer func() {
- for i := range subs {
- subs[i].Release()
+ for _, sub := range subs {
+ if sub != nil {
+ sub.Release()
+ }
}
}()
+ for i, f := range dt.Fields() {
+ subs[i] = ctx.loadChild(f.Type)
+ }
return array.NewData(dt, int(field.Length()), buffers, subs, 0, 0)
}
diff --git a/arrow/ipc/fuzz_regression_test.go
b/arrow/ipc/fuzz_regression_test.go
new file mode 100644
index 00000000..828e6186
--- /dev/null
+++ b/arrow/ipc/fuzz_regression_test.go
@@ -0,0 +1,79 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package ipc_test
+
+import (
+ "bytes"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/apache/arrow-go/v18/arrow/ipc"
+ "github.com/apache/arrow-go/v18/arrow/memory"
+)
+
+func TestStreamFuzzRegression(t *testing.T) {
+ dataDir := os.Getenv("ARROW_TEST_DATA")
+ if dataDir == "" {
+ t.Skip("ARROW_TEST_DATA is not set")
+ }
+ dir := filepath.Join(dataDir, "arrow-ipc-stream")
+ entries, err := os.ReadDir(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // Go reads these to the end without error or batches; Arrow C++
rejects both
+ // (invalid flatbuffer, decimal precision 0), so tightening validation
may flip them.
+ clean := map[string]bool{
+
"clusterfuzz-testcase-minimized-arrow-ipc-stream-fuzz-4757582821064704": true,
+ "crash-5e88bae6ac5250714e8c8bc73b9d67b949fadbb4":
true,
+ }
+ count := 0
+ for _, entry := range entries {
+ if !entry.Type().IsRegular() || entry.Name() == "README.md" {
+ continue
+ }
+ count++
+ t.Run(entry.Name(), func(t *testing.T) {
+ defer func() {
+ if p := recover(); p != nil {
+ t.Errorf("%s: unexpected panic: %v",
entry.Name(), p)
+ }
+ }()
+ data, err := os.ReadFile(filepath.Join(dir,
entry.Name()))
+ if err != nil {
+ t.Fatal(err)
+ }
+ mem :=
memory.NewCheckedAllocator(memory.NewGoAllocator())
+ defer mem.AssertSize(t, 0)
+ r, err := ipc.NewReader(bytes.NewReader(data),
ipc.WithAllocator(mem))
+ if err == nil {
+ defer r.Release()
+ for r.Next() {
+ }
+ err = r.Err()
+ }
+ if !clean[entry.Name()] && err == nil {
+ t.Error("expected an error, got success")
+ }
+ })
+ }
+ if count == 0 {
+ t.Fatal("no stream regression inputs found")
+ }
+}
diff --git a/arrow/ipc/loader_leak_test.go b/arrow/ipc/loader_leak_test.go
new file mode 100644
index 00000000..2ddb2d8e
--- /dev/null
+++ b/arrow/ipc/loader_leak_test.go
@@ -0,0 +1,83 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package ipc
+
+import (
+ "encoding/binary"
+ "testing"
+
+ "github.com/apache/arrow-go/v18/arrow"
+ "github.com/apache/arrow-go/v18/arrow/internal/flatbuf"
+ "github.com/apache/arrow-go/v18/arrow/memory"
+ flatbuffers "github.com/google/flatbuffers/go"
+ "github.com/stretchr/testify/require"
+)
+
+func TestLoaderReleasesOnError(t *testing.T) {
+ tests := []struct {
+ name string
+ dt arrow.DataType
+ nodes []fieldMetadata
+ buffers []bufferMetadata
+ max int
+ // compressed makes the second buffer read fail after the
validity buffer is loaded
+ compressed bool
+ }{
+ {"union validity", arrow.SparseUnionOf([]arrow.Field{{Name:
"a", Type: arrow.PrimitiveTypes.Int32}}, []arrow.UnionTypeCode{0}),
+ []fieldMetadata{{Len: 1, Nulls: 1}},
[]bufferMetadata{{Len: 1}}, 10, false},
+ {"primitive buffer", arrow.PrimitiveTypes.Int32,
+ []fieldMetadata{{Len: 1, Nulls: 1}},
[]bufferMetadata{{Len: 9}, {Offset: 64, Len: 8}}, 10, true},
+ {"binary buffer", arrow.BinaryTypes.String,
+ []fieldMetadata{{Len: 1, Nulls: 1}},
[]bufferMetadata{{Len: 9}, {Len: 9}, {Offset: 64, Len: 8}}, 10, true},
+ {"fixed size binary buffer",
&arrow.FixedSizeBinaryType{ByteWidth: 4},
+ []fieldMetadata{{Len: 1, Nulls: 1}},
[]bufferMetadata{{Len: 9}, {Offset: 64, Len: 8}}, 10, true},
+ {"map buffer", arrow.MapOf(arrow.PrimitiveTypes.Int32,
arrow.PrimitiveTypes.Int32),
+ []fieldMetadata{{Len: 1, Nulls: 1}},
[]bufferMetadata{{Len: 9}, {Offset: 64, Len: 8}}, 10, true},
+ {"list buffer", arrow.ListOf(arrow.PrimitiveTypes.Int32),
+ []fieldMetadata{{Len: 1, Nulls: 1}},
[]bufferMetadata{{Len: 9}, {Offset: 64, Len: 8}}, 10, true},
+ {"list view buffer",
arrow.ListViewOf(arrow.PrimitiveTypes.Int32),
+ []fieldMetadata{{Len: 1, Nulls: 1}},
[]bufferMetadata{{Len: 9}, {Len: 9}, {Offset: 64, Len: 8}}, 10, true},
+ {"struct child", arrow.StructOf(
+ arrow.Field{Name: "a", Type:
arrow.PrimitiveTypes.Int32},
+ arrow.Field{Name: "b", Type:
arrow.StructOf(arrow.Field{Name: "c", Type: arrow.Null})},
+ ),
+ []fieldMetadata{{Len: 1}, {Len: 1}, {Len: 1}},
[]bufferMetadata{{Len: 4}}, 1, false},
+ {"union child", arrow.SparseUnionOf([]arrow.Field{
+ {Name: "a", Type: arrow.PrimitiveTypes.Int32},
+ {Name: "b", Type: arrow.StructOf(arrow.Field{Name: "c",
Type: arrow.Null})},
+ }, []arrow.UnionTypeCode{0, 1}),
+ []fieldMetadata{{Len: 1}, {Len: 1}, {Len: 1}, {Len:
1}}, []bufferMetadata{{}, {Len: 1}, {}, {Len: 4}}, 1, false},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ mem :=
memory.NewCheckedAllocator(memory.NewGoAllocator())
+ defer mem.AssertSize(t, 0)
+ body := memory.NewResizableBuffer(mem)
+ body.Resize(64)
+ defer body.Release()
+ b := flatbuffers.NewBuilder(0)
+ record := recordToFB(b, 1, 64, tt.nodes, tt.buffers,
-1, nil)
+ b.Finish(record)
+ ctx := arrayLoaderContext{src: ipcSource{meta:
flatbuf.GetRootAsRecordBatch(b.FinishedBytes(), 0), rawBytes: body, mem: mem},
version: MetadataV4, max: tt.max}
+ if tt.compressed {
+ binary.LittleEndian.PutUint64(body.Bytes(),
^uint64(0))
+ ctx.src.codec = &lz4Decompressor{}
+ }
+ require.Panics(t, func() { ctx.loadArray(tt.dt) })
+ })
+ }
+}