Skip to content

select will timeout even if timeout is set to NULL #16704

@versaloon

Description

@versaloon

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 (3ff7eff)
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
......

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions