Repository navigation
fix(#6180, #6176): a truncated batch upload is never served its own bytes again - #6215
Conversation
…ytes again POST /api/v1/batch could load records the client never sent in that request: its own request head, and a replay of records it had already committed. On a type with a unique index the replay collided with what it duplicated, and the load was answered 409 Conflict for a key the client had sent exactly once instead of the 408 and the partial-commit counts a truncated upload is contracted to get (#6176). On a type without one - which is what a bulk load usually has - it duplicated rows and answered 200. Measured before changing anything, by instrumenting the body stream of a 100-record upload cut after 4600 bytes: read before=0 read=4600 head=[{"@type":"vertex",...,"id":3000000}|...] available() threw java.io.IOException: UT000128: Remote peer closed connection before all data could be read at java.base/sun.nio.cs.StreamDecoder.inReady(StreamDecoder.java:430) read before=4600 read=8192 head=[POST /api/v1/batch/graph?vertexBatchSize=1 HTTP/1.1|Host: 1] The connection is what lies, and the reader is what asks it. UndertowInputStream.readIntoBufferNonBlocking takes a buffer from the pool BEFORE the channel read that throws and does not give it back, so the buffer stays on the stream holding whatever the pool last left in it - identified here by object identity as the very buffer that held this connection's request head and body (limit 16364 = capacity, zeros past byte 4801). The next read is served from it without the channel being touched at all. What reaches the failure is InputStreamReader, which probes with available() between decodes (StreamDecoder.inReady) and SWALLOWS the IOException: the failure that should have ended the load disappears, and the parser is handed the replay as payload. Sent in two record-aligned chunks, that replay is whole records - reads of 8192 and 8172 bytes past the end of the upload, starting at the exact record the second chunk started with, and 40284 bytes consumed against the 23920 the client sent. CountingInputStream now ends a body that has failed. The first failure is remembered whether it surfaces on a read or on a probe; the probe answers -1, which is Undertow's own "the body is finished" and exactly what the handler's truncation check already reads, so the decoder stops asking instead of swallowing anything; and every later read is refused with the recorded failure itself, which sends the load down the truncated-body path it was always meant to take - 408, the counts needed to resume, and not one record the client did not send. A follower relaying a batch to the leader reads through the same guarded stream, so a cut upload cannot relay a replay of itself onward either. Every answer now carries bytesRead, not only the successful one. On a truncated load that is where a client learns how far the server got, and it is the number that says the server read no further than the client wrote: Issue5470BatchStreamStallIT asserts it in both truncation tests, and the existing one fails without the fix with 20964 bytes read against the 4600 sent. The OpenAPI batch response and error schemas were completed to match what both bodies have actually carried since #5618: bytesRead, linesRead, linesSkipped and verticesWithoutId. Tests: PostBatchHandlerBodyFailureTest drives the stream directly, which is the only way to reproduce a body that fails on a probe and then offers bytes anyway; 3 of its 4 tests go red with the guard removed (the fourth is the intact-body control). The record-aligned IT is the shape rather than the detector - which buffer the pool hands back is not under a test's control - and says so. Verified green: 4 batch IT classes (48 tests), 4 more that touch the endpoint (33), and the handler and OpenAPI unit tests (19). Closes #6180, closes #6176.
|
Tick the box to add this pull request to the merge queue (same as
|
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 10 |
🟢 Coverage 85.37% diff coverage
Metric Results Coverage variation Report missing for 82db25a1 Diff coverage ✅ 85.37% diff coverage Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (82db25a) Report Missing Report Missing Report Missing Head commit (5382f09) 190567 137598 72.20% Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch:
<coverage of head commit> - <coverage of common ancestor commit>Diff coverage details
Coverable lines Covered lines Diff coverage Pull request (#6215) 41 35 85.37% Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified:
<covered lines added or modified>/<coverable lines added or modified> * 100%1 Codacy didn't receive coverage data for the commit, or there was an error processing the received data. Check your integration for errors and validate that your coverage setup is correct.
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
|
Review Solid fix, and unusually well-verified for a bug this subtle (pooled-buffer reuse across a failed non-blocking read is not something you can reliably reproduce in CI, so the direct unit test that fakes the "fails-on-probe-then-offers-stale-bytes" connection in Correctness
Design / reuse
Tests
Nits
Security Nothing here blocks merging; the |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6215 +/- ##
==========================================
- Coverage 70.00% 69.98% -0.02%
==========================================
Files 1836 1836
Lines 158151 158224 +73
Branches 33370 33384 +14
==========================================
+ Hits 110717 110738 +21
- Misses 33578 33630 +52
Partials 13856 13856 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
ReviewThorough fix with strong test coverage (unit tests that drive Correctness
In the current code this looks unreachable: both Style / consistencyDoc formatting nit - Every other multi-issue entry in this file uses one line with comma-separated links (e.g. Test coverageCoverage looks solid:
One thing I didn't see covered: a test where the forwarding path ( Nothing else flaggedNo security concerns (the fix tightens data integrity guarantees, doesn't loosen any), no performance concerns (the guard is O(1) per call and only relevant on the failure path), and the |
Two issues, one defect:
POST /api/v1/batchcould apply records the client never sent in that request.What was happening
Issue5470BatchStreamStallIT.aBodyShorterThanItsContentLengthIsNotReportedAsSuccesshas been red onmainwith409 Conflict where it expects 408. #6180 read that correctly: 409 is the duplicate-key mapping, and the ids in
that payload are unique within it and unique in a fresh database, so reaching it means the records were inserted
twice. #6176 filed the same failure from the status-code end.
Measured before changing anything, by instrumenting the handler's body stream on a 100-record upload cut after
4600 bytes:
The second read is the connection's own request head. The chain:
UndertowInputStream.readIntoBufferNonBlockingallocates a pooled buffer before the channel read thatthrows, and does not release it on the way out. The buffer stays on the stream, holding whatever the pool last
left in it - identified here by object identity as the very buffer that held this connection's request head and
body (
limit 16364 = capacity, zeros past byte 4801).read()sees a non-null buffer and is served from it without touching the channel at all.InputStreamReader: it probes withavailable()between decodes(
StreamDecoder.inReady) and swallows theIOException. The failure that should have ended the loaddisappears, and the replay is parsed as payload.
Sent in two record-aligned chunks, the replay is whole records: reads of 8192 and 8172 bytes past the end of the
upload, starting at the exact record the second chunk started with, and 40284 bytes consumed against the 23920 the
client sent. On a type with a unique index those re-applied records collide and the load is answered 409 for a key
the client sent once. On a type without one - what a bulk load usually has - they duplicate rows and the load is
answered 200.
One correction to #6176's second claim: the "interrupted after 0 vertices and 0 edges" log line quoted there
belongs to the sibling test
stalledClientIsCutOffAndAnswered, which legitimately reports 0 - it sends a singlevertex with the default
vertexBatchSize, so nothing has been flushed when the client goes silent. The resumecounts were never lost.
The fix
PostBatchHandler.CountingInputStreamends a body that has failed:-1- Undertow's own "the body is finished", whichbodyEndedEarly/drainAlreadyBufferedalready read - so the decoder stops asking instead of swallowing anything;
always meant to take: 408, the counts needed to resume, and not one record the client did not send.
The follower-to-leader forwarding path now reads through the same guarded stream, so a cut upload cannot relay a
replay of itself to the leader either.
Every answer carries
bytesReadnow, not only the successful one: on a truncated load that is where a clientlearns how far the server got, and it is the number that says the server read no further than the client wrote.
The OpenAPI batch response and error schemas were completed to match what both bodies have carried since #5618
(
bytesRead,linesRead,linesSkipped,verticesWithoutId).Verification
PostBatchHandlerBodyFailureTest(new, 4 tests) drives the stream directly - the only way to reproduce a bodythat fails on a probe and then offers bytes anyway. 3 of the 4 go red with the guard removed; the fourth is
the intact-body control.
Issue5470BatchStreamStallIT: the existing truncation test now assertsbytesRead, and fails without the fixwith 20964 bytes read against the 4600 sent. A new test covers the record-aligned shape, where the replay
carries whole records; its javadoc says plainly that it is the shape and not the detector, because which buffer
the pool hands back is not under a test's control.
Issue5470BatchStreamStallIT,Issue5470BatchErrorDeliveryIT,PostBatchHandlerIT,Issue5618BatchLineAccountingIT(48 tests);RemoteGraphBatchIT,OpenApiSpecGenerationIT,UngatedHandlerCrossDatabaseIT,UngatedHandlerPerTypeAclIT(33);PostBatchHandlerBodyFailureTest,PostBatchHandlerForwardRequestTest,CoreApiSpecTest(19).Closes #6180, closes #6176.