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 c9c65e34 fix(arrow/flight): skip metadata-only messages in the record 
reader (#1549)
c9c65e34 is described below

commit c9c65e34b9b435e259d75926bef5e68a48b1fd41
Author: Shivam Sharma <[email protected]>
AuthorDate: Tue Nov 24 00:19:28 2026 +0530

    fix(arrow/flight): skip metadata-only messages in the record reader (#1549)
    
    ### Rationale for this change
    
    `Writer.WriteMetadata` sends a `FlightData` message that has
    `app_metadata` but no IPC header or body. `NewRecordReader` dropped the
    first such message while peeking for a descriptor, but the message
    adapter passed any later one to `ipc.NewMessage`. The empty header then
    reached the flatbuffers decoder, and reading failed with:
    
    ```
    arrow/flight: could not create flight reader: arrow/ipc: unknown error 
while reading: runtime error: index out of range [3] with length 0
    ```
    
    So two heartbeats before the schema broke the stream (see #1517). A
    single metadata-only message between two record batches failed the same
    way, because `Next()` hit the same decode error.
    
    ### What changes are included in this PR?
    
    `dataMessageReader.Message()` now skips messages that have neither a
    `DataHeader` nor a `DataBody`. It still records any `FlightDescriptor`
    they carry. Messages with a header, or with a body and no header, follow
    the same path as before. `LatestAppMetadata()` still returns the
    metadata attached to the record batch that was read, not the metadata of
    a skipped heartbeat.
    
    ### Are these changes tested?
    
    Yes. `TestReaderSkipsMetadataOnlyMessages` writes three `WriteMetadata`
    messages before the schema and one after each record batch (including
    after the last one). It then checks that every batch reads back equal,
    with its own `WriteWithAppMetadata` value, and that the stream ends
    cleanly. The test fails on `main` with the error above and passes with
    this change. `collectingFlightStreamWriter` now copies `AppMetadata`
    too, so the test can check it.
    
    `go test ./arrow/flight/...` passes, with and without `-tags assert,test
    -short`. `go vet ./arrow/flight/...` and gofmt are clean.
    
    ### Are there any user-facing changes?
    
    Readers no longer fail on streams that contain standalone metadata
    messages. The record-oriented reader skips those messages.
    
    Fixes #1517
    
    AI assistance: I used Claude Code (Anthropic) to write this change and
    the test. I reviewed the code and ran the tests locally.
    
    🤖 Generated with [Claude Code](https://claude.com/claude-code)
    
    ---------
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 arrow/flight/flight_test.go         | 50 +++++++++++++++++++++++++++++++++
 arrow/flight/record_batch_reader.go | 56 ++++++++++++++++++++++++++-----------
 2 files changed, 89 insertions(+), 17 deletions(-)

diff --git a/arrow/flight/flight_test.go b/arrow/flight/flight_test.go
index a95db2bc..b4d1971c 100644
--- a/arrow/flight/flight_test.go
+++ b/arrow/flight/flight_test.go
@@ -515,6 +515,55 @@ func TestReaderRetainsDescriptorForFirstRecordBatch(t 
*testing.T) {
        require.Equal(t, descriptor, r.Chunk().Desc)
 }
 
+func TestReaderSkipsMetadataOnlyMessages(t *testing.T) {
+       recs, ok := arrdata.Records["primitives"]
+       require.True(t, ok)
+
+       fs := collectingFlightStreamWriter{}
+       w := flight.NewRecordWriter(&fs, ipc.WithSchema(recs[0].Schema()))
+       // metadata-only messages before the schema, e.g. heartbeats sent while
+       // the first batch is still being produced
+       for range 3 {
+               require.NoError(t, w.WriteMetadata([]byte("heartbeat")))
+       }
+       for i, rec := range recs {
+               require.NoError(t, w.WriteWithAppMetadata(rec, fmt.Appendf(nil, 
"batch %d", i)))
+               require.NoError(t, w.WriteMetadata([]byte("heartbeat")))
+       }
+       require.NoError(t, w.Close())
+
+       r, err := flight.NewRecordReader(&fs)
+       require.NoError(t, err)
+       defer r.Release()
+
+       for i, rec := range recs {
+               require.True(t, r.Next(), "batch %d: %v", i, r.Err())
+               require.Truef(t, array.RecordEqual(rec, r.RecordBatch()), 
"batch %d differs", i)
+               require.Equal(t, fmt.Appendf(nil, "batch %d", i), 
r.LatestAppMetadata())
+       }
+       require.False(t, r.Next())
+       require.NoError(t, r.Err())
+}
+
+func TestReaderRejectsBodyWithoutHeader(t *testing.T) {
+       recs, ok := arrdata.Records["primitives"]
+       require.True(t, ok)
+
+       fs := collectingFlightStreamWriter{}
+       w := flight.NewRecordWriter(&fs, ipc.WithSchema(recs[0].Schema()))
+       require.NoError(t, w.Write(recs[0]))
+       require.NoError(t, fs.Send(&flight.FlightData{DataBody: 
[]byte("body")}))
+       require.NoError(t, w.Close())
+
+       r, err := flight.NewRecordReader(&fs)
+       require.NoError(t, err)
+       defer r.Release()
+
+       require.True(t, r.Next(), "%v", r.Err())
+       require.False(t, r.Next())
+       require.ErrorContains(t, r.Err(), "FlightData has a body but no IPC 
header")
+}
+
 type collectingFlightStreamWriter struct {
        hasDescriptor []bool
        payloads      []*flight.FlightData
@@ -527,6 +576,7 @@ func (f *collectingFlightStreamWriter) Send(data 
*flight.FlightData) error {
                FlightDescriptor: data.FlightDescriptor,
                DataHeader:       append([]byte(nil), data.DataHeader...),
                DataBody:         append([]byte(nil), data.DataBody...),
+               AppMetadata:      append([]byte(nil), data.AppMetadata...),
        })
        return nil
 }
diff --git a/arrow/flight/record_batch_reader.go 
b/arrow/flight/record_batch_reader.go
index dac7a352..4bf6e52c 100644
--- a/arrow/flight/record_batch_reader.go
+++ b/arrow/flight/record_batch_reader.go
@@ -56,28 +56,47 @@ func (d *dataMessageReader) Message() (*ipc.Message, error) 
{
                err error
        )
 
-       if d.peeked != nil {
-               fd = d.peeked
-               d.peeked = nil
-       } else {
-               fd, err = d.rdr.Recv()
-       }
+       for {
+               if d.peeked != nil {
+                       fd = d.peeked
+                       d.peeked = nil
+               } else {
+                       fd, err = d.rdr.Recv()
+               }
 
-       if err != nil {
-               if d.msg != nil {
-                       // clear the previous message in the error case
-                       d.msg.Release()
-                       d.msg = nil
+               if err != nil {
+                       if d.msg != nil {
+                               // clear the previous message in the error case
+                               d.msg.Release()
+                               d.msg = nil
+                       }
+                       d.lastAppMetadata = nil
+                       d.descr = nil
+                       return nil, err
+               }
+
+               if fd.FlightDescriptor != nil {
+                       d.descr = fd.FlightDescriptor
+               }
+               // a message with neither an IPC header nor a body only carries
+               // app metadata (see Writer.WriteMetadata); there is no IPC 
message
+               // to decode, so skip it
+               if len(fd.DataHeader) > 0 {
+                       break
+               }
+               if len(fd.DataBody) > 0 {
+                       if d.msg != nil {
+                               // clear the previous message in the error case
+                               d.msg.Release()
+                               d.msg = nil
+                       }
+                       d.lastAppMetadata = nil
+                       d.descr = nil
+                       return nil, errors.New("arrow/flight: FlightData has a 
body but no IPC header")
                }
-               d.lastAppMetadata = nil
-               d.descr = nil
-               return nil, err
        }
 
        d.lastAppMetadata = fd.AppMetadata
-       if fd.FlightDescriptor != nil {
-               d.descr = fd.FlightDescriptor
-       }
        d.msg = ipc.NewMessage(memory.NewBufferBytes(fd.DataHeader), 
memory.NewBufferBytes(fd.DataBody))
        return d.msg, nil
 }
@@ -101,6 +120,9 @@ func (d *dataMessageReader) Release() {
 // Reader is an ipc.Reader which also keeps track of the metadata from
 // the FlightData messages as they come in, calling LatestAppMetadata
 // will return the metadata bytes from the most recently read message.
+//
+// Messages that carry only app metadata, without an IPC header or body (see
+// Writer.WriteMetadata), are skipped and their metadata is not exposed.
 type Reader struct {
        *ipc.Reader
        dmr *dataMessageReader

Reply via email to