Skip to content

fix(speedcap): count a user's download when the client side takes it - #15

Draft
Free-Guy-IR wants to merge 4 commits into
mainfrom
fix/003-f102-delivered-count
Draft

Free-Guy-IR wants to merge 4 commits into
mainfrom
fix/003-f102-delivered-count

Conversation

@Free-Guy-IR

@Free-Guy-IR Free-Guy-IR commented Dec 3, 2026 •

Copy link
Copy Markdown
Owner

CI only for now; do not merge until the rollout is approved.

A user's download was counted when the outbound handed bytes to the dispatcher: before the speed cap's bucket wait and before the inbound wrote them to the client. When a client closed a download midway, the read parked in the bucket wait, the bytes left in the downlink pipe and the batch whose write failed were counted but never sent.

Changes (all in patches/, plus guards, test lines and the version):

  • Dispatch path (Shadowsocks, Trojan, VMess, mux sub-sessions): a batch the inbound took from the downlink pipe is counted when the inbound enters its next read, and never if the inbound does not come back after it (for TCP inbounds, a failed write). UDP inbounds that ignore a failed send still count that datagram, as before. Interrupt is the upstream one.
  • Plain HTTP proxy: the handler settles the batch it holds after it relayed the origin's response (not after its own 503). Data an origin sends after Content-Length is still counted, as on 0.6.29.
  • DispatchLink path (VLESS, socks, HTTP CONNECT): the user's SizeStatWriter counts after the bucket wait and after the client writer returned without an error.
  • Uplink is unchanged.
  • xray-stats-register-race.patch keeps its changes; only the context of its dispatcher hunks follows the new lines.
  • NodeVersion 0.6.30.

Tests with real xray processes, run on Linux in a throwaway container (scenario test binary built from this tree):

test 0.6.29 core 2e4c3db 03ac8fe and later
8 aborts per protocol at an 8 Mbit/s cap VLESS +65,536 per abort; Trojan +65,536 to +1,245,184 per abort (+3,932,160, 74.4%) exact exact
48 cap changes racing 62-63 aborts +4,784,128 exact exact
600 downloads of 300,000 bytes reset right after the last byte exact Trojan -9,585,600 exact
8 plain HTTP responses of 20,082 bytes, origin keeps its connection 300 ms exact 120,492 of 160,656 exact

Not changed: bytes that the node's TCP send buffer and a relay still hold when a client aborts are counted as before; for mux over VMess, Trojan or Shadowsocks a sub-session batch counts once it is in the outer mux pipe; a write that fails midway is not counted at all, even the part the kernel took.

The user downlink counter counted bytes when the outbound handed them to
the dispatcher, before the speed cap's bucket wait and before the inbound
wrote them to the client. When a client closed a capped download midway,
the read parked in the bucket wait was counted and never sent: 65,536
bytes in every measured abort. On the Dispatch path the bytes still in
the downlink pipe and the batch whose write failed were counted as well.

- Dispatch path (Shadowsocks, Trojan, VMess, mux sub-sessions): the
  downlink pipe reader counts a batch when the inbound comes back for the
  next read, drops the batch it holds when the inbound interrupts it, and
  at the end of the connection counts a last batch only if the stream
  finished and the pipe is drained.
- DispatchLink path (VLESS, socks, HTTP CONNECT): the user's
  SizeStatWriter counts after the bucket wait and after the client writer
  returned without an error.
- Uplink is unchanged: it is counted when it arrives from the client.
- xray-stats-register-race.patch keeps its changes; only the context of
  its dispatcher hunks follows the new lines.

Evidence on Linux, a throwaway container on the dev box, with the scenario
test binary built from this tree (8 Mbit/s cap, raw VLESS and Trojan
clients that read part of an endless download, drain and reset):
- xray from the previous patch: 8 of 8 aborts per protocol over-counted
  65,536 bytes (VLESS 524,288 bytes, 10.000%; Trojan 524,288 bytes,
  9.861%); 48 cap changes racing 63 aborts: +4,784,128 bytes.
- xray from this patch: 0 bytes on all 16 aborts; 0 bytes in the
  interleaving run.
macOS gave the same per-abort numbers before and after.

Not changed: bytes the node's TCP send buffer and a relay still hold when
a client aborts are counted as before.
NodeVersion goes 0.6.29 to 0.6.30, one patch step from the highest 0.6
value on any branch of the fork on GitHub (0.6.29 on main,
feat/003-speed-cap and ci-003-speed-cap; checked on 2026-12-03), for the
node whose core counts a user's download when the client side takes it.
TestNodeVersionIsPinned failed against the 0.6.29 constant and passes
after the bump. The three upstream-waiver rows that cite the constant
follow.
2e4c3db dropped the batch an inbound held when the inbound interrupted
the downlink pipe, and counted a held batch at the end of the connection
only if the stream had finished and the pipe was empty. Both rules lost
bytes that had reached the client:
- a client that resets right after the last byte makes the inbound
  interrupt the pipe before its copy loop reads again, so the last
  delivered batch was never counted (Linux, 600 Trojan downloads of
  300,000 bytes: 170,414,400 counted for 180,000,000 received);
- a plain HTTP proxy stops reading after the response, so its last batch
  waited for an origin that keeps its connection open (Linux, 8 responses
  of 20,082 bytes: 120,492 counted for 160,656).

Now a batch the inbound took from the pipe is counted when the inbound
enters its next read, whatever happened in between, and is never counted
if the inbound does not come back after it (its write failed). Interrupt
is the upstream one again and the pipe keeps no context hook. The plain
HTTP handler settles the batch it holds after it wrote the response.

Linux, throwaway container, scenario test binary from this tree:
- 0.6.29 core: the capped abort test over-counted 65,536 bytes on each
  VLESS abort and between 65,536 and 1,245,184 bytes on each Trojan abort
  (3,932,160 bytes, 74.4%, over 8 aborts); the reset-after-last-byte and
  plain HTTP tests counted exactly.
- 2e4c3db: the abort tests counted exactly; the two new tests lost the
  bytes above.
- this commit: all four tests count to the byte.
The 65,536 bytes per abort in 2e4c3db's message are the smallest
over-count seen on the 0.6.29 core, not the only one.
…in's response

From the second review of 03ac8fe:
- The plain HTTP handler also settled the batch it held when it answered
  503 itself because the origin's response did not parse. Six broken
  origin responses of 20,013 bytes, the client got only xray's 503 (594
  bytes): 120,078 bytes counted before, 0 now (macOS, review test with
  XRAY_RAY_BUFFER_SIZE=1; the 0.6.29 core also counted 120,078). Data an
  origin sends after Content-Length is still counted, as on 0.6.29.
- The abort tests drained the client socket right after a read whose
  deadline had passed, so the drain never read; the deadline is cleared
  first now.
- docs/upstream-parity.md says the stats register race patch's dispatcher
  hunks have the speed cap patch's code as context.

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.

1 participant