diff --git a/macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift index 4fb156317..0ce244873 100644 --- a/macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift @@ -2370,6 +2370,7 @@ extension Ghostty.SurfaceView { /// We use this to cache our surface content. This probably should be extracted some day /// to a more generic helper. class CachedValue { + private let lock = NSLock() private var value: T? private let fetch: () -> T private let duration: Duration @@ -2381,10 +2382,15 @@ class CachedValue { } deinit { + lock.lock() expiryTask?.cancel() + lock.unlock() } func get() -> T { + lock.lock() + defer { lock.unlock() } + if let value { return value } @@ -2399,8 +2405,7 @@ class CachedValue { expiryTask = Task { [weak self] in do { try await Task.sleep(until: expires) - self?.value = nil - self?.expiryTask = nil + self?.expire() } catch { // Task was cancelled, do nothing } @@ -2408,4 +2413,12 @@ class CachedValue { return result } + + private func expire() { + lock.lock() + defer { lock.unlock() } + + value = nil + expiryTask = nil + } }