Skip to content

buffer: drop the reader reference after copying - #17695

Open
copybara-service[bot] wants to merge 1 commit into
masterfrom
test/cl1022522754
Open

copybara-service[bot] wants to merge 1 commit into
masterfrom
test/cl1022522754

Conversation

@copybara-service

Copy link
Copy Markdown

buffer: drop the reader reference after copying

Netstack TCP endpoints reuse one io.LimitedReader across writes, and
WriteFromReaderAndLimitedReader() left the caller's reader in lr.R
after the copy. For write(2) and writev(2) that reader is a field of the
socket, so a closed socket stayed reachable until its endpoint was
freed, which can be a minute or more after close in FIN_WAIT_2 or
TIME_WAIT. Servers that churn short connections held that memory the
whole time.

Set lr.R to nil once io.Copy() returns. The LimitedReader is still
reused, so no LimitedReader is allocated per write.

On nginx serving 80,000 fresh connections, peak server memory drops by
18% on systrap (251 to 205 MB).

Reviewed and tested by me.

Assisted-by: Claude Code
FUTURE_COPYBARA_INTEGRATE_REVIEW=#17685 from sam10ridh:Samridh/reader-retention-20261115 8f3c49f

Netstack TCP endpoints reuse one `io.LimitedReader` across writes, and
`WriteFromReaderAndLimitedReader()` left the caller's reader in `lr.R`
after the copy. For write(2) and writev(2) that reader is a field of the
socket, so a closed socket stayed reachable until its endpoint was
freed, which can be a minute or more after close in FIN_WAIT_2 or
TIME_WAIT. Servers that churn short connections held that memory the
whole time.

Set `lr.R` to nil once `io.Copy()` returns. The `LimitedReader` is still
reused, so no `LimitedReader` is allocated per write.

On nginx serving 80,000 fresh connections, peak server memory drops by
18% on systrap (251 to 205 MB).

Reviewed and tested by me.

Assisted-by: Claude Code
FUTURE_COPYBARA_INTEGRATE_REVIEW=#17685 from sam10ridh:Samridh/reader-retention-20261115 8f3c49f
PiperOrigin-RevId: 1022522754
@copybara-service copybara-service Bot added the exported Issue was exported automatically label Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

exported Issue was exported automatically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant