Skip to content

std::promise::get() hangs the main thread #7388

Closed

Description

@VirtualTim

std::promise::get() often uses 100% cpu, starving the thread it is waiting for. This happens sporadically with std::thread's, but consistently with emscripten_async_wget2_data. The behaviour appears the same in WASM/ASM.js. This affects both 1.38.13 and 1.38.14 (Edit: still an issue in 1.38.26).

Example Code:

#include <iostream>
#include <future>
#include "emscripten.h"

std::promise<bool> isComplete;
std::string str;

void callback_onLoad(unsigned handle, void* arg, void* pBuffer, unsigned pBufferSize) {
	str = "test string\n";
	isComplete.set_value(true);
}

int main(int argc, char** argv) {
	std::future<bool> callback_finished = isComplete.get_future();
	
	emscripten_async_wget2_data(
		"https://emscripten.org/_static/Emscripten_logo_full.png",
		"GET", "",
		nullptr,
		1,
		&callback_onLoad,
		nullptr,
		nullptr
	);
	
	if (callback_finished.get())	//<-- Hang here, mutex never unlocks, 100% cpu
		std::cout << str << std::endl;
	
	return EXIT_SUCCESS;
}

Compile with:
emcc promise.cpp -std=c++11 -o promise.html --emrun

The png gets downloaded, but 'callback_onLoad' is never called.


EDIT: Better example

#include <iostream>
#include <future>
#include <thread>

std::promise<bool> isComplete;
std::string str;

void test_function() {
	str = "test string";
	isComplete.set_value(true);
}

int main(int argc, char** argv) {
	std::future<bool> callback_finished = isComplete.get_future();
	
	std::thread(test_function).detach();
	
	if (callback_finished.get())	//<-- Hang here, mutex never unlocks, 100% cpu
		std::cout << str << std::endl;
	
	return EXIT_SUCCESS;
}

Compile with:
emcc promise.cpp -std=c++11 -o promise.html --emrun -s USE_PTHREADS=1

Activity

  1. VirtualTim commented on Oct 25, 2018

    @VirtualTim
    CollaboratorAuthor

    Possibly related to #7261?

  2. kripken commented on Oct 25, 2018

    @kripken
    Member

    I think you'd actually need to run the operation on another thread for this to work, but I'm not sure.

  3. VirtualTim commented on Oct 26, 2018

    @VirtualTim
    CollaboratorAuthor

    So I should be calling 'emscripten_async_wget2_data(...)' like 'std::thread(emscripten_async_wget2_data, ...)'? I kind of thought that this was all handled internally by that function. I'll give that a shot and see how that goes.

    Edit:
    Calling 'emscripten_async_wget2_data(...)' like 'std::thread(emscripten_async_wget2_data, ...).detach()' doesn't seem to change anything.

    Looking at the JS callstack it seems to be stuck in 'emscripten_futex_wait', which seems weird since JS natively has promises/futures. I would have thought that a std::promise would just get converted into a JS promise.

  4. VirtualTim commented on Mar 6, 2019

    @VirtualTim
    CollaboratorAuthor

    Looking back at this with more experience, I'm pretty sure that emscripten_async_wget2_data is trying to execute on the main thread, but since the main thread is waiting on a promise, it never gets executed.

    I'd still consider this a bug, as waiting on a promise should still execute queued calls on the main thread. There's other cases in Emscripten where sleeping the main thread really sleeps for a bit, waked up to check the event queue (emscripten_main_thread_process_queued_calls), then goes back to sleeping. Eg timedwait here.
    Edit: _emscripten_futex_wait does call emscripten_main_thread_process_queued_calls, so I'm not really sure why that's not working.

  5. stale commented on Mar 10, 2020

    @stale

    This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions