From 13529cc5f443ef4e242da3716daa0032aa8d34f2 Mon Sep 17 00:00:00 2001 From: Chris O'Hara Date: Thu, 25 May 2023 12:07:48 +1000 Subject: syscall: try non-blocking stdio on wasip1 Try to set stdio to non-blocking mode before the os package calls NewFile for each fd. NewFile queries the non-blocking flag but doesn't change it, even if the runtime supports non-blocking stdio. Since WebAssembly modules are single-threaded, blocking system calls temporarily halt execution of the module. If the runtime supports non-blocking stdio, the Go runtime is able to use the WASI net poller to poll for read/write readiness and is able to schedule goroutines while waiting. Change-Id: I1e3ce68a414e3c5960ce6a27fbfd38556e59c3dc Reviewed-on: https://go-review.googlesource.com/c/go/+/498196 Reviewed-by: Johan Brandhorst-Satzkorn Auto-Submit: Johan Brandhorst-Satzkorn Reviewed-by: Achille Roussel Reviewed-by: Heschi Kreinick Run-TryBot: Johan Brandhorst-Satzkorn TryBot-Result: Gopher Robot Reviewed-by: Dmitri Shuralyov Reviewed-by: Ian Lance Taylor --- src/syscall/fs_wasip1.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/syscall/fs_wasip1.go b/src/syscall/fs_wasip1.go index d60ab0b53e..4ad3f9610b 100644 --- a/src/syscall/fs_wasip1.go +++ b/src/syscall/fs_wasip1.go @@ -11,6 +11,20 @@ import ( "unsafe" ) +func init() { + // Try to set stdio to non-blocking mode before the os package + // calls NewFile for each fd. NewFile queries the non-blocking flag + // but doesn't change it, even if the runtime supports non-blocking + // stdio. Since WebAssembly modules are single-threaded, blocking + // system calls temporarily halt execution of the module. If the + // runtime supports non-blocking stdio, the Go runtime is able to + // use the WASI net poller to poll for read/write readiness and is + // able to schedule goroutines while waiting. + SetNonblock(0, true) + SetNonblock(1, true) + SetNonblock(2, true) +} + type uintptr32 = uint32 type size = uint32 type fdflags = uint32 -- cgit v1.2.3-54-g00ecf