This is an automated email from the ASF dual-hosted git repository.
paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git
The following commit(s) were added to refs/heads/main by this push:
new 698b118c fix: separate end-of-stream state from input errors (#1038)
698b118c is described below
commit 698b118c42451c367df02a4fa65e7787eb233711
Author: Muhammad Umar Nadeem <[email protected]>
AuthorDate: Fri Nov 27 12:12:06 2026 -0800
fix: separate end-of-stream state from input errors (#1038)
Related to #909.
Keep EOF as a separate reader flag so an input callback returning
`ENODATA` is not mistaken for a successful end of stream. On platforms
without `ENODATA`, use `ENOENT` instead of the synthetic value `120`.
Existing native `ENODATA` returns are preserved.
Added a reader regression for `ENODATA`, `ENOENT` and `EIO`. It failed
on main and passes with the fix. The Debug C/C++ and IPC tests passed
(435 registered, 16 skipped), and pre-commit passed. A build with
`ENODATA` undefined also passed decoder EOF, empty-input and
callback-error checks.
---
src/nanoarrow/ipc/decoder.c | 2 +-
src/nanoarrow/ipc/reader.c | 37 +++++++++++++++++++----------
src/nanoarrow/ipc/reader_test.cc | 50 ++++++++++++++++++++++++++++++++++++++++
src/nanoarrow/nanoarrow_ipc.h | 1 +
4 files changed, 77 insertions(+), 13 deletions(-)
diff --git a/src/nanoarrow/ipc/decoder.c b/src/nanoarrow/ipc/decoder.c
index 63006862..0830638c 100644
--- a/src/nanoarrow/ipc/decoder.c
+++ b/src/nanoarrow/ipc/decoder.c
@@ -28,7 +28,7 @@
// ENODATA is an XSI extension and is not defined by all libcs (e.g., older
// Windows/MinGW toolchains used by R 3.6, FreeBSD, and OpenBSD).
#if !defined(ENODATA)
-#define ENODATA 120
+#define ENODATA ENOENT
#endif
#define NANOARROW_IPC_MAGIC "ARROW1"
diff --git a/src/nanoarrow/ipc/reader.c b/src/nanoarrow/ipc/reader.c
index 6dc1a528..ab24770e 100644
--- a/src/nanoarrow/ipc/reader.c
+++ b/src/nanoarrow/ipc/reader.c
@@ -26,7 +26,7 @@
// ENODATA is an XSI extension and is not defined by all libcs (e.g., older
// Windows/MinGW toolchains used by R 3.6, FreeBSD, and OpenBSD).
#if !defined(ENODATA)
-#define ENODATA 120
+#define ENODATA ENOENT
#endif
// Sentinel value to indicate that we haven't read a message yet
@@ -225,7 +225,9 @@ static void ArrowIpcArrayStreamReaderRelease(struct
ArrowArrayStream* stream) {
}
static int ArrowIpcArrayStreamReaderNextHeader(
- struct ArrowIpcArrayStreamReaderPrivate* private_data, int
schema_expected) {
+ struct ArrowIpcArrayStreamReaderPrivate* private_data, int schema_expected,
+ int* out_end_of_stream) {
+ *out_end_of_stream = 0;
private_data->header.size_bytes = 0;
int64_t bytes_read = 0;
@@ -242,7 +244,8 @@ static int ArrowIpcArrayStreamReaderNextHeader(
// is one of the valid outcomes) but we set the error anyway in case it
gets
// propagated higher (e.g., if the stream is empty and there's no schema
message)
ArrowErrorSet(&private_data->error, "No data available on stream");
- return ENODATA;
+ *out_end_of_stream = 1;
+ return NANOARROW_OK;
} else if (bytes_read == 4 && private_data->expected_header_prefix_size ==
4) {
// Special case very, very old IPC streams that used 0x00000000 as the
// end-of-stream indicator. We may want to remove this case at some point:
@@ -251,7 +254,8 @@ static int ArrowIpcArrayStreamReaderNextHeader(
memcpy(&last_four_bytes, private_data->header.data, sizeof(uint32_t));
if (last_four_bytes == 0) {
ArrowErrorSet(&private_data->error, "No data available on stream");
- return ENODATA;
+ *out_end_of_stream = 1;
+ return NANOARROW_OK;
} else {
ArrowErrorSet(&private_data->error,
"Expected 0x00000000 if exactly four bytes are available
at the end "
@@ -270,8 +274,13 @@ static int ArrowIpcArrayStreamReaderNextHeader(
// Use PeekHeader to fill in decoder.header_size_bytes
int32_t prefix_size_bytes = 0;
- NANOARROW_RETURN_NOT_OK(ArrowIpcDecoderPeekHeader(
- &private_data->decoder, input_view, &prefix_size_bytes,
&private_data->error));
+ int result = ArrowIpcDecoderPeekHeader(&private_data->decoder, input_view,
+ &prefix_size_bytes,
&private_data->error);
+ if (result == ENODATA) {
+ *out_end_of_stream = 1;
+ return NANOARROW_OK;
+ }
+ NANOARROW_RETURN_NOT_OK(result);
// Check for a consistent header prefix size
if (private_data->expected_header_prefix_size !=
kExpectedHeaderPrefixSizeNotSet &&
@@ -389,7 +398,12 @@ static int ArrowIpcArrayStreamReaderReadSchemaIfNeeded(
return NANOARROW_OK;
}
- NANOARROW_RETURN_NOT_OK(ArrowIpcArrayStreamReaderNextHeader(private_data,
1));
+ int end_of_stream;
+ NANOARROW_RETURN_NOT_OK(
+ ArrowIpcArrayStreamReaderNextHeader(private_data, 1, &end_of_stream));
+ if (end_of_stream) {
+ return ENODATA;
+ }
// Error if this isn't a schema message
if (private_data->decoder.message_type != NANOARROW_IPC_MESSAGE_TYPE_SCHEMA)
{
@@ -509,17 +523,16 @@ static int ArrowIpcArrayStreamReaderProcessMessage(
struct ArrowIpcArrayStreamReaderPrivate* private_data,
enum ArrowIpcMessageType* message_type, struct ArrowArray* out) {
// Read + decode the next header
- int result = ArrowIpcArrayStreamReaderNextHeader(private_data, 0);
- if (result == ENODATA) {
+ int end_of_stream;
+ NANOARROW_RETURN_NOT_OK(
+ ArrowIpcArrayStreamReaderNextHeader(private_data, 0, &end_of_stream));
+ if (end_of_stream) {
// Stream is finished either because there is no input or because
// end of stream bytes were read. Read this as a RecordBatch in the
// sense that we populate out->release to NULL and return OK.
*message_type = NANOARROW_IPC_MESSAGE_TYPE_RECORD_BATCH;
out->release = NULL;
return NANOARROW_OK;
- } else if (result != NANOARROW_OK) {
- // Other error
- return result;
}
// Make sure we have a RecordBatch message or DictionaryBatch message
diff --git a/src/nanoarrow/ipc/reader_test.cc b/src/nanoarrow/ipc/reader_test.cc
index 13545a69..1812bb1e 100644
--- a/src/nanoarrow/ipc/reader_test.cc
+++ b/src/nanoarrow/ipc/reader_test.cc
@@ -527,6 +527,56 @@ TEST(NanoarrowIpcReader, StreamReaderEmptyInput) {
ArrowArrayStreamRelease(&stream);
}
+TEST(NanoarrowIpcReader, StreamReaderPropagatesEndOfStreamReadError) {
+ for (int error_code : {ENODATA, ENOENT, EIO}) {
+ SCOPED_TRACE(error_code);
+ struct ErrorInput {
+ struct ArrowIpcInputStream input;
+ int error_code;
+ } error_input;
+ error_input.error_code = error_code;
+
+ struct ArrowBuffer input_buffer;
+ ArrowBufferInit(&input_buffer);
+ ASSERT_EQ(ArrowBufferAppend(&input_buffer, kSimpleSchema,
sizeof(kSimpleSchema)),
+ NANOARROW_OK);
+ ASSERT_EQ(ArrowIpcInputStreamInitBuffer(&error_input.input, &input_buffer),
+ NANOARROW_OK);
+
+ struct ArrowIpcInputStream input;
+ input.private_data = &error_input;
+ input.read = [](struct ArrowIpcInputStream* stream, uint8_t* buf, int64_t
size_bytes,
+ int64_t* size_read_out, struct ArrowError* error) {
+ auto* state = static_cast<ErrorInput*>(stream->private_data);
+ int result =
+ state->input.read(&state->input, buf, size_bytes, size_read_out,
error);
+ if (result == NANOARROW_OK && *size_read_out == 0) {
+ ArrowErrorSet(error, "Injected input read error");
+ return state->error_code;
+ }
+ return result;
+ };
+ input.release = [](struct ArrowIpcInputStream* stream) {
+ auto* state = static_cast<ErrorInput*>(stream->private_data);
+ state->input.release(&state->input);
+ stream->release = nullptr;
+ };
+
+ struct ArrowArrayStream stream;
+ ASSERT_EQ(ArrowIpcArrayStreamReaderInit(&stream, &input, nullptr),
NANOARROW_OK);
+ struct ArrowSchema schema;
+ ASSERT_EQ(ArrowArrayStreamGetSchema(&stream, &schema, nullptr),
NANOARROW_OK);
+ ArrowSchemaRelease(&schema);
+
+ struct ArrowArray array;
+ struct ArrowError error;
+ ArrowErrorInit(&error);
+ EXPECT_EQ(ArrowArrayStreamGetNext(&stream, &array, &error), error_code);
+ EXPECT_STREQ(error.message, "Injected input read error");
+ ArrowArrayStreamRelease(&stream);
+ }
+}
+
TEST(NanoarrowIpcReader, StreamReaderIncompletePrefix) {
struct ArrowBuffer input_buffer;
ArrowBufferInit(&input_buffer);
diff --git a/src/nanoarrow/nanoarrow_ipc.h b/src/nanoarrow/nanoarrow_ipc.h
index d10f2fc1..d5d56c02 100644
--- a/src/nanoarrow/nanoarrow_ipc.h
+++ b/src/nanoarrow/nanoarrow_ipc.h
@@ -638,6 +638,7 @@ typedef ArrowErrorCode
(*ArrowIpcMessageMetadataVisitFunction)(
/// these bytes and returns ESPIPE if there are not enough remaining bytes in
data to read
/// the entire header message, EINVAL if the first 8 bytes are not valid,
ENODATA if the
/// Arrow end-of-stream indicator has been reached, or NANOARROW_OK otherwise.
+/// On platforms where ENODATA is not defined, ENOENT is returned for end of
stream.
///
/// Pre-1.0 messages were not prefixed with 0xFFFFFFFF. For these messages, a
value
/// of 4 will be placed into prefix_size_bytes; otherwise a value of 8 will be
placed