The stream reader drops a message of an unexpected type but not its body, and parses the body as the next messages

オープン 初心者向け
#546 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@Umer-Butt07 がすでに取り組んでいます。

2026年11月30日 から。

  • #553 @Umer-Butt07 による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
80/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript, typescript
領域
data

調査の方向性

両方のループは src/ipc/reader.ts にあり、RecordBatchStreamReaderImpl.next() は 504〜530 行目付近、AsyncRecordBatchStreamReaderImpl.next() は 579〜605 行目付近で、そこでは RecordBatch と DictionaryBatch の分岐だけが reader.readMessageBody(message.bodyLength) を呼んでいる。パッケージをビルドし、issue の repro.ts をそれに対して実行し、現状では 666 行で 1 で終了することを確認する。完了とは、repro が 0 で終了すること — 未知の header 型で両方のループが throw するか、両方が読み進める前に body を消費するかのいずれか — であり、既存の IPC reader テストが引き続き通ることである。

索引モデルが issue の本文から書いたものです。

説明

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

The stream reader drops a message whose header type is not Schema, RecordBatch or DictionaryBatch (for example Tensor, SparseTensor or NONE), but it does not skip the body of that message. So the next read starts inside the body, and the reader parses the body as the next messages. A reader that frames each message by its bodyLength, as the encapsulated message format says, sees different messages in the same bytes.

The repro puts a Tensor message between the Schema and the RecordBatch of a valid stream of [1, 2, 3]. The body of the Tensor message is the complete RecordBatch message of a second stream of [666, 777, 888]. tableFromIPC returns 6 rows, [666, 777, 888, 1, 2, 3], with no error.

Version and platform: apache-arrow 21.2.0, Node.js 24.21.0 on Windows 11. The code below is the same on main (5aa0205).

Repro. Save as repro.ts, install [email protected] and [email protected] (the version that apache-arrow installs), and run node repro.ts (Node.js 24 strips the types). It exits 1 while the fault is present.

// apache-arrow 21.2.0. The stream reader drops a message of an unexpected header type (for example Tensor), but it
// does not skip the body of that message. RecordBatchStreamReaderImpl.next() handles only Schema, RecordBatch and
// DictionaryBatch. So the reader parses the body as the next messages. A reader that frames each message by its
// bodyLength sees different rows in the same bytes.
import * as A from "apache-arrow";
import { Message } from "apache-arrow/fb/message";
import * as flatbuffers from "flatbuffers";

// This function gives the bounds of each message of a stream, and the root table of its metadata.
function messages(bytes: Uint8Array) {
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
  const found = [];
  let start = 0;
  while (start + 8 <= bytes.length) {
    const metadataLength = view.getInt32(start + 4, true);
    if (metadataLength === 0) {
      found.push({ start, metadataEnd: start + 8, end: start + 8, header: "EOS", bodyLength: 0, fb: null });
      break;
    }
    const metadataEnd = start + 8 + metadataLength;
    const fb = Message.getRootAsMessage(new flatbuffers.ByteBuffer(bytes.slice(start + 8, metadataEnd)));
    const bodyLength = Number(fb.bodyLength());
    found.push({ start, metadataEnd, end: metadataEnd + bodyLength, header: A.MessageHeader[fb.headerType()], bodyLength, fb });
    start = metadataEnd + bodyLength;
  }
  return found;
}

const visible = A.tableToIPC(A.tableFromArrays({ x: Int32Array.of(1, 2, 3) }), "stream");
const hidden = A.tableToIPC(A.tableFromArrays({ x: Int32Array.of(666, 777, 888) }), "stream");
const [schema, batch, end] = messages(visible);
const hiddenBatch = messages(hidden)[1]!;
const hiddenBytes = hidden.slice(hiddenBatch.start, hiddenBatch.end);

// A Tensor message: the prefix and metadata of the RecordBatch, with the header type 4 (Tensor) and a bodyLength
// that covers the complete RecordBatch message of the hidden stream. Message field 1 (vtable offset 6) is
// header_type and field 3 (vtable offset 10) is bodyLength.
const fb = batch!.fb!;
const tensor = visible.slice(batch!.start, batch!.metadataEnd);
const headerTypePosition = 8 + fb.bb_pos + fb.bb!.__offset(fb.bb_pos, 6);
const bodyLengthPosition = 8 + fb.bb_pos + fb.bb!.__offset(fb.bb_pos, 10);
tensor[headerTypePosition] = A.MessageHeader.Tensor;
new DataView(tensor.buffer).setBigInt64(bodyLengthPosition, BigInt(hiddenBytes.length), true);

const crafted = new Uint8Array([
  ...visible.subarray(schema!.start, schema!.end),
  ...tensor,
  ...hiddenBytes,
  ...visible.subarray(batch!.start, batch!.end),
  ...visible.subarray(end!.start, end!.end),
]);

console.log(`messages by bodyLength: ${messages(crafted).map((m) => `${m.header} (body ${m.bodyLength})`).join(", ")}`);
const rows = (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(`tableFromIPC(original stream): ${rows(visible)}`);
const result = rows(crafted);
console.log(`tableFromIPC(stream with the Tensor message): ${result}`);

if (result.includes("666")) {
  console.log("BUG PRESENT: the reader returns the rows 666, 777 and 888 from the body of the Tensor message.");
  process.exit(1);
}
console.log("OK: the reader skips the body of the Tensor message, or refuses the message.");

Output:

messages by bodyLength: Schema (body 0), Tensor (body 160), RecordBatch (body 16), EOS (body 0)
tableFromIPC(original stream): 3 rows, x = [1,2,3]
tableFromIPC(stream with the Tensor message): 6 rows, x = [666,777,888,1,2,3]
BUG PRESENT: the reader returns the rows 666, 777 and 888 from the body of the Tensor message.
exit code: 1

Expected: An error for a message type that the stream format does not permit, or a reader that skips bodyLength bytes of the message before it reads the next one. The rows 666, 777 and 888 are not in the stream when each message is framed by its bodyLength.

Cause: RecordBatchStreamReaderImpl.next() in src/ipc/reader.ts (lines 504 to 530) has a branch for isSchema(), isRecordBatch() and isDictionaryBatch(), and no else. Only the RecordBatch and DictionaryBatch branches call reader.readMessageBody(message.bodyLength). For each other header type the loop reads the next message from the current position, which is the start of the body. AsyncRecordBatchStreamReaderImpl.next() (lines 579 to 605) has the same loop.

https://github.com/apache/arrow-js/blob/5aa020540217b1aef9571cd3c9473627cd909e2f/src/ipc/reader.ts#L504-L530

Possible fix: Add an else branch to both loops that throws an error that names the header type, because a stream holds only Schema, DictionaryBatch and RecordBatch messages. If the reader must accept such messages, the else branch can read and drop the body with reader.readMessageBody(message.bodyLength).

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

Component(s)

JavaScript

主要言語
TypeScript
スター
114
フォーク
35
平均マージ
13時間 35分
マージ済み PR(30日)
5

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/arrow-js のほかの issue

apache/arrow-js の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。