From e538349910266e75c4e098a4826a8fe0eb961bb8 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sun, 1 Dec 2024 13:49:46 +0100 Subject: [PATCH] runtime: use exitGoroutine instead of deadlock This makes the meaning more clear. And while the behavior is currently the same, when we start to use threads we should actually exit the goroutine when calling runtime.Goexit() instead of just leaving it deadlocked. --- src/runtime/panic.go | 4 ++-- src/runtime/scheduler_cooperative.go | 13 +++++++++++-- src/runtime/scheduler_none.go | 6 ++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/runtime/panic.go b/src/runtime/panic.go index ec33a4469c..e99fbcbb8f 100644 --- a/src/runtime/panic.go +++ b/src/runtime/panic.go @@ -71,9 +71,9 @@ func panicOrGoexit(message interface{}, panicking panicState) { } } if panicking == panicGoexit { - // Call to Goexit() instead of a panic. + // This is a call to Goexit() instead of a panic. // Exit the goroutine instead of printing a panic message. - deadlock() + exitGoroutine() } printstring("panic: ") printitf(message) diff --git a/src/runtime/scheduler_cooperative.go b/src/runtime/scheduler_cooperative.go index 5c4dfd5bf5..9c1054ddd4 100644 --- a/src/runtime/scheduler_cooperative.go +++ b/src/runtime/scheduler_cooperative.go @@ -37,9 +37,18 @@ var ( // //go:noinline func deadlock() { - // call yield without requesting a wakeup + exitGoroutine() +} + +func exitGoroutine() { + // Pause the goroutine without a way for it to wake up. + // This makes the goroutine unreachable, and should eventually get it + // cleaned up by the GC. task.Pause() - panic("unreachable") + + // We will never return from task.Pause(). Make sure the compiler knows + // this. + trap() } // Add this task to the end of the run queue. diff --git a/src/runtime/scheduler_none.go b/src/runtime/scheduler_none.go index 7775b360eb..b03d572a9f 100644 --- a/src/runtime/scheduler_none.go +++ b/src/runtime/scheduler_none.go @@ -29,6 +29,12 @@ func deadlock() { runtimePanic("all goroutines are asleep - deadlock!") } +func exitGoroutine() { + // There is only one goroutine, which would exit, so that leads to a + // deadlock. + deadlock() +} + func scheduleTask(t *task.Task) { // Pause() will panic, so this should not be reachable. }