emscripten
emscripten copied to clipboard
select will timeout even if timeout is set to NULL
Please include the following in your bug report:
Version of emscripten/emsdk: emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.8 (3ff7eff373d31d8c0179895165462019221f192e) clang version 15.0.0 (https://github.com/llvm/llvm-project 80ec0ebfdc5692a58e0832125f2c6a991df9d63f) Target: wasm32-unknown-emscripten Thread model: posix InstalledDir: C:\Users\simon\Desktop\software\emsdk\upstream\bin
Failing command line in full: compiling and linking are good. emcc -pthread -sPTHREAD_POOL_SIZE=2 test.c node --experimental-wasm-threads --experimental-wasm-bulk-memory .\a.out.js
Source code
#include <unistd.h>
#include <pthread.h>
#include <sys/select.h>
#include <stdio.h>
int pipefd[2];
static void * __select_thread(void *arg)
{
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(pipefd[0], &rfds);
int ret;
char ch;
while (1) {
ret = select(pipefd[0] + 1, &rfds, NULL, NULL, NULL);
printf("select returned %d\n", ret);
if (0 == ret) {
FD_SET(pipefd[0], &rfds);
} else {
read(pipefd[0], &ch, 1);
}
}
return NULL;
}
int main(void)
{
pipe(pipefd);
char ch = 0;
write(pipefd[1], &ch, 1);
pthread_t thread;
pthread_create(&thread, NULL, __select_thread, NULL);
pthread_join(thread, NULL);
printf("exit\n");
return 0;
}
Desired output On Ubuntu, the output is: simonqian@simonqian-VirtualBox:~$ gcc test.c -lpthread simonqian@simonqian-VirtualBox:~$ ./a.out select returned 1
On nodejs, the output is: PS Z:> emcc -pthread -sPTHREAD_POOL_SIZE=2 test.c PS Z:> node --experimental-wasm-threads --experimental-wasm-bulk-memory .\a.out.js select returned 1 select returned 0 select returned 0 select returned 0 select returned 0 select returned 0 ......
I think this is due to the fact that we can't bloc the main thread when running the browser and so our select implementation is basically just falls back to poll:
https://github.com/emscripten-core/emscripten/blob/e1d97f55d12ed63dc30e547c818af9a63be0b976/src/library_syscall.js#L591-L595
Perhaps we should at least assert/warn when we return even though a timeout was requested?
Or perhaps we can actually block in some environments? If you would like to contribute here any patches would be welcome.
I've posted a PR related to this issue https://github.com/emscripten-core/emscripten/pull/25523. @sbc100 Could you please take a look at that PR?