You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to this "Any allocated resources will be freed once the thread exits." However when a detached thread exits it doesn't get disposed. So not only does that leak memory, it makes maintaining a thread pool not really work.
Example code (swap 100 with something like 10 it you don't want to go out of memory):
With the hardocded 100 threads I go out of memory at about 20. Changing the hardcoded value to 10 doesn't go OOM, but the workers still never get freed.
Is this a browser OOM? We don't destroy workers, I believe, so that would make sense. If we destroyed them, we'd have issues with their creation (it's async, that's why we have the compile option to spin them up beforehand). So not sure we can do much better here. At minimum we should document this better though.
Not browser OOM, the program just reaches the TOTAL_MEMORY limit. I don't think pthreads work with ALLOW_MEMORY_GROWTH, but each worker takes a decent amount of memory, so if this is ever supported there will be issues, I'd imagine.
Edit: actual error: Cannot enlarge memory arrays to size 52428800 bytes (OOM). Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value 52428800, (2) compile with -s ALLOW_MEMORY_GROWTH=1 which allows increasing the size at runtime, or (3) if you want malloc to return NULL (0) instead of this abort, compile with -s ABORTING_MALLOC=0
I would have thought that if a detached thread gets disposed, either the webworker would get disposed, or it would just go back into the thread pool. As it is worker just keep getting created until
TOTAL_MEMORY is reached.
For what I was currently working on, I was registering a click callback on the canvas element, and on click create a thread that performs some lengthy synchronous operation (related to the fun I was having with #8117). Obviously joining the thread would block the main thread (or in my case create a deadlock). However if every click creates a web worker that never gets disposed the application is going to quickly run out of memory.
If we destroyed them, we'd have issues with their creation
I see. Yeah, we intend to have memory growth working with pthreads, but this is not yet ready.
If we destroyed them, we'd have issues with their creation
I meant the same issues at startup - when you do pthread_create, if we need to spin up a worker, that's an async operation, so it must return to the main browser event loop for the thread to actually be created. To work around that, we can preload some workers at startup, and then pthread_create just activates an existing worker, which we can do synchronously.
Would there be a way to return detached threads to the thread pool when they finish?
So for example you could call:
std::thread(some_function).detach(); //running on thread 1
sleep(5000) //give the thread time to finish (pretend this doesn't block the main thread)
//thread 1 is finished and returned to the pool
std::thread(another_function).detach(); //another_function gets run on thread 1
I believe that's how things currently work, yes, there is a pool that is reused. (Assuming .detach() in fact uses pthread_exit or pthread_kill or such to halt the child thread?)
Hm, that doesn't seem to be the case in my testing. I'm guessing the example I created above creates threads too fast, but in another project I'm creating a thread per mouse click (using a callback attached to the canvas) and it seems like that created additional threads. I may try making another example to better show this.
For debugging this, I think the key location is unusedWorkerPool in src/library_pthread.js.
Looks like the cancelDone event should lead to a worker being cleaned up and re-added to that list. Adding some debug prints may help see why it's not happening in your case.
Thanks for the pointer. I looks like the d.cmd === 'exit' is a noop?
When I changed } else if (d.cmd === 'cancelDone') { to } else if (d.cmd === 'cancelDone' || d.cmd === 'exit') {, and commented out the line } else if (d.cmd === 'exit') { I get the expected behaviour.
Are there any obvious issues you see here? Should I create a PR and see if the tests pass?
The initial example I posted still creates threads too fast, and threads aren't removed from the pool (which seems reasonable). However an example like this behaves as expected, with detached threads being returned to the pool.
I'm seeing the same issue using std::async - an event being triggered launches a background thread (if one isn't already being run) - what I see in emscripten compiled code is that a new thread appears each time std::async is called, which, as above rapidly causes memory to run out - there will only ever be one async-launched thread running at a time.
std::async documentation suggests that the implementation likely uses a threadpool under the hood, but there is no user facing configuration for this in the specification
@VirtualTim - yeah, I'd suggest opening a PR. I'm not familiar enough with that code to say offhand. Can discuss in a PR and hopefully other people can chime in there too.
Activity
kripken commentedon Feb 27, 2019
Is this a browser OOM? We don't destroy workers, I believe, so that would make sense. If we destroyed them, we'd have issues with their creation (it's async, that's why we have the compile option to spin them up beforehand). So not sure we can do much better here. At minimum we should document this better though.
VirtualTim commentedon Feb 28, 2019
Not browser OOM, the program just reaches the TOTAL_MEMORY limit. I don't think pthreads work with ALLOW_MEMORY_GROWTH, but each worker takes a decent amount of memory, so if this is ever supported there will be issues, I'd imagine.
Edit: actual error:
Cannot enlarge memory arrays to size 52428800 bytes (OOM). Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value 52428800, (2) compile with -s ALLOW_MEMORY_GROWTH=1 which allows increasing the size at runtime, or (3) if you want malloc to return NULL (0) instead of this abort, compile with -s ABORTING_MALLOC=0I would have thought that if a detached thread gets disposed, either the webworker would get disposed, or it would just go back into the thread pool. As it is worker just keep getting created until
TOTAL_MEMORY is reached.
For what I was currently working on, I was registering a click callback on the canvas element, and on click create a thread that performs some lengthy synchronous operation (related to the fun I was having with #8117). Obviously joining the thread would block the main thread (or in my case create a deadlock). However if every click creates a web worker that never gets disposed the application is going to quickly run out of memory.
Can you explain further what the issue is here?
kripken commentedon Feb 28, 2019
I see. Yeah, we intend to have memory growth working with pthreads, but this is not yet ready.
I meant the same issues at startup - when you do pthread_create, if we need to spin up a worker, that's an async operation, so it must return to the main browser event loop for the thread to actually be created. To work around that, we can preload some workers at startup, and then pthread_create just activates an existing worker, which we can do synchronously.
VirtualTim commentedon Mar 1, 2019
Would there be a way to return detached threads to the thread pool when they finish?
So for example you could call:
and both functions could run on the same thread?
kripken commentedon Mar 4, 2019
I believe that's how things currently work, yes, there is a pool that is reused. (Assuming
.detach()in fact usespthread_exitorpthread_killor such to halt the child thread?)VirtualTim commentedon Mar 5, 2019
Hm, that doesn't seem to be the case in my testing. I'm guessing the example I created above creates threads too fast, but in another project I'm creating a thread per mouse click (using a callback attached to the canvas) and it seems like that created additional threads. I may try making another example to better show this.
kripken commentedon Mar 7, 2019
For debugging this, I think the key location is
unusedWorkerPoolinsrc/library_pthread.js.Looks like the
cancelDoneevent should lead to a worker being cleaned up and re-added to that list. Adding some debug prints may help see why it's not happening in your case.VirtualTim commentedon Mar 7, 2019
Thanks for the pointer. I looks like the
d.cmd === 'exit'is a noop?When I changed
} else if (d.cmd === 'cancelDone') {to} else if (d.cmd === 'cancelDone' || d.cmd === 'exit') {, and commented out the line} else if (d.cmd === 'exit') {I get the expected behaviour.Are there any obvious issues you see here? Should I create a PR and see if the tests pass?
The initial example I posted still creates threads too fast, and threads aren't removed from the pool (which seems reasonable). However an example like this behaves as expected, with detached threads being returned to the pool.
benjymous commentedon Mar 8, 2019
I'm seeing the same issue using std::async - an event being triggered launches a background thread (if one isn't already being run) - what I see in emscripten compiled code is that a new thread appears each time std::async is called, which, as above rapidly causes memory to run out - there will only ever be one async-launched thread running at a time.
std::async documentation suggests that the implementation likely uses a threadpool under the hood, but there is no user facing configuration for this in the specification
VirtualTim commentedon Mar 11, 2019
Does the fix I mentioned above also fix your std::async issue?
kripken commentedon Mar 11, 2019
@VirtualTim - yeah, I'd suggest opening a PR. I'm not familiar enough with that code to say offhand. Can discuss in a PR and hopefully other people can chime in there too.
Return detached threads to the pool (#8286)
Return detached threads to the pool (emscripten-core#8286)
Return detached threads to the pool (emscripten-core#8286)