diff --git a/internal/awake/awake.go b/internal/awake/awake.go index 61688ebc3..566c1427c 100644 --- a/internal/awake/awake.go +++ b/internal/awake/awake.go @@ -114,3 +114,18 @@ func Keep(ctx context.Context, level func() Level, state func() State) { } } } + +// NoNap keeps macOS from App Napping the magpie serving the gateway until +// the release it returns is called (#6261, ChasLui). The menu-bar app +// serves it in its own process, and with the screen locked macOS naps the +// app as one in the background: its priority falls to 4 and its timers +// wait, so the first request after a quiet while spent its 30 seconds +// before routing and got a 504. An agent waiting on the gateway is the +// user's work, so the activity says it is user initiated, as Apple's App +// Nap opt-out does. It lets the system sleep when idle, as it did: holding +// that off is Keep's, and only while the user asked for it. Nor does it +// ask for NSActivityLatencyCritical's timer precision, which is for audio +// and video; a request needs the process run, not run to the millisecond. +func NoNap() func() { + return napOff("magpie serves the gateway agents send their requests to") +} diff --git a/internal/awake/nap_darwin.go b/internal/awake/nap_darwin.go new file mode 100644 index 000000000..af696b68c --- /dev/null +++ b/internal/awake/nap_darwin.go @@ -0,0 +1,44 @@ +//go:build darwin && cgo + +package awake + +/* +#cgo CFLAGS: -x objective-c +#cgo LDFLAGS: -framework Foundation +#import +#include + +// magpie_nap_off begins an activity that keeps App Nap off and lets the +// system sleep when idle; it returns the activity, retained, for +// magpie_nap_on to end. +static void *magpie_nap_off(const char *reason) { + @autoreleasepool { + id a = [[NSProcessInfo processInfo] + beginActivityWithOptions:NSActivityUserInitiatedAllowingIdleSystemSleep + reason:[NSString stringWithUTF8String:reason]]; + return (void *)[a retain]; + } +} + +static void magpie_nap_on(void *a) { + @autoreleasepool { + [[NSProcessInfo processInfo] endActivity:(id)a]; + [(id)a release]; + } +} +*/ +import "C" + +import ( + "sync" + "unsafe" +) + +// napOff begins the activity; the release it returns ends it, once. +func napOff(reason string) func() { + r := C.CString(reason) + defer C.free(unsafe.Pointer(r)) + a := C.magpie_nap_off(r) + var once sync.Once + return func() { once.Do(func() { C.magpie_nap_on(a) }) } +} diff --git a/internal/awake/nap_darwin_test.go b/internal/awake/nap_darwin_test.go new file mode 100644 index 000000000..d9c216d99 --- /dev/null +++ b/internal/awake/nap_darwin_test.go @@ -0,0 +1,37 @@ +//go:build darwin && cgo + +package awake + +import ( + "fmt" + "os" + "os/exec" + "strings" + "testing" +) + +// TestNoNapLetsTheMacSleep holds NoNap and reads the system's power +// assertions: none of this process's may hold sleep off, which is Keep's to +// do and only while the user asked for it (#6261). +func TestNoNapLetsTheMacSleep(t *testing.T) { + if _, err := os.Stat("/usr/bin/pmset"); err != nil { + t.Skip("no pmset:", err) + } + release := NoNap() + defer release() + out, err := exec.Command("/usr/bin/pmset", "-g", "assertions").CombinedOutput() + if err != nil { + t.Fatalf("pmset: %v\n%s", err, out) + } + mine := fmt.Sprintf("pid %d(", os.Getpid()) + for _, l := range strings.Split(string(out), "\n") { + if strings.Contains(l, mine) { + t.Logf("held: %s", strings.TrimSpace(l)) + if strings.Contains(l, "Prevent") { + t.Errorf("NoNap holds sleep off: %s", strings.TrimSpace(l)) + } + } + } + release() + release() // a second release does nothing +} diff --git a/internal/awake/nap_other.go b/internal/awake/nap_other.go new file mode 100644 index 000000000..d3cc97939 --- /dev/null +++ b/internal/awake/nap_other.go @@ -0,0 +1,8 @@ +//go:build !darwin || !cgo + +package awake + +// napOff does nothing: App Nap is a Mac's, and a Mac build without cgo is +// the command-line magpie, which runs under launchd or a terminal, where +// App Nap doesn't reach it. +func napOff(string) func() { return func() {} } diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index c687c0478..88eb862b0 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -914,6 +914,8 @@ func (s *Server) ListenAndServe(ctx context.Context) error { // and keeps the community's plugins up to date, noting others' updates, and the Bun they run on go plugin.KeepUpdated(ctx) go plugin.KeepBunUpdated(ctx) + // and keeps macOS from App Napping the process the agents wait on (#6261) + defer awake.NoNap()() // and, when settings say to, keeps the computer awake while agents work go awake.Keep(ctx, func() awake.Level { switch set := settings.Load(); {