Response body is never freed when a keep-alive client disconnects mid-transfer
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 58/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- backend, networking
Hướng nghiên cứu
Start in src/uvutil.cpp: read ExtendedWrite::next(), writecb and WriteOp::end() together, and check the _errored branch in next() that closes the data source. Then compare the keep-alive path in src/httprequest.cpp (_on_request_read calling close()) against the Connection: close path where reading stops first. Rebuild the package and run the report's script on a keep-alive connection; done means the aborted body's memory is released and the owned = TRUE temp file is gone without stopServer().
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
When a client on a keep-alive connection disconnects while httpuv is still sending a response body, the body is never released. A raw-vector body stays in memory, and a list(file = ..., owned = TRUE) body stays open (and, on Windows, on disk) until the R process exits. With Connection: close, the same disconnect frees the response as expected.
Reproducible example
library(httpuv)
port <- 8767
files <- character()
s <- startServer("127.0.0.1", port, list(call = function(req) {
if (req$PATH_INFO == "/file") {
f <- tempfile("owned-")
writeBin(raw(30e6), f)
files <<- c(files, f)
body <- list(file = f, owned = TRUE)
} else {
body <- raw(30e6)
}
list(status = 200L, headers = list("Content-Type" = "application/octet-stream"), body = body)
}))
# In another process: request `path`, read about 2 MB slowly, then disconnect
abort_download <- function(port, path, keep_alive) {
con <- socketConnection("127.0.0.1", port, blocking = TRUE, open = "r+b")
writeLines(paste0("GET ", path, " HTTP/1.1\r\nHost: localhost\r\n",
if (!keep_alive) "Connection: close\r\n", "\r\n"), con, sep = "")
for (i in 1:32) { readBin(con, "raw", 65536); Sys.sleep(0.02) }
close(con)
}
run <- function(path, keep_alive) {
p <- callr::r_bg(abort_download, list(port = port, path = path, keep_alive = keep_alive))
while (p$is_alive()) service(50)
for (i in 1:40) service(50)
}
rss_mb <- function() ps::ps_memory_info()[["rss"]] / 1e6
for (i in 1:3) run("/file", keep_alive = FALSE)
for (i in 1:3) run("/file", keep_alive = TRUE)
invisible(gc())
cat("Owned files still on disk, Connection: close:", sum(file.exists(files[1:3])), "of 3\n")
cat("Owned files still on disk, keep-alive: ", sum(file.exists(files[4:6])), "of 3\n")
before <- rss_mb()
for (i in 1:5) run("/memory", keep_alive = FALSE)
invisible(gc())
cat(sprintf("RSS change after 5 aborted 30 MB raw bodies, Connection: close: %+.0f MB\n", rss_mb() - before))
before <- rss_mb()
for (i in 1:5) run("/memory", keep_alive = TRUE)
invisible(gc())
cat(sprintf("RSS change after 5 aborted 30 MB raw bodies, keep-alive: %+.0f MB\n", rss_mb() - before))
stopServer(s)
for (i in 1:40) service(50)
invisible(gc())
cat("Owned files still on disk after stopServer():", sum(file.exists(files)), "of 6\n")
Output:
Owned files still on disk, Connection: close: 0 of 3
Owned files still on disk, keep-alive: 3 of 3
RSS change after 5 aborted 30 MB raw bodies, Connection: close: +0 MB
RSS change after 5 aborted 30 MB raw bodies, keep-alive: +150 MB
Owned files still on disk after stopServer(): 3 of 6
The client has to disconnect after the body has started to stream. A client that disconnects right after the headers arrive does not trigger it. Killing the client process mid-transfer instead of closing the socket gives the same result.
Environment: httpuv 1.6.17, R 4.6.2 (ucrt), Windows 11 x64. I have not tested Linux or macOS. There the owned file is unlinked when it is opened, so I would expect the open descriptor and the disk space it holds to leak instead of a visible file.
What seems to happen
This comes from reading the 1.6.17 source, not from tracing it, so treat it as a likely explanation:
- On a keep-alive connection, httpuv keeps reading the socket while it writes the response. When the client disconnects,
_on_request_readgetsUV_EOForUV_ECONNRESETand callsclose(), whichuv_close()s the handle while the body'sExtendedWriteis still running. WithConnection: close, httpuv stops reading first, so this path is never taken. The writes then fail one by one until the body runs out, and the response is freed. writecbandWriteOp::end()ignore the write's status. After a cancelled write they callnext()again.ExtendedWrite::next()increments_activeWritesand callsuv_write()on the closing handle without checking its return value. libuv rejects the write at once and never calls its callback, so_activeWritesnever returns to 0.- Without that,
_pDataSource->close()andonWriteComplete()never run. TheHttpResponseExtendedWriteis never deleted, and it holds theHttpResponseand the data source. For an owned file,FileDataSource::close()never runs, so on Windows theFILE_FLAG_DELETE_ON_CLOSEhandle is never closed and the file is never deleted.
Possible fix
- Check the return value of
uv_write()inExtendedWrite::next(). On failure, undo the_activeWritesincrement and set_errored. - Treat a non-zero
statusinwritecb/WriteOp::end()as an error.
next() then reaches its existing _errored branch, which closes the data source and completes the write.
Impact
Every client that disconnects mid-download on a keep-alive connection keeps that whole response body in the server process. This covers a closed tab, a cancelled download, or a dropped mobile connection. A server that runs for weeks can therefore run out of memory or disk. Anyone who can reach the server can trigger it on purpose by repeatedly requesting a large response and disconnecting partway through. I found this in a reverse proxy written with httpuv, which hands app responses of 8 MB or more to httpuv as owned files.
- Ngôn ngữ chính
- C
- Star
- 253
- Fork
- 87
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của rstudio/httpuv
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 46/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 30/100
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
Tất cả issue của rstudio/httpuv
Issue tương tự
-
Cyberpunk not listed in readmeĐang mở
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Polprzewodnikowy/N64FlashcartMenu#486 · 3 bình luận ·
-
docs: EigenLua is labelled private in docs/CI.md and consumer_acceptance.sh, but the repo is publicĐang mởarea:docs kind:bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
InauguralSystems/EigenScript#2602 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[raygui] update to 5.0Đang mởcategory:port-update
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
Maintainer thường phản hồi trong vòng 2 ngày