From 8e92fef5279202f590c86ef856d1ca70965be1d3 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: Publish request completion with release/acquire ordering HTTPRequest::completed_ was a plain bool set on the download thread, and naett's res->complete a plain int set on its transfer thread. On ARM, the thread polling Done() could see the flag before the result code or body written ahead of it, and report a stale code or a partial body. Co-Authored-By: Claude Opus 5.5 (1M context) --- Common/Net/HTTPClient.h | 5 ++++- ext/naett-lib/src/naett_android.c | 4 ++-- ext/naett-lib/src/naett_core.c | 2 +- ext/naett-lib/src/naett_internal.h | 22 ++++++++++++++++++++++ ext/naett-lib/src/naett_linux.c | 8 ++++---- ext/naett-lib/src/naett_osx.c | 4 ++-- ext/naett-lib/src/naett_win.c | 18 +++++++++--------- 7 files changed, 44 insertions(+), 19 deletions(-) diff --git a/Common/Net/HTTPClient.h b/Common/Net/HTTPClient.h index b678a2c7e1..65f61e0720 100644 --- a/Common/Net/HTTPClient.h +++ b/Common/Net/HTTPClient.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -124,7 +125,9 @@ private: std::string postData_; std::thread thread_; std::string postMime_; - bool completed_ = false; + // Set last on the download thread, after the result code and buffer. Atomic, so that once + // Done() sees it, those are visible too. + std::atomic completed_{false}; bool failed_ = false; net::ResolveFunc customResolve_; }; diff --git a/ext/naett-lib/src/naett_android.c b/ext/naett-lib/src/naett_android.c index 3d433200db..d2125dc4a9 100644 --- a/ext/naett-lib/src/naett_android.c +++ b/ext/naett-lib/src/naett_android.c @@ -314,7 +314,7 @@ static void* processRequest(void* data) { res->code = statusCode; finally: - res->complete = 1; + naettSetComplete(res); (*env)->PopLocalFrame(env, NULL); JavaVM* vm = getVM(); (*env)->ExceptionClear(env); @@ -332,7 +332,7 @@ static void startWorkerThread(InternalResponse* res) { LOGE("Failed to start the request worker thread"); res->workerThread = 0; res->code = naettGenericError; - res->complete = 1; + naettSetComplete(res); } else { pthread_setname_np(res->workerThread, "naett worker thread"); } diff --git a/ext/naett-lib/src/naett_core.c b/ext/naett-lib/src/naett_core.c index 7140beaebd..27788e2149 100644 --- a/ext/naett-lib/src/naett_core.c +++ b/ext/naett-lib/src/naett_core.c @@ -390,7 +390,7 @@ naettReq* naettGetRequest(naettRes* response) { int naettComplete(const naettRes* response) { assert(response != NULL); InternalResponse* res = (InternalResponse*)response; - return res->complete; + return naettLoadComplete(res); } int naettGetStatus(const naettRes* response) { diff --git a/ext/naett-lib/src/naett_internal.h b/ext/naett-lib/src/naett_internal.h index 8631b0a83d..17c7147e49 100644 --- a/ext/naett-lib/src/naett_internal.h +++ b/ext/naett-lib/src/naett_internal.h @@ -111,6 +111,28 @@ typedef struct { #endif } InternalResponse; +// PPSSPP: complete is set on a transfer thread and polled on the caller's. Publish it with release +// semantics and read it with acquire, so that the status code and body written before it are +// visible once it reads as set. With plain accesses, ARM can reorder them. +static inline void naettSetComplete(InternalResponse* res) { +#ifdef _MSC_VER + MemoryBarrier(); + *(volatile int*)&res->complete = 1; +#else + __atomic_store_n(&res->complete, 1, __ATOMIC_RELEASE); +#endif +} + +static inline int naettLoadComplete(InternalResponse* res) { +#ifdef _MSC_VER + int complete = *(volatile int*)&res->complete; + MemoryBarrier(); + return complete; +#else + return __atomic_load_n(&res->complete, __ATOMIC_ACQUIRE); +#endif +} + void naettPlatformInit(naettInitData initData); int naettPlatformInitRequest(InternalRequest* req); void naettPlatformMakeRequest(InternalResponse* res); diff --git a/ext/naett-lib/src/naett_linux.c b/ext/naett-lib/src/naett_linux.c index ad505137e6..112a8bc76f 100644 --- a/ext/naett-lib/src/naett_linux.c +++ b/ext/naett-lib/src/naett_linux.c @@ -64,7 +64,7 @@ static void* curlWorker(void* data) { long responseCode = 0; curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &responseCode); res->code = (int)responseCode; - res->complete = 1; + naettSetComplete(res); // PPSSPP: curl wants the easy handle out of the multi before it's cleaned up. curl_multi_remove_handle(mc, handle); curl_easy_cleanup(handle); @@ -237,14 +237,14 @@ void naettPlatformMakeRequest(InternalResponse* res) { // No libcurl, or the worker died. Complete the request as failed rather than // leaving the caller waiting forever on a request nobody is going to run. res->code = naettGenericError; - res->complete = 1; + naettSetComplete(res); return; } CURL* c = curl_easy_init(); if (c == NULL) { res->code = naettGenericError; - res->complete = 1; + naettSetComplete(res); return; } curl_easy_setopt(c, CURLOPT_URL, req->url); @@ -303,7 +303,7 @@ void naettPlatformMakeRequest(InternalResponse* res) { res->headerList = NULL; curl_easy_cleanup(c); res->code = naettGenericError; - res->complete = 1; + naettSetComplete(res); } } diff --git a/ext/naett-lib/src/naett_osx.c b/ext/naett-lib/src/naett_osx.c index a583d09057..9e936e7630 100644 --- a/ext/naett-lib/src/naett_osx.c +++ b/ext/naett-lib/src/naett_osx.c @@ -161,7 +161,7 @@ void didReceiveData(id self, SEL _sel, id session, id dataTask, id data) { int written = res->request->options.bodyWriter(bytes, (int)length, res->request->options.bodyWriterData); if (written != (int)length) { res->code = naettReadError; - res->complete = 1; + naettSetComplete(res); objc_msgSend_void(dataTask, sel("cancel")); release(p); return; @@ -183,7 +183,7 @@ static void didComplete(id self, SEL _sel, id session, id dataTask, id error) { if (error != nil) { res->code = naettConnectionError; } - res->complete = 1; + naettSetComplete(res); } } diff --git a/ext/naett-lib/src/naett_win.c b/ext/naett-lib/src/naett_win.c index 84c38340b6..d9bfcb9ae2 100644 --- a/ext/naett-lib/src/naett_win.c +++ b/ext/naett-lib/src/naett_win.c @@ -161,7 +161,7 @@ callback(HINTERNET request, DWORD_PTR context, DWORD status, LPVOID statusInform if (!WinHttpQueryDataAvailable(request, NULL)) { res->code = naettProtocolError; - res->complete = 1; + naettSetComplete(res); } } break; @@ -169,14 +169,14 @@ callback(HINTERNET request, DWORD_PTR context, DWORD status, LPVOID statusInform DWORD* available = (DWORD*)statusInformation; res->bytesLeft = *available; if (res->bytesLeft == 0) { - res->complete = 1; + naettSetComplete(res); break; } size_t bytesToRead = min(res->bytesLeft, sizeof(res->buffer)); if (!WinHttpReadData(request, res->buffer, (DWORD)bytesToRead, NULL)) { res->code = naettReadError; - res->complete = 1; + naettSetComplete(res); } } break; @@ -193,7 +193,7 @@ callback(HINTERNET request, DWORD_PTR context, DWORD status, LPVOID statusInform // which both kept the transfer running and left WinHTTP writing into a response // the caller was by then free to close. res->code = naettReadError; - res->complete = 1; + naettSetComplete(res); break; } res->totalBytesRead += (int)bytesRead; @@ -204,12 +204,12 @@ callback(HINTERNET request, DWORD_PTR context, DWORD status, LPVOID statusInform size_t bytesToRead = min(res->bytesLeft, sizeof(res->buffer)); if (!WinHttpReadData(request, res->buffer, (DWORD)bytesToRead, NULL)) { res->code = naettReadError; - res->complete = 1; + naettSetComplete(res); } } else { if (!WinHttpQueryDataAvailable(request, NULL)) { res->code = naettProtocolError; - res->complete = 1; + naettSetComplete(res); } } } break; @@ -223,7 +223,7 @@ callback(HINTERNET request, DWORD_PTR context, DWORD status, LPVOID statusInform } else { if (!WinHttpReceiveResponse(request, NULL)) { res->code = naettReadError; - res->complete = 1; + naettSetComplete(res); } } } break; @@ -247,7 +247,7 @@ callback(HINTERNET request, DWORD_PTR context, DWORD status, LPVOID statusInform res->code = naettGenericError; } - res->complete = 1; + naettSetComplete(res); } break; } } @@ -350,7 +350,7 @@ void naettPlatformMakeRequest(InternalResponse* res) { if (!WinHttpSendRequest(req->request, extraHeaders, -1, NULL, 0, 0, (DWORD_PTR)res)) { res->code = naettConnectionError; - res->complete = 1; + naettSetComplete(res); } }