Skip to content

fix(tcp): keep a partial active line across re-arm and passive recv - #108

Merged
mremond merged 1 commit into
mainfrom
infinitary-batch/a3ce6ba7
Dec 3, 2026
Merged

mremond merged 1 commit into
mainfrom
infinitary-batch/a3ce6ba7

Conversation

@mremond

@mremond mremond commented Dec 3, 2026

Copy link
Copy Markdown
Member

Why

Programs that read a TCP connection line by line could lose the beginning of a line: if Nera had already received part of the line when the program paused message delivery, the rest arrived later without that part, while Erlang/OTP keeps the line whole.

What changed

  • Programs using line mode now get the whole line after pausing and resuming message delivery, or after switching to reading it directly.
  • This also holds when the program resumes delivery while it is still turned on.
  • The lost beginning no longer shows up stuck to the front of a later line.
  • When a program switches the connection from lines to raw bytes, it reads those waiting bytes first; a line cut off by the other side closing is still dropped, as in Erlang/OTP.
  • A new test checks nine such cases in Nera and in Erlang/OTP.

Before and after

Before: When abc had arrived, delivery was paused, and def plus a newline came next, Nera handed the program def and the newline, where Erlang/OTP 29 hands over abcdef and the newline; 7 of 9 test cases differed from Erlang/OTP 29.

After: The same program now gets abcdef and the newline either way, and all 9 test cases match Erlang/OTP 29 byte for byte.

Only the bytes a program receives change, with no screen to show; a side-by-side run of Erlang/OTP 29, the previous build and this build stands in.

Checked

  • Nine-case comparison of Erlang/OTP 29 (both socket backends), the previous build and this build: 7 cases differed from Erlang/OTP 29 before the change; all 9 match after it
  • New tcp_line_partial test on the previous build: Fails at its first case, which receives only def and the newline
  • The new test with only one half of the fix: Without either half it fails: 3 of 3 runs for one half, 9 of 10 for the other
  • The new test with the fix, in Nera and in Erlang/OTP 29: 10 of 10 Nera runs passed with no retries; it passes in Erlang/OTP 29 with both backends
  • Resuming delivery while the other side streams data with no line end: The call returns in about 30 microseconds; it reads at most 4 KB before handing over
  • Full local test suite on macOS at the final version: 559 end-to-end tests passed with the 2 usual skips; 2,303 Rust tests passed; the lint is clean; all 19 socket tests passed three runs
How it was checked

cargo clippy --workspace --all-targets --locked -- -D warnings: clean

cargo test --workspace --locked --no-fail-fast: 2,303 passed, 0 failed, 1 ignored

nera test -j 1 --timeout 30: 559 passed, 0 failed, 2 skipped (the OneCached exclusions) of 561

nera test 'tcp_*' -j 1 --timeout 30, three runs: 19 of 19 each

scripts/test-allocator-config.py and scripts/test-stacktrace-order.py: pass

tcp_line_partial on Erlang/OTP 29.1.2 with the inet and socket backends: passes three times each

The fix sets {active, false} and {packet, raw} to move a buffered partial line in front of the bytes later reads take first, and makes the line-mode setopts read start from that partial line.

Risk

Risk: low in every dimension.

Full risk assessment
Dimension Level Why
Blast radius low One read path for line-mode TCP connections and its buffer
Behavior low A defect corrected: the beginning of a line is no longer dropped
Undo low Revert the commits; no saved data or settings change
Evidence low Compared byte for byte with Erlang/OTP 29; the test runs in both
Sensitivity low No sign-ins, keys, saved data or release steps
Novelty low Edits an existing read path
Dependencies low No packages added or updated
Not done

On Windows, where Nera reads connections without its event loop, nothing changed and nothing was run; that reader keeps reading a line it has begun after delivery is paused, as the known limits now say.

Switching a connection that still delivers messages from lines to raw bytes hands over the waiting bytes only when more data arrives; Erlang/OTP 29's socket backend does the same, while its default backend hands them over at once.

Programs using line mode now get the whole line after pausing and resuming message delivery, or after switching to reading it directly.

This also holds when the program resumes delivery while it is still turned on.

The lost beginning no longer shows up stuck to the front of a later line.

When a program switches the connection from lines to raw bytes, it reads those waiting bytes first; a line cut off by the other side closing is still dropped, as in Erlang/OTP.

A new test checks nine such cases in Nera and in Erlang/OTP.
@mremond
mremond merged commit 2983d75 into main Dec 3, 2026
21 checks passed
@mremond
mremond deleted the infinitary-batch/a3ce6ba7 branch December 3, 2026 21:21
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.

1 participant