Repository navigation
fix(import): parse each JSON array element once, however many reads it spans - #1235
Conversation
…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 <[email protected]>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
[Medium impact] The PR appears safe to merge; the previous buffering issue is fixed, and no new actionable issues remain.
|
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.
|
Confirmed and fixed in 897ceb0. The claim measured exactly: with the previous commit, a 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 (
|
🦋 Changeset detectedLatest commit: 897ceb0 The changes in this PR will be included in the next version bump. This PR includes changesets to release 0 packagesWhen 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 |
## What The scanner that finds where a JSON array element ends has produced the two faults this reader has had — an element spanning several reads parsed once per read (`#1235`), and a malformed *scalar* buffering 4 MiB before anything looked at it. Both were found by review. Neither is reachable by a test that enumerates cases. `crates/nostream-scan` extracts `ElementEnd` and `ends_scalar` verbatim from `nostream-services`, with **no dependencies** on purpose: Kani builds every dependency of the crate it verifies, and verifying in place would spend minutes compiling tokio, reqwest, sqlx and secp256k1's C sources to reason about 130 lines of state machine. The reader remains the only caller, so there is no copy to drift. Five harnesses, gated on `cfg(kani)`, beside the code in `proofs.rs`: | harness | property | |---|---| | `any_bytes_never_panic` | all 2^32 four-byte inputs; the reader indexes a buffer with the offset this machine produces | | `a_scalar_is_probed_on_the_same_schedule` | the regression: an unfinished scalar past `FIRST_PROBE` is handed to the parser | | `the_probe_schedule_only_advances` | the threshold **doubles** (a stalled reader cannot re-parse), only moves forward, and a probe is due exactly at it | | `resume_past_a_scalar_advances` | the reader cannot spin on one delimiter | | `a_second_pass_examines_nothing_new` | work is linear: a repeat pass examines none of the bytes again, and `visits <= scanned + 1` across calls | ## Evidence - `cargo kani -p nostream-scan` → **5 successfully verified harnesses, 0 failures**, 0 of 246 checks failed, ~0.6 s. - `cargo test -p nostream-services --lib` → 32 passed, the seven import tests included. `cargo fmt --all --check` clean, `cargo check --workspace --all-targets` clean with no warnings. - **The proofs can fail**, checked for each: - pre-fix state machine (`probe_due` returning `false` for scalars) → `an unfinished scalar past FIRST_PROBE must be handed to the parser`; - threshold set to `len` instead of doubling → `the next threshold is at least twice the length just probed`; - the visits counter is deliberately not an absolute count, because a variant that wipes the whole scanner wipes the counter with it — so the harness asserts the relationship between bytes examined and bytes consumed, which is the property the reader relies on. ## CI `checks.yml` gains `kani-proofs` on the existing `rust` path filter: it installs `kani-verifier` 0.68.0 and runs `cargo kani -p nostream-scan`. It deliberately does not use a toolchain action — Kani brings the nightly it was built against, and the repository's own pin in `rust-toolchain.toml` must not be honoured. ## Not in scope Verifying the relay itself. Most of it is async I/O over external state where a bounded model check proves a property of the model, not of the code; the scanner is the part where exhaustive checking is cheap and the fault class is real.
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
ArrayStreamre-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 toserde_jsononce 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
event_import_service.rs, its tests, one changeset).CLAUDE.mdchecks, 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.