Skip to content

Commit 8f10557

Browse files
committed
Fix fd_sync under pthreads
This function is marked as `__async: 'auto'` which means it should return a promise when run in proxied mode on the main thread. However it wasn't always returning a promise. Adding the `async` keyword is the simplest way to ensure it always returns a promise. Also, update the `wrapSyscallFunction` function to preserve the async keyword. Replaces: #26209
1 parent 93dcd26 commit 8f10557

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

src/lib/libcore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2605,7 +2605,7 @@ function wrapSyscallFunction(x, library, isWasi) {
26052605
post = handler + post;
26062606

26072607
if (pre || post) {
2608-
t = modifyJSFunction(t, (args, body) => `function (${args}) {\n${pre}${body}${post}}\n`);
2608+
t = modifyJSFunction(t, (args, body, async_) => `${async_}function (${args}) {\n${pre}${body}${post}}\n`);
26092609
}
26102610

26112611
library[x] = eval('(' + t + ')');

src/lib/libwasi.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -540,27 +540,32 @@ var WasiLibrary = {
540540
return 0;
541541
},
542542

543+
#if SYSCALLS_REQUIRE_FILESYSTEM
543544
fd_sync__async: 'auto',
544545
fd_sync: (fd) => {
545-
#if SYSCALLS_REQUIRE_FILESYSTEM
546546
var stream = SYSCALLS.getStreamFromFD(fd);
547547
var rtn = stream.stream_ops?.fsync?.(stream);
548-
#if ASYNCIFY
549-
var mount = stream.node.mount;
550-
if (mount.type.syncfs) {
551-
return new Promise((resolve) => {
548+
#if ASYNCIFY || PTHREADS
549+
return new Promise((resolve) => {
550+
var mount = stream.node.mount;
551+
if (mount && mount.type.syncfs) {
552552
mount.type.syncfs(mount, false, (err) => resolve(err ? {{{ cDefs.EIO }}} : 0));
553-
});
554-
}
555-
#endif // ASYNCIFY
553+
} else {
554+
resolve(rtn);
555+
}
556+
});
557+
#else
556558
return rtn;
559+
#endif // ASYNCIFY || PTHREADS
560+
},
557561
#else // SYSCALLS_REQUIRE_FILESYSTEM
562+
fd_sync: (fd) => {
558563
#if ASSERTIONS
559564
abort('fd_sync called without SYSCALLS_REQUIRE_FILESYSTEM');
560565
#endif
561566
return {{{ cDefs.ENOSYS }}};
562-
#endif // SYSCALLS_REQUIRE_FILESYSTEM
563567
},
568+
#endif // SYSCALLS_REQUIRE_FILESYSTEM
564569

565570
// random.h
566571

test/fs/test_memfs_fsync.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ int main() {
2323
struct stat st;
2424

2525
// a file whose contents are just 'az'
26-
rtn = stat("/wakaka.txt", &st);
26+
rtn = stat("wakaka.txt", &st);
2727
assert(rtn == -1 && errno == ENOENT);
2828

29-
fd = open("/wakaka.txt", O_RDWR | O_CREAT, 0666);
29+
fd = open("wakaka.txt", O_RDWR | O_CREAT, 0666);
3030
assert(fd >= 0);
3131

3232
rtn = write(fd, "az", 2);
@@ -38,5 +38,6 @@ int main() {
3838
rtn = close(fd);
3939
assert(rtn == 0);
4040

41+
printf("success\n");
4142
return 0;
4243
}

test/test_browser.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,9 +1325,7 @@ def test_fs_idbfs_fsync(self):
13251325
self.btest('fs/test_idbfs_fsync.c', '1', cflags=args + [f'-DSECRET="{secret}"', '-lidbfs.js'])
13261326

13271327
def test_fs_memfs_fsync(self):
1328-
args = ['-sASYNCIFY', '-sEXIT_RUNTIME']
1329-
secret = str(time.time())
1330-
self.btest_exit('fs/test_memfs_fsync.c', cflags=args + [f'-DSECRET="{secret}"'])
1328+
self.btest_exit('fs/test_memfs_fsync.c', cflags=['-sASYNCIFY', '-sEXIT_RUNTIME'])
13311329

13321330
def test_fs_workerfs(self):
13331331
create_file('pre.js', '''

test/test_other.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
also_with_asan,
5959
also_with_minimal_runtime,
6060
also_with_modularize,
61+
also_with_nodefs_both,
6162
also_with_noderawfs,
6263
also_with_standalone_wasm,
6364
also_with_wasm2js,
@@ -5525,7 +5526,16 @@ def test_dashM_respect_dashO(self):
55255526
@with_all_fs
55265527
@crossplatform
55275528
def test_fs_bad_lookup(self):
5528-
self.do_runf(path_from_root('test/fs/test_fs_bad_lookup.c'), expected_output='ok')
5529+
self.do_runf('fs/test_fs_bad_lookup.c', 'ok')
5530+
5531+
@also_with_nodefs_both
5532+
@crossplatform
5533+
@parameterized({
5534+
'': ([],),
5535+
'pthreads': (['-pthread', '-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME'],),
5536+
})
5537+
def test_fsync(self, args):
5538+
self.do_runf('fs/test_memfs_fsync.c', 'success', cflags=args)
55295539

55305540
@with_all_fs
55315541
@crossplatform

0 commit comments

Comments
 (0)