Skip to content

fix: Make the Arrow stream safe to release from a handler, keep its failures final, and return from every callback - #965

Draft
krlmlr wants to merge 8 commits into
claude/rb-arrow-streamfrom
claude/rb-arrow-stream-robust
Draft

krlmlr wants to merge 8 commits into
claude/rb-arrow-streamfrom
claude/rb-arrow-stream-robust

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

No change of behavior for users: the Arrow stream of #964 is still internal. This makes it robust in three ways that #964 left open.

Release from a handler. A release() called from a handler of a warning that get_next() raises freed the stream's state while the callback still used it (a crash, or an invalid write under valgrind). Now release() during a callback only marks the state released; the callback deletes it once its body has returned, and it holds the result until then, so R may free the result meanwhile. The call that was running finishes; a consumer that calls the released stream again without checking its release callback, as nanoarrow's convert_array_stream() does, gets EINVAL and "Can't read an Arrow stream after it has been released." from get_last_error().

Final failures. After an error of SQLite, the next get_next() returned the end of the stream, as if the rows were complete. Now a failure is final: every later get_next() returns the same error code and get_last_error() keeps the message, also after the result is cleared. get_schema() still returns a schema read before, and fails the same way if the failure came before a schema was read (before, it then reported an empty schema and nanoarrow converted an empty, complete stream). The codes: EIO for an error of SQLite or another failure of the fetch, EINTR for an interrupt of the fetch, EINVAL for a result that is cleared or bound to new parameters and for a released stream, EBUSY for the refused call from a handler (#964), and EAGAIN below. An interrupt of the fetch is final rather than retryable because the interrupted fetch ends the result's execution and loses the rows it had read.

Failed executions. Any failure that ends an execution claimed for Arrow fetches (an error of SQLite, a value beyond the byte limit, running out of memory, an interrupt, an R error) loses the rows of that fetch. The result now records it for the execution: every later Arrow read of it (this stream, another or a new stream on the result, fetch_arrow_chunk_warn()) fails again with the same error, instead of ending as if the rows were complete, until a bind starts a new execution or the result is cleared. An error of SQLite comes back as an RSQLite_error with its code, an interrupt as "An interrupt has ended the Arrow fetches of this result." A chunk the result kept before is still handed out first. dbHasCompleted() is FALSE for such an execution, because its remaining rows weren't read and can't be; so a loop on dbHasCompleted() fails rather than stopping early. This changes the expectations of the chunk fetch tests of #954–#963, which expected TRUE and an empty chunk after a failure, and are updated here. Data frame fetches keep their behavior.

To tell an interrupt from an R error that R raises while it processes events, such as the one of setTimeLimit(), an Arrow fetch checks for interrupts through R_tryCatch(): an interrupt gives EINTR, an R error EIO with the condition's message. Because that costs some microseconds, an Arrow fetch checks every 16384 rows (and every 16000 parameter sets without rows) instead of 1024 and 1000; data frame fetches still let the interrupt go on to R. An R error that leaves the fetch otherwise, such as from a busy handler, gives EIO with "An R condition stopped reading an Arrow stream." The tests simulate an interrupt of an Arrow fetch with an internal hook, result_set_interrupt_after_rows(), which a bind disarms and a data frame fetch ignores.

No R jump through the consumer. The callbacks no longer continue an R jump (R_ContinueUnwind()) through the consumer's C or C++ frames, which the C stream interface forbids. The warnings about converted values are raised by a small R function that catches an error or interrupt among them, such as a warning made an error with options(warn = 2), and returns its message; a jump that still leaves it, as by a handler further out that exits, ends in the callback, which drops the continuation. Either way the callback returns EAGAIN with the message of the condition, and the result keeps the chunk (#962), so the next get_next() hands it out without warning again. An interrupt of the warnings gives "Raising the warnings of an Arrow chunk was interrupted." Such a failure is therefore not final, unless a handler has cleared the result or bound it to new parameters before the jump: then the call fails with EINVAL and the message of #964 for that case, for good. A retry after EAGAIN goes on after the last chunk that a call returned: a consumer that drops the chunks it got before the failure, as convert_array_stream() does, doesn't get them again. Consequences worth knowing: handlers further out don't see an error from a warning made an error; they see the error that nanoarrow raises for the failed callback, with the same message. A tryCatch(warning = ) around the stream no longer exits through its handler; the call fails with the warning's message instead. A condition raised by a handler further out (say, stop() in a calling handler of the warning) is reported with the message of the warning, since the stream can't see it. A jump to the top level, such as an unhandled error in such a handler, invokeRestart("abort") or Q in the browser, also ends in the callback, after R has already reported the error; nanoarrow then reports a second error for the failed callback. The callback can't continue the jump, so this stays.

Tested in test-arrow-stream.R: release from a warning handler during get_next() (also with the result dropped and garbage-collected in the handler) and during convert_array_stream(), calls after release, an SQLite error mid-stream followed by repeated get_next() calls (same code [5] and message each time, also after clearing) and during the schema read (get_schema(), get_next() and convert_array_stream() repeated), EINVAL for a cleared result, options(warn = 2) and an exiting handler both with get_next() and while nanoarrow converts, with the chunk recovered afterwards, a handler that clears or rebinds the result before it exits (EINVAL, final), a simulated interrupt of a fetch, both of the stream's own call and of another stream read from a warning handler, followed by reads through the same stream, another stream, a new stream and fetch_arrow_chunk_warn(), then a rebind, the same for an error of SQLite, and an R error from setTimeLimit() while the fetch checks for interrupts. The tests take the error codes from C rather than hard-coding them. Red on #964's head: 14 failures, then a segfault in the release test; green with the fix. The new test file is clean under valgrind (0 errors, no leaks); the full suite passes with NOT_CRAN=true (FAIL 0).

Layer F6b of the rebuilt chain, planned in #880; based on #964. Ported from the old chain's commits a0f7fee and db5b47a (#864), adapted to #964's design: the state's running flag defers the release, a kept chunk decides whether a failure is final, and the result's clear semantics stay those of #964's weak reference (no clear() that frees the implementation). Keeping the result alive and package unload are left to F7. The third item comes from the review of #964.

Coordinating session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


Generated by Claude Code

…ailures final, and always return from its callbacks

A `release()` from a handler of a warning that `get_next()` raises
freed the state of the stream while the callback still used it.
It now only marks the state released,
and the callback deletes it once its body has returned.
Later callbacks of the released stream fail with `EINVAL`.

A failure is final:
after an error of SQLite, later calls fail with the same code and message
instead of ending the stream as if its rows were complete,
and `get_schema()` fails too until a schema has been read.
A cleared or rebound result fails with `EINVAL`, an error of SQLite with `EIO`.

The callbacks no longer continue an R jump through the consumer's frames.
An error or interrupt while the warnings are raised,
as from a warning made an error with `options(warn = 2)`, is caught in R,
and a jump by a handler further out that exits ends in the callback.
The callback fails with `EAGAIN` and the message of the condition,
and the result keeps the chunk for the next `get_next()`,
so that such a failure is not final.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
… only

`condition_message()` converts the saved message with cpp11,
so that an R error in the conversion ends in its catch,
and the R helper is named `stream_condition_message()`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
claude and others added 3 commits December 1, 2026 09:15
…nds its result and exits

A jump out of the warnings after a handler cleared the result
or bound it to new parameters
now fails the stream for good with `EINVAL` and the message for that case,
rather than with `EIO` and the warning's message.
An interrupt of the fetch fails it with `EINTR`
and the message of an interrupt of the warnings;
it stays final, since the interrupted fetch ends the result's execution.

The internal `arrow_stream_error_codes()` gives the error codes,
which differ between systems, to the tests.
The comments say that a retry after `EAGAIN` goes on after the last chunk returned,
and that a jump to the top level ends in the callback after R has reported it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
…as ended

An interrupt ends the execution of the fetch it interrupts,
and the rows that fetch read are lost.
The result now records it for an execution claimed for Arrow fetches:
every later Arrow fetch of the execution,
through any stream or `fetch_arrow_chunk_warn()`, fails with an error that says so,
until a bind starts a new execution,
and `dbHasCompleted()` is `FALSE`, as its remaining rows can't be read.
A stream reports it with `EINTR`.
Tests simulate the interrupt with the internal `result_set_interrupt_after_rows()`.

Another R condition that leaves the fetch or the stream's R code by a jump,
such as an R error of a busy handler,
fails with `EIO` and a message that doesn't claim an interrupt.
The message of an interrupt of the warnings is written in C++ only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
krlmlr pushed a commit that referenced this pull request Dec 1, 2026
… into claude/rb-arrow-stream-lifetime

Keeps the messages of #965's latest head over the CI's reformatting of the older ones.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
…s ended

Any failure that ends an execution claimed for Arrow fetches,
such as an error of SQLite, a value beyond the byte limit or an interrupt,
loses the rows of its fetch.
The result now records it,
and every later Arrow fetch of the execution raises it again,
through any stream or `fetch_arrow_chunk_warn()`,
until a bind starts a new execution;
`dbHasCompleted()` is `FALSE` for it.
The chunk fetch tests expect this now. Data frame fetches are unchanged.

An Arrow fetch checks for interrupts through `R_tryCatch()`,
so that an R error that R raises while it processes events,
such as the one of `setTimeLimit()`, fails with its message rather than as an interrupt.
It checks every 16384 rows, since the check costs some microseconds.
The tests' simulated interrupt applies to Arrow fetches only,
raises a C++ error rather than jumping to the top level,
and a bind disarms it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4

krlmlr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Question: how often should an Arrow fetch check for interrupts, now that the check tells an interrupt from an R error?

An Arrow fetch checks for a user interrupt every so many rows. R's check also processes events, which can raise ordinary R errors, for example from setTimeLimit(). To report those as errors rather than as interrupts, this pull request runs the check through R_tryCatch(), which costs about 28 µs per check. To keep that cost small, Arrow fetches now check every 16384 rows (about 4 ms of reading) instead of every 1024, see the interval. Data frame fetches keep 1024 and the plain check. One side effect: a real Ctrl-C during a direct internal chunk fetch now arrives as an R error rather than an interrupt condition. Streams still report it as an interrupt (EINTR).

  • Keep the classifying check every 16384 rows for Arrow fetches, as implemented: correct codes, interrupts noticed within milliseconds (recommended)
  • Use the plain check every 1024 rows and report any jump from it as an interrupt: cheapest and most responsive, but an R error such as a time limit is reported as an interrupt
  • Use the plain check every 1024 rows, and classify only after a jump happened, by inspecting R's state: no per-check cost, but it relies on R internals that aren't part of the API

Kirill decides. Until then the first option stays as implemented.


Generated by Claude Code

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants