Skip to content

Commit 14bc5a7

Browse files
authored
[WasmFS] Suppress Node.js warnings for unmanaged FDs (#25413)
In WasmFS, calling `close()` on a file descriptor from a thread that did not open it can trigger harmless Node.js warnings. To prevent this, set `trackUnmanagedFds` to `false` for pthread workers when WasmFS is enabled. Resolves: #24731.
1 parent 1fbe205 commit 14bc5a7

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

src/lib/libpthread.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ const pthreadWorkerOptions = `{
5151
// This is the way that we signal to the node worker that it is hosting
5252
// a pthread.
5353
'workerData': 'em-pthread',
54+
#if WASMFS
55+
// In WasmFS, close() is not proxied to the main thread. Suppress
56+
// warnings when a thread closes a file descriptor it didn't open.
57+
// See: https://github.com/emscripten-core/emscripten/issues/24731
58+
'trackUnmanagedFds': false,
59+
#endif
5460
#endif
5561
#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER
5662
// This is the way that we signal to the Web Worker that it is hosting

test/other/test_pthread_fd_close.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <fcntl.h>
2+
#include <pthread.h>
3+
#include <stdio.h>
4+
#include <unistd.h>
5+
6+
void* thread_func(void* arg) {
7+
int* fd = (int*)arg;
8+
9+
printf("thread main\n");
10+
int rc = close(*fd);
11+
printf("close: %d\n", rc);
12+
13+
return NULL;
14+
}
15+
16+
int main() {
17+
printf("main\n");
18+
int fd = open("example.txt", O_RDWR | O_CREAT, 0644);
19+
20+
pthread_t t;
21+
int rc = pthread_create(&t, NULL, thread_func, &fd);
22+
printf("pthread_create: %d\n", rc);
23+
rc = pthread_join(t, NULL);
24+
printf("pthread_join: %d\n", rc);
25+
26+
printf("done\n");
27+
28+
return 0;
29+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
main
2+
pthread_create: 0
3+
thread main
4+
close: 0
5+
pthread_join: 0
6+
done

test/test_other.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13556,6 +13556,13 @@ def test_pthread_icu(self):
1355613556
def test_pthread_set_main_loop(self, args):
1355713557
self.do_other_test('test_pthread_set_main_loop.c', cflags=args)
1355813558

13559+
@node_pthreads
13560+
def test_pthread_fd_close_wasmfs(self):
13561+
create_file('node_warnings', '')
13562+
self.node_args += ['--trace-warnings', '--redirect-warnings=node_warnings']
13563+
self.do_other_test('test_pthread_fd_close.c', cflags=['-sWASMFS', '-sNODERAWFS'])
13564+
self.assertNotContained('closed but not opened in unmanaged mode', read_file('node_warnings'))
13565+
1355913566
# unistd tests
1356013567

1356113568
def test_unistd_confstr(self):

0 commit comments

Comments
 (0)