From b9741a1b57df53ba722a78947e39dee3316d2a27 Mon Sep 17 00:00:00 2001 From: Lukas <134181853+bo2themax@users.noreply.github.com> Date: Thu, 13 Aug 2026 18:29:24 +0200 Subject: [PATCH] macOS: avoid holding SurfaceView when sending notifications --- .../Ghostty/Surface View/SurfaceView_AppKit.swift | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift index 35b99e4368..7055ae8ffc 100644 --- a/macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift @@ -1787,20 +1787,22 @@ extension Ghostty { // Note the callback may be executed on a background thread as documented // so we need @MainActor since we're reading/writing view state. - Task { @MainActor in + // We use [weak self] here because we don't want to extend the surface's + // lifetime when a notification is triggered right before the surface closes. + Task { @MainActor [weak self] in do { try await UNUserNotificationCenter.current().add(request) // We need to keep track of this notification so we can remove it // under certain circumstances - notificationIdentifiers.insert(uuid) + self?.notificationIdentifiers.insert(uuid) // If we're focused then we schedule to remove the notification // after a few seconds. If we gain focus we automatically remove it // in focusDidChange. - if self.focused { + if self?.focused == true { try await Task.sleep(for: .seconds(3)) - notificationIdentifiers.remove(uuid) + self?.notificationIdentifiers.remove(uuid) UNUserNotificationCenter.current() .removeDeliveredNotifications(withIdentifiers: [uuid]) }