Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func TestBuild(t *testing.T) {
"print.go",
"reflect.go",
"signal.go",
"signalnotify.go",
"slice.go",
"sort.go",
"stdlib.go",
Expand Down
10 changes: 9 additions & 1 deletion src/runtime/signalstub.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ func signal_ignore(uint32) {}
func signal_waitUntilIdle() {}

//go:linkname signal_recv os/signal.signal_recv
func signal_recv() uint32 { return ^uint32(0) }
func signal_recv() uint32 {
// Signals can never arrive on these platforms, so block forever, like the
// real implementation does while no signal is pending. os/signal.loop
// calls this function in a tight loop; returning immediately would make
// that goroutine spin, which starves cooperative schedulers: on wasm the
// program never yields back to the host again after signal.Notify.
deadlock()
return 0
}
22 changes: 22 additions & 0 deletions testdata/signalnotify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

// Verify that signal.Notify does not wedge the scheduler on platforms where
// signals can never arrive (wasm and baremetal, see runtime/signalstub.go).
// The signal watcher goroutine that Notify starts must block forever instead
// of spinning: on a cooperative scheduler a spinning goroutine starves every
// other goroutine, so the sleep below would never return.

import (
"os"
"os/signal"
"time"
)

func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
// Yield so the signal watcher goroutine runs (and, before the fix, takes
// over the scheduler forever).
time.Sleep(10 * time.Millisecond)
println("done")
}
1 change: 1 addition & 0 deletions testdata/signalnotify.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
done
Loading