Skip to content

Commit 5625f68

Browse files
aykevldeadprogram
authored andcommitted
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).
1 parent f7d8502 commit 5625f68

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/runtime/scheduler_cores.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,16 @@ func systemStackPtr() *uintptr {
303303
const cpuColoredPrint = false
304304

305305
func printlock() {
306-
printLock.Lock()
306+
// Don't lock the print output inside an interrupt.
307+
// Locking the print output inside an interrupt can lead to a deadlock: if
308+
// the interrupt happens while the print lock is held, the interrupt won't
309+
// be able to take this lock anymore.
310+
// This isn't great, but the alternative would be to disable interrupts
311+
// while printing which seems like a worse idea to me.
312+
if !interrupt.In() {
313+
printLock.Lock()
314+
}
315+
307316
if cpuColoredPrint {
308317
switch currentCPU() {
309318
case 1:
@@ -322,5 +331,8 @@ func printunlock() {
322331
printstring("\x1b[0m") // reset colored output
323332
}
324333
}
325-
printLock.Unlock()
334+
335+
if !interrupt.In() {
336+
printLock.Unlock()
337+
}
326338
}

0 commit comments

Comments
 (0)