From f44e28fa03a64ae5b3d9352d21aee2cc84f9af6c Mon Sep 17 00:00:00 2001 From: Thor <8681572+thorfour@users.noreply.github.com> Date: Tue, 1 Aug 2023 12:00:13 -0500 Subject: [PATCH] GH-36981: [Go] Fix ipc reader leak (#36982) ### Rationale for this change Previously the ipc reader was leaking allocations. ### What changes are included in this PR? Call `Clear()` on the memo table on final release of the ipc reader. ### Are these changes tested? Yes ### Are there any user-facing changes? * Closes: #36981 Authored-by: thorfour Signed-off-by: Matt Topol --- go/arrow/ipc/reader.go | 1 + go/arrow/ipc/reader_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/go/arrow/ipc/reader.go b/go/arrow/ipc/reader.go index 99aab597ce950..bee48cf965682 100644 --- a/go/arrow/ipc/reader.go +++ b/go/arrow/ipc/reader.go @@ -159,6 +159,7 @@ func (r *Reader) Release() { r.r.Release() r.r = nil } + r.memo.Clear() } } diff --git a/go/arrow/ipc/reader_test.go b/go/arrow/ipc/reader_test.go index a8930984fbf37..7bcf737af0d6d 100644 --- a/go/arrow/ipc/reader_test.go +++ b/go/arrow/ipc/reader_test.go @@ -56,3 +56,40 @@ func TestReaderCatchPanic(t *testing.T) { assert.Contains(t, err.Error(), "arrow/ipc: unknown error while reading") } } + +func TestReaderCheckedAllocator(t *testing.T) { + alloc := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer alloc.AssertSize(t, 0) + schema := arrow.NewSchema([]arrow.Field{ + { + Name: "s", + Type: &arrow.DictionaryType{ + ValueType: arrow.BinaryTypes.String, + IndexType: arrow.PrimitiveTypes.Int32, + }, + }, + }, nil) + + b := array.NewRecordBuilder(alloc, schema) + defer b.Release() + + bldr := b.Field(0).(*array.BinaryDictionaryBuilder) + bldr.Append([]byte("foo")) + bldr.Append([]byte("bar")) + bldr.Append([]byte("baz")) + + rec := b.NewRecord() + defer rec.Release() + + buf := new(bytes.Buffer) + writer := NewWriter(buf, WithSchema(schema), WithAllocator(alloc)) + defer writer.Close() + require.NoError(t, writer.Write(rec)) + + reader, err := NewReader(buf, WithAllocator(alloc)) + require.NoError(t, err) + defer reader.Release() + + _, err = reader.Read() + require.NoError(t, err) +}