emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.89k stars 3.32k forks source link

Websocket proxy crash #16485

Open engineer1109 opened 2 years ago

engineer1109 commented 2 years ago

Please include the following in your bug report:

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.7 (48a16209b1a0de5efd8112ce6430415730008d18) clang version 15.0.0 (https://github.com/llvm/llvm-project fbce4a78035c32792b0a13cf1f169048b822c06b) Target: wasm32-unknown-emscripten Thread model: posix InstalledDir: /home/wjl/github/emsdk/upstream/bin

Failing command line in full:

./websocket_to_posix_proxy 8080 Full link command and output with -v appended:

It will make crashes.

terminate called after throwing an instance of 'std::system_error' what(): Resource temporarily unavailable

engineer1109 commented 2 years ago

ProcessWebSocketMessageAsynchronouslyInBackgroundThread create many threads seems not recycled. I add pthread_detach(thread); seems to fix.

void ProcessWebSocketMessageAsynchronouslyInBackgroundThread(int client_fd, uint8_t *payload, uint64_t numBytes)
{
  MessageArg *arg = (MessageArg*)malloc(sizeof(MessageArg));
  arg->client_fd = client_fd;
  arg->payload = (uint8_t*)memdup(payload, (size_t)numBytes);
  arg->numBytes = numBytes;
  THREAD_T thread;
  // TODO: Instead of unconditionally always creating a thread here, create a thread pool and push messages to it.
  // (leaving this as a future optimization because not sure if it matters here much at all for performance)
    auto res = CREATE_THREAD(thread, message_processing_thread, arg);
    if(!CREATE_THREAD_SUCCEEDED(res)) {
        printf("res err %d\n", res);
    }
#if __unix__
    pthread_detach(thread);
#endif
    printf("g_totalThreads %d\n", g_totalThreads);
}
sbc100 commented 2 years ago

Would like like to send a PR to fix that? Perhaps the detach should be part of the CREATE_THREAD macro, and it should be implemented for win32 too?

Also, I wonder if we can/should consider updating this code to use C11 threads to avoid the need for maintaining both windows and unix threading code here?

engineer1109 commented 2 years ago

Would like like to send a PR to fix that? Perhaps the detach should be part of the CREATE_THREAD macro, and it should be implemented for win32 too?

Also, I wonder if we can/should consider updating this code to use C11 threads to avoid the need for maintaining both windows and unix threading code here?

I will pull request tomorrow. It seems exit cannot recycle the threads.

Moreover, this code and emrun also needs ssl due to the update of chrome 91. Posix threads must need ssl now.

engineer1109 commented 2 years ago

h

Would like like to send a PR to fix that? Perhaps the detach should be part of the CREATE_THREAD macro, and it should be implemented for win32 too?

Also, I wonder if we can/should consider updating this code to use C11 threads to avoid the need for maintaining both windows and unix threading code here?

Not only threads, socket can be used by asio (no boost).

sbc100 commented 2 years ago

itsn't problem here that the threads are not be cleanuped up (i.e. not detached or joined?)