From f87179d0c1f89c60fa493ce9120c9c2fa8218b0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 30 Sep 2026 11:14:58 -0600 Subject: [PATCH] HTTP: Keep abandoned naett requests' sinks and POST bodies alive Join() deliberately leaks the naett objects of a request still running at shutdown and parks its sink, so a late chunk lands somewhere valid. But HTTPSShutdown, which runs right after CancelAll, deleted those sinks, and the POST body naett reads in place was owned by the request being destroyed. On Android, where NativeShutdown isn't process exit, a late WinHTTP/JNI callback could then write to or read from freed memory. Only free the list when it's empty (that still fixes the leak the delete was added for), and keep the POST body in the sink. Co-Authored-By: Claude Opus 5.5 (1M context) --- Common/Net/HTTPNaettRequest.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Common/Net/HTTPNaettRequest.cpp b/Common/Net/HTTPNaettRequest.cpp index 8161e6d92a..b2991274f2 100644 --- a/Common/Net/HTTPNaettRequest.cpp +++ b/Common/Net/HTTPNaettRequest.cpp @@ -31,6 +31,8 @@ HTTPSRequest::~HTTPSRequest() { // still exists instead of in a destroyed object. struct NaettBodySink { Buffer buffer; + // naett reads the POST body in place while uploading, so it lives here too. + std::string postData; // Written by us, read by the transfer thread. std::atomic cancelled{false}; }; @@ -43,8 +45,11 @@ struct NaettBodySink { static std::vector> *g_abandonedSinks = nullptr; void HTTPSShutdown() { - delete g_abandonedSinks; - g_abandonedSinks = nullptr; + // Free the list only if it's empty. An abandoned sink can still be written to. + if (g_abandonedSinks && g_abandonedSinks->empty()) { + delete g_abandonedSinks; + g_abandonedSinks = nullptr; + } } int HTTPSRequest::WriteBodyThunk(const void *source, int bytes, void *userData) { @@ -73,6 +78,13 @@ void HTTPSRequest::Start() { _dbg_assert_(!req_); _dbg_assert_(!res_); + // Our own writer, so that Cancel() can actually stop a transfer rather than just relabelling + // it once it finishes. + sink_ = std::make_unique(); + // In case someone managed to cancel us between construction and here. + sink_->cancelled = cancelled_; + sink_->postData = postData_; + std::vector options; options.push_back(naettMethod(method_ == RequestMethod::GET ? "GET" : "POST")); options.push_back(naettHeader("Accept", acceptMime_)); @@ -83,18 +95,13 @@ void HTTPSRequest::Start() { if (method_ == RequestMethod::POST) { if (!postData_.empty()) { // Note: Naett does not take ownership over the body. - options.push_back(naettBody(postData_.data(), (int)postData_.size())); + options.push_back(naettBody(sink_->postData.data(), (int)sink_->postData.size())); } } else { _dbg_assert_(postData_.empty()); } // 30 s timeout - not sure what's reasonable? options.push_back(naettTimeout(30 * 1000)); // milliseconds - // Our own writer, so that Cancel() can actually stop a transfer rather than just relabelling - // it once it finishes. - sink_ = std::make_unique(); - // In case someone managed to cancel us between construction and here. - sink_->cancelled = cancelled_; options.push_back(naettBodyWriter(&HTTPSRequest::WriteBodyThunk, sink_.get())); const naettOption **opts = (const naettOption **)options.data();