Skip to content

fix(import): parse each JSON array element once, however many reads it spans - #1235

Merged
cameri merged 2 commits into
rewrite/mainfrom
import-parse-once-1101
Nov 23, 2026
Merged

cameri merged 2 commits into
rewrite/mainfrom
import-parse-once-1101

Conversation

@chappie-daemon

Copy link
Copy Markdown
Collaborator

Closes #1101.

Contributed by @AgrimTawani, who cannot open the PR himself; the commit here is theirs, unmodified, cherry-picked from AgrimTawani/nostream:fix/1101-import-scan-once (cf7b034) with its authorship intact.

The mechanism

ArrayStream re-parsed an incomplete array element from its first byte after every 8 KiB read, so a large event cost time quadratic in its size. The reader now finds each element's end as bytes arrive — tracking string contents, escapes and bracket depth — and hands the element to serde_json once its last byte is in. An unfinished element is still parsed each time its length doubles, which bounds what a malformed element buffers to about twice the offset of its fault and keeps total parsing linear. Error messages are unchanged; a bare number split across two reads is no longer cut at the boundary.

Evidence

  • No workflow, dependency or install-time change: 3 files (event_import_service.rs, its tests, one changeset).
  • Hidden-character scan over the diff: no tag characters, no zero-width or bidi controls.
  • The five CLAUDE.md checks, run against this exact tree: cargo fmt --all --check, both clippy invocations, cargo test --workspace --all-features, cargo build --workspace --all-features --release — results in the comments below.
  • 8 new unit tests plus one integration test; the 4 MiB case asserts the parser saw under 3× the input.

…t spans

The array reader re-parsed an incomplete element from its first byte after
every 8 KiB read, so a large event cost time quadratic in its size. It now
scans for the element's end as bytes arrive, keeping the scan's state across
reads, and hands the element to serde_json once it is complete.

An unfinished element is also tried each time it doubles, from 64 KiB, so a
malformed element whose brackets never balance still fails early instead of
buffering the rest of the input; the tries keep parsing linear. Error
messages for malformed and truncated input are unchanged, and a bare scalar
split across two reads is no longer cut at the read boundary.

Closes #1101

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3a1fe8f4-6a31-4010-beb0-f736d95ab26f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the previous buffering issue is fixed, and no new actionable issues remain.

Summary

The PR tracks array element boundaries across reads and uses size-doubling checks to keep parsing work linear.

  • JSON-array imports scan each event across reads before parsing it.

Reviews (2) · Last reviewed commit: "fix(import): probe unfinished scalars, s..." · Reviewed by Greptile

Greptile's P1 on #1235: excluding scalars from the doubling probe left the one
element kind whose scan cannot see an end buffering until EOF. `[` followed by
4 MiB of `x` held all 4,194,305 bytes before the parser saw its first byte.

Scalars now probe on the same schedule. The cost is that a scalar longer than
FIRST_PROBE is refused rather than read: a number prefix parses as a whole value
and the separator check then names the byte after it, which is the same early
failure this path is for. THE 4 MiB regression test fails on the previous commit
and passes here.
@chappie-daemon

Copy link
Copy Markdown
Collaborator Author

Confirmed and fixed in 897ceb0.

The claim measured exactly: with the previous commit, a [ followed by 4 MiB of x held 4,194,305 bytes before the parser saw its first byte — the whole input, buffered to EOF, because probe_due skipped every scalar while ends_scalar never ends on x.

The fix is the one you asked for: scalars probe on the same doubling schedule. A number prefix parses as a whole value, and the separator check then names the byte after it (expected \,` or `]`), so the failure still arrives early rather than as a silently truncated element. The trade is stated in the doc comment and the commit: a scalar longer than FIRST_PROBE` (64 KiB) is now refused instead of read forever — no valid event is a 64 KiB number.

a_malformed_scalar_fails_without_buffering_the_rest_of_the_input is the regression test: it fails on cf7b034 with 4194305 bytes buffered, and passes here. The five CLAUDE.md checks pass on this tree, 1817 tests, 0 failed.

@changeset-bot

changeset-bot Bot commented Nov 23, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 897ceb0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@cameri
cameri merged commit 5ac6996 into rewrite/main Nov 23, 2026
17 checks passed
@cameri
cameri deleted the import-parse-once-1101 branch November 23, 2026 22:35
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.

3 participants