Skip to content

Commit 1853f90

Browse files
committed
internal/graphicsdriver/metal: add an early return to prevent deadlock
Updates #3353
1 parent 3c95370 commit 1853f90

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

internal/graphicsdriver/metal/displaylink_macos.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ package metal
4040
// int ebitengine_DisplayLinkOutputCallback(CVDisplayLinkRef displayLinkRef, CVTimeStamp* inNow, CVTimeStamp* inOutputTime, uint64_t flagsIn, uint64_t* flagsOut, void* displayLinkContext);
4141
import "C"
4242
import (
43+
"log/slog"
4344
"runtime"
4445
"runtime/cgo"
4546
"time"
@@ -80,6 +81,13 @@ func (v *view) initCAMetalDisplayLink() error {
8081
{
8182
Cmd: objc.RegisterName("metalDisplayLink:needsUpdate:"),
8283
Fn: func(id objc.ID, cmd objc.SEL, metalDisplayLink objc.ID, needsUpdate objc.ID) {
84+
// There is a case where this callback is invoked from the main run loop (#3353).
85+
// This is very mysterious, but this causes a deadlock.
86+
// As a workaround, return this immediately when the current run loop is the main run loop.
87+
if cocoa.NSRunLoop_currentRunLoop() == cocoa.NSRunLoop_mainRunLoop() {
88+
slog.Debug("metal: metalDisplayLink:needsUpdate: is unexpectedly called from the main run loop")
89+
return
90+
}
8391
drawable := ca.MetalDisplayLinkUpdate{ID: needsUpdate}.Drawable()
8492
if drawable == (ca.MetalDrawable{}) {
8593
return

0 commit comments

Comments
 (0)