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) <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-10-01 10:26:00 -06:00
1 parent f87179d0c1
commit 8e92fef527
7 files changed
+44 -19

No files matched your search

+4 -1
View File
@@ -1,5 +1,6 @@
#pragma once
#include <atomic>
#include <functional>
#include <memory>
#include <thread>
@@ -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<bool> completed_{false};
bool failed_ = false;
net::ResolveFunc customResolve_;
};
+2 -2
View File
@@ -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");
}
+1 -1
View File
@@ -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) {
+22
View File
@@ -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);
+4 -4
View File
@@ -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);
}
}
+2 -2
View File
@@ -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);
}
}
+9 -9
View File
@@ -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);
}
}