wasm: Proxy emscripten_fetch() to the main thread

Calling emscripten_fetch() on worker thread which never yields control
back to the browser, will leave the fetch request pending forever.
This can be a problematic for example in QML Loader, which tries
to load resource by network.
Proxy this function call to the main thread, so it can be processed by
the browser.

Fixes: QTBUG-118225
Pick-to: 6.6 6.5
Change-Id: I969d73f6a66670c4135960e08d2eedc8d2a6e5c3
Reviewed-by: Lorn Potter <lorn.potter@gmail.com>
This commit is contained in:
Piotr Wierciński 2023-11-17 16:16:38 +01:00
parent a397247e2b
commit f2f2b6ef18
2 changed files with 6 additions and 2 deletions

View File

@ -53,6 +53,7 @@ public:
void interrupt() override;
void wakeUp() override;
static void runOnMainThreadAsync(std::function<void(void)> fn);
static void socketSelect(int timeout, int socket, bool waitForRead, bool waitForWrite,
bool *selectForRead, bool *selectForWrite, bool *socketDisconnect);
protected:
@ -90,7 +91,6 @@ private:
static void run(std::function<void(void)> fn);
static void runOnMainThread(std::function<void(void)> fn);
static void runAsync(std::function<void(void)> fn);
static void runOnMainThreadAsync(std::function<void(void)> fn);
static QEventDispatcherWasm *g_mainThreadEventDispatcher;

View File

@ -9,6 +9,7 @@
#include <QtCore/qcoreapplication.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qthread.h>
#include <QtCore/private/qeventdispatcher_wasm_p.h>
#include <QtCore/private/qoffsetstringarray_p.h>
#include <QtCore/private/qtools_p.h>
@ -295,7 +296,10 @@ void QNetworkReplyWasmImplPrivate::doSendRequest()
QByteArray destinationPath = dPath.toUtf8();
attr.destinationPath = destinationPath.constData();
m_fetch = emscripten_fetch(&attr, request.url().toString().toUtf8());
auto url = request.url().toString().toUtf8();
QEventDispatcherWasm::runOnMainThreadAsync([attr, url]() mutable {
emscripten_fetch(&attr, url);
});
state = Working;
}