GoncaloPereiraFigueiredoFerreira opened a new issue, #1517:
URL: https://github.com/apache/arrow-go/issues/1517

   ### Describe the bug, including details regarding any error messages, 
version, and platform.
   
   ### Describe the bug, including details regarding any error messages, 
version, and platform.
   
   `flight.NewRecordReader` fails when two or more metadata-only `FlightData` 
messages arrive before a valid IPC schema and record batch. These messages 
contain `app_metadata` with empty `data_header` and `data_body`. Arrow Go’s own 
[Writer.WriteMetadata](https://github.com/apache/arrow-go/blob/78af17d3b7c4d337694b12abe3a4c13cf10b462d/arrow/flight/record_batch_writer.go)
 emits this message shape.
   
   **Heartbeat use case:** a Flight SQL query can take a long time to produce 
its first result batch. We want application-level activity on the same DoGet 
response stream during that wait, for example to avoid intermediary 
request-idle timeouts. A zero-row IPC batch requires a schema first; 
metadata-only heartbeats allow activity without committing the DoGet stream to 
a schema early. The Go reader currently rejects the resulting stream, affecting 
downstream Flight SQL clients built on it.
   
   **Java interoperability reference:** Arrow Java 19.0.0 explicitly supports 
messages without an IPC payload. 
[ArrowMessage.getMessageType](https://github.com/apache/arrow-java/blob/v19.0.0/flight/flight-core/src/main/java/org/apache/arrow/flight/ArrowMessage.java#L242)
 maps an absent IPC message to its metadata/no-data type, and [FlightStream’s 
observer](https://github.com/apache/arrow-java/blob/v19.0.0/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightStream.java#L418)
 handles metadata/descriptor-only messages separately from schema and record 
batches. Our Java Flight 19.0.0 client tests successfully consumed repeated 
metadata-only startup messages followed by the schema and result batches. 
   
   **Versions and platform:** reproduced with Arrow Go **v18.8.0** and upstream 
**main at `78af17d3b7c4d337694b12abe3a4c13cf10b462d`**, using **Go 1.27.3 on 
macOS arm64 (`darwin/arm64`)**. The example below uses only public Arrow Go 
APIs and an in-memory stream; it needs no server, ADBC driver, or shared test 
data.
   
   **Expected behavior:** the record reader consumes the subsequent schema and 
the original one-row batch containing `42`, without treating the metadata-only 
messages as IPC or returning synthetic record batches.
   
   **Actual behavior:** reader construction returns this error before the valid 
schema is consumed:
   
   ```text
   arrow/flight: could not create flight reader: arrow/ipc: unknown error while 
reading: runtime error: index out of range [3] with length 0
   ```
   
   The IPC decoder recovers the panic and returns an error. This example does 
not crash the process with an unrecovered panic.
   
   #### Minimal reproduction
   
   Save the following as `main.go` in a new directory:
   
   ```go
   package main
   
   import (
        "flag"
        "fmt"
        "io"
        "os"
   
        "github.com/apache/arrow-go/v18/arrow"
        "github.com/apache/arrow-go/v18/arrow/array"
        "github.com/apache/arrow-go/v18/arrow/flight"
        "github.com/apache/arrow-go/v18/arrow/ipc"
        "github.com/apache/arrow-go/v18/arrow/memory"
   )
   
   type stream struct {
        messages []*flight.FlightData
   }
   
   func (s *stream) Send(data *flight.FlightData) error {
        // The writer reuses buffers, so retain an independent copy of each 
message.
        s.messages = append(s.messages, &flight.FlightData{
                DataHeader:  append([]byte(nil), data.DataHeader...),
                AppMetadata: append([]byte(nil), data.AppMetadata...),
                DataBody:    append([]byte(nil), data.DataBody...),
        })
        return nil
   }
   
   func (s *stream) Recv() (*flight.FlightData, error) {
        if len(s.messages) == 0 {
                return nil, io.EOF
        }
        data := s.messages[0]
        s.messages = s.messages[1:]
        return data, nil
   }
   
   func run(metadataMessages int) error {
        schema := arrow.NewSchema([]arrow.Field{{Name: "number", Type: 
arrow.PrimitiveTypes.Int32}}, nil)
        builder := array.NewInt32Builder(memory.DefaultAllocator)
        builder.Append(42)
        values := builder.NewInt32Array()
        builder.Release()
        defer values.Release()
        record := array.NewRecordBatch(schema, []arrow.Array{values}, 1)
        defer record.Release()
   
        input := &stream{}
        writer := flight.NewRecordWriter(input, ipc.WithSchema(schema))
        for i := 0; i < metadataMessages; i++ {
                if err := writer.WriteMetadata([]byte("heartbeat")); err != nil 
{
                        return err
                }
        }
        if err := writer.Write(record); err != nil {
                return err
        }
        if err := writer.Close(); err != nil {
                return err
        }
   
        reader, err := flight.NewRecordReader(input)
        if err != nil {
                return err
        }
        defer reader.Release()
        if !reader.Next() {
                return fmt.Errorf("missing record: %v", reader.Err())
        }
        if !array.RecordEqual(record, reader.RecordBatch()) {
                return fmt.Errorf("record differs")
        }
        if reader.Next() {
                return fmt.Errorf("unexpected extra record")
        }
        if err := reader.Err(); err != nil {
                return err
        }
        fmt.Printf("read one batch containing 42 (%d metadata-only 
messages)\n", metadataMessages)
        return nil
   }
   
   func main() {
        count := flag.Int("metadata", 2, "metadata-only messages before schema")
        flag.Parse()
        if err := run(*count); err != nil {
                fmt.Fprintln(os.Stderr, err)
                os.Exit(1)
        }
   }
   ```
   
   Run from that directory:
   
   ```sh
   go mod init example.com/flight-metadata-reproducer
   go get github.com/apache/arrow-go/[email protected]
   go mod tidy
   go run .
   ```
   
   The default sends two metadata-only messages, then a schema and one batch, 
then EOF. All messages are produced with the ordinary Flight writer and copied 
before its buffers are reused.
   
   Controls and longer bursts can be selected with `-metadata`:
   
   ```sh
   go run . -metadata=0
   go run . -metadata=1
   go run . -metadata=5
   go run . -metadata=1024
   ```
   
   Observed results with the identical example. The columns distinguish 
released code from the tested upstream development snapshot:
   
   - **Released v18.8.0:** the published Arrow Go release, without this fix.
   - **Upstream main (before patch):** the upstream snapshot at 
`78af17d3b7c4d337694b12abe3a4c13cf10b462d`, without this fix. This checks 
whether upstream has already resolved the released-reader defect.
   - **Same main + patch:** that exact upstream snapshot with the proposed 
reader patch. Its resolved Go dependencies match the unpatched main build, 
isolating the patch in the before/after comparison.
   
   | Metadata-only messages before schema | Released v18.8.0 | Upstream main 
(before patch) | Same main + patch |
   | --- | --- | --- | --- |
   | 0 | Reads the original batch | Reads the original batch | Reads the 
original batch |
   | 1 | Reads the original batch | Reads the original batch | Reads the 
original batch |
   | 2 | Empty-header decoder error | Empty-header decoder error | Reads the 
original batch |
   | 5 | Empty-header decoder error | Empty-header decoder error | Reads the 
original batch |
   | 1,024 | Empty-header decoder error | Empty-header decoder error | Reads 
the original batch |
   
   #### Cause and proposed fix
   
   The [Flight reader constructor and message 
adapter](https://github.com/apache/arrow-go/blob/78af17d3b7c4d337694b12abe3a4c13cf10b462d/arrow/flight/record_batch_reader.go)
 discard the first headerless message while peeking for a descriptor, but pass 
a subsequent headerless message to `ipc.NewMessage`. The empty header then 
reaches the FlatBuffers decoder.
   
   The proposed fix filters messages with neither an IPC header nor a body at 
the Flight-envelope boundary, while preserving descriptor updates and 
application metadata attached to actual IPC payloads. A headerless message with 
a nonempty body remains an error, and existing IPC schema/message validation 
remains in force. Standalone metadata is ignored by this record-oriented reader.
   
   #### AI assistance
   
   OpenAI Codex assisted with drafting this report and producing the standalone 
reproducer. The reported results come from local executions against released, 
unpatched upstream, and patched Arrow Go readers.
   
   ### Component(s)
   
   Other
   
   
   
   ### Component(s)
   
   Other


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to