Skip to content

tableFromIPC reads a stream cut inside a message prefix as a complete stream, with no error #535

Open

Description

@mark1russell7

Describe the bug, including details regarding any error messages, version, and platform.

When an IPC stream ends inside the 8-byte prefix of a message (the continuation marker and the metadata length), tableFromIPC and RecordBatchStreamReader treat the cut as the end of the stream. A stream of 3 rows, cut 2, 4, 6 or 8 bytes into the prefix of its RecordBatch, reads as the schema with 0 rows and no error. A cut inside the metadata of the same message throws Expected to read 136 metadata bytes, but only read 10., so only a cut in the prefix is silent.

Version and platform: apache-arrow 21.2.0, Node.js 24.22.2 on Linux (GitHub Actions), and Node.js 24.21.0 on Windows.

Repro. Save as repro.ts and run node repro.ts (Node.js 24 strips the types). It exits 1 while the fault is present.

// F0145: apache-arrow 21.2.0. A stream that ends inside the 8-byte prefix of a message reads as a complete stream
// with no error. MessageReader.readMetadataLength() changes a partial prefix to the length 0, which is the end of the
// stream. A stream that ends inside the metadata or the body of a message throws an error.
import * as A from "apache-arrow";

const stream = A.tableToIPC(A.tableFromArrays({ x: Int32Array.of(1, 2, 3) }), "stream");

// The prefix of the first message is 0xFFFFFFFF and the length of its metadata. The Schema message ends after its
// metadata, because it has no body. The RecordBatch message starts there.
const view = new DataView(stream.buffer, stream.byteOffset, stream.byteLength);
const recordBatchStart = 8 + view.getInt32(4, true);

const read = (bytes: Uint8Array): string => {
  try {
    const table = A.tableFromIPC(bytes);
    return `${table.numRows} rows, x = ${JSON.stringify(Array.from(table.getChild("x") ?? []))}`;
  } catch (error) {
    return `throws: ${(error as Error).message}`;
  }
};

console.log(`full stream (${stream.length} bytes): ${read(stream)}`);
console.log(`cut inside the RecordBatch metadata: ${read(stream.subarray(0, recordBatchStart + 18))}`);

const cuts = [2, 4, 6, 8];
let silent = 0;
for (const cut of cuts) {
  const result = read(stream.subarray(0, recordBatchStart + cut));
  console.log(`cut after ${cut} of the 8 prefix bytes of the RecordBatch: ${result}`);
  if (!result.startsWith("throws")) silent++;
}

if (silent > 0) {
  console.log(`BUG PRESENT: ${silent} of ${cuts.length} streams cut inside a message prefix read as a complete stream with no error.`);
  process.exit(1);
}
console.log("OK: each stream cut inside a message prefix throws an error.");

Output (CI run):

node: v24.22.2, os: Linux 7.0.0-1019-azure

full stream (304 bytes): 3 rows, x = [1,2,3]
cut inside the RecordBatch metadata: throws: Expected to read 136 metadata bytes, but only read 10.
cut after 2 of the 8 prefix bytes of the RecordBatch: 0 rows, x = []
cut after 4 of the 8 prefix bytes of the RecordBatch: 0 rows, x = []
cut after 6 of the 8 prefix bytes of the RecordBatch: 0 rows, x = []
cut after 8 of the 8 prefix bytes of the RecordBatch: 0 rows, x = []
BUG PRESENT: 4 of 4 streams cut inside a message prefix read as a complete stream with no error.
run.sh exit code: 1

Expected: An error for a stream that ends inside a message prefix, the same as for a stream that ends inside the metadata. The end-of-stream marker is 8 bytes (0xFFFFFFFF 0x00000000), or 4 zero bytes in the old format. A partial prefix is not an end-of-stream marker.

Cause: MessageReader.readMetadataLength() in src/ipc/message.ts reads up to 4 bytes and returns done when readInt32(0) || 0 is 0, so a missing or short read looks like the end-of-stream length 0. When the prefix is complete but no metadata follows (the cut after 8 bytes), readMetadata() returns ITERATOR_DONE for an empty read (if (!buf)), before its length check.

An AI coding assistant (Claude Code) found this fault, wrote the repro and wrote this text.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions