From 5625f68d513874c5c870892c9675de972af96b1f Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 12 Jun 2025 13:58:53 +0200 Subject: [PATCH] runtime: don't lock the print output inside interrupts This should avoid a deadlock when trying to print inside an interrupt, if the interrupted code is also printing (and therefore has the print lock taken). --- src/runtime/scheduler_cores.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/runtime/scheduler_cores.go b/src/runtime/scheduler_cores.go index 698596a71..c2736cafd 100644 --- a/src/runtime/scheduler_cores.go +++ b/src/runtime/scheduler_cores.go @@ -303,7 +303,16 @@ func systemStackPtr() *uintptr { const cpuColoredPrint = false func printlock() { - printLock.Lock() + // Don't lock the print output inside an interrupt. + // Locking the print output inside an interrupt can lead to a deadlock: if + // the interrupt happens while the print lock is held, the interrupt won't + // be able to take this lock anymore. + // This isn't great, but the alternative would be to disable interrupts + // while printing which seems like a worse idea to me. + if !interrupt.In() { + printLock.Lock() + } + if cpuColoredPrint { switch currentCPU() { case 1: @@ -322,5 +331,8 @@ func printunlock() { printstring("\x1b[0m") // reset colored output } } - printLock.Unlock() + + if !interrupt.In() { + printLock.Unlock() + } }