From c582e52c15da9a92af684641770b13b918cd072e Mon Sep 17 00:00:00 2001 From: realrasengan Date: Mon, 30 Mar 2026 18:53:48 -0700 Subject: [PATCH] WASM: replace goroutine recv with JS-driven poll() to prevent scheduler starvation --- cmd/mosh-wasm/main.go | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/cmd/mosh-wasm/main.go b/cmd/mosh-wasm/main.go index 708f1e6..5a33371 100644 --- a/cmd/mosh-wasm/main.go +++ b/cmd/mosh-wasm/main.go @@ -7,7 +7,6 @@ import ( "fmt" "sync" "syscall/js" - "time" mosh "github.com/unixshells/mosh-go" ) @@ -119,28 +118,15 @@ func (s *session) jsObject() js.Value { return nil })) - // recv(callback) — calls callback(string) whenever output is available - obj.Set("recv", js.FuncOf(func(this js.Value, args []js.Value) interface{} { - if len(args) < 1 { - return nil + // poll() — check for output. Called by JS setInterval. + // Returns string if output available, null otherwise. + // Using JS-driven polling avoids Go goroutine scheduler starvation. + obj.Set("poll", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + out := s.client.Recv(0) + if out == nil { + return js.Null() } - cb := args[0] - go func() { - for { - out := s.client.Recv(60 * time.Second) - if out == nil { - s.mu.Lock() - closed := s.closed - s.mu.Unlock() - if closed { - return - } - continue - } - cb.Invoke(string(out)) - } - }() - return nil + return string(out) })) obj.Set("close", js.FuncOf(func(this js.Value, args []js.Value) interface{} {