Skip to content

Commit

Permalink
Attempt to fix continuation resume crash
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Aug 25, 2024
1 parent 90be44b commit ce0e60f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
37 changes: 21 additions & 16 deletions Ice/MenuBar/MenuBarItemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,6 @@ extension MenuBarItemManager {
}
}

/// Delays an event tap callback from returning.
private func delayEventTapCallback() {
// Small delay to prevent timeouts when running alongside certain event tapping apps.
// TODO: Try to find a better solution for this.
Thread.sleep(forTimeInterval: 0.015)
}

/// Posts an event to the given event tap location and waits until it is
/// received before returning.
///
Expand All @@ -491,6 +484,7 @@ extension MenuBarItemManager {
/// - item: The menu bar item that the event affects.
private func postEventAndWaitToReceive(_ event: CGEvent, to location: EventTap.Location, item: MenuBarItem) async throws {
return try await withCheckedThrowingContinuation { continuation in
var resumed = false
let eventTap = EventTap(
options: .defaultTap,
location: location,
Expand Down Expand Up @@ -523,16 +517,21 @@ extension MenuBarItemManager {
Logger.itemManager.debug("Received \(type.logString) at \(location.logString) (item: \(item.logString))")

proxy.disable()
continuation.resume()
delayEventTapCallback()
if !resumed {
resumed = true
continuation.resume()
}

return rEvent
}

eventTap.enable(timeout: .milliseconds(50)) {
eventTap.enable(timeout: 0.05) {
Logger.itemManager.error("Event tap \"\(eventTap.label)\" timed out (item: \(item.logString))")
eventTap.disable()
continuation.resume()
if !resumed {
resumed = true
continuation.resume()
}
}

postEvent(event, to: location)
Expand All @@ -554,6 +553,7 @@ extension MenuBarItemManager {
item: MenuBarItem
) async throws {
return try await withCheckedThrowingContinuation { continuation in
var resumed = false
let eventTap = EventTap(
options: .defaultTap,
location: initialLocation,
Expand Down Expand Up @@ -588,16 +588,21 @@ extension MenuBarItemManager {
postEvent(event, to: forwardedLocation)

proxy.disable()
continuation.resume()
delayEventTapCallback()
if !resumed {
resumed = true
continuation.resume()
}

return rEvent
}

eventTap.enable(timeout: .milliseconds(50)) {
eventTap.enable(timeout: 0.05) {
Logger.itemManager.error("Event tap \"\(eventTap.label)\" timed out (item: \(item.logString))")
eventTap.disable()
continuation.resume()
if !resumed {
resumed = true
continuation.resume()
}
}

postEvent(event, to: initialLocation)
Expand Down Expand Up @@ -1125,7 +1130,7 @@ extension MenuBarItemManager {
continue
}
do {
try await move(item: item, to: context.returnDestination)
try await slowMove(item: item, to: context.returnDestination)
} catch {
Logger.itemManager.error("Failed to rehide \(item.logString) (error: \(error))")
failedContexts.append(context)
Expand Down
13 changes: 5 additions & 8 deletions Ice/Utilities/EventMonitors/EventTap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class EventTap {
}

/// Enables the event tap with the given timeout.
func enable(timeout: Duration, onTimeout: @escaping () -> Void) {
func enable(timeout: TimeInterval, onTimeout: @escaping () -> Void) {
tap.enable(timeout: timeout, onTimeout: onTimeout)
}

Expand Down Expand Up @@ -231,17 +231,14 @@ class EventTap {
}

/// Enables the event tap with the given timeout.
func enable(timeout: Duration, onTimeout: @escaping () -> Void) {
func enable(timeout: TimeInterval, onTimeout: @escaping () -> Void) {
enable()
Task { [weak self] in
try await Task.sleep(for: timeout)
queue.asyncAfter(deadline: .now() + timeout) { [weak self] in
guard let self else {
return
}
queue.async {
if self.isEnabled {
onTimeout()
}
if isEnabled {
onTimeout()
}
}
}
Expand Down

0 comments on commit ce0e60f

Please sign in to comment.