Skip to content

Commit b282d95

Browse files
committed
Fix that can't bring the extension service app back to live when it quits
1 parent 35199a3 commit b282d95

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

CommunicationBridge/ServiceDelegate.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,17 @@ actor ExtensionServiceLauncher {
117117
var isLaunching: Bool = false
118118
var application: NSRunningApplication?
119119
var isApplicationValid: Bool {
120-
if let application, !application.isTerminated { return true }
120+
guard let application else { return false }
121+
if application.isTerminated { return false }
122+
let identifier = application.processIdentifier
123+
if let application = NSWorkspace.shared.runningApplications.first(where: {
124+
$0.processIdentifier == identifier
125+
}) {
126+
Logger.communicationBridge.info(
127+
"Service app found: \(application.processIdentifier) \(String(describing: application.bundleIdentifier))"
128+
)
129+
return true
130+
}
121131
return false
122132
}
123133

@@ -126,9 +136,16 @@ actor ExtensionServiceLauncher {
126136
isLaunching = true
127137

128138
Logger.communicationBridge.info("Launching extension service app.")
139+
129140
NSWorkspace.shared.openApplication(
130141
at: appURL,
131-
configuration: .init()
142+
configuration: {
143+
let configuration = NSWorkspace.OpenConfiguration()
144+
configuration.createsNewApplicationInstance = false
145+
configuration.addsToRecentItems = false
146+
configuration.activates = false
147+
return configuration
148+
}()
132149
) { app, error in
133150
if let error = error {
134151
Logger.communicationBridge.error(

Core/Sources/HostApp/General.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ struct General {
2424
}
2525

2626
@Dependency(\.toast) var toast
27+
28+
struct ReloadStatusCancellableId: Hashable {}
2729

2830
var body: some ReducerOf<Self> {
2931
Reduce { state, action in
@@ -91,7 +93,7 @@ struct General {
9193
toast(error.localizedDescription, .error)
9294
await send(.failedReloading)
9395
}
94-
}
96+
}.cancellable(id: ReloadStatusCancellableId(), cancelInFlight: true)
9597

9698
case let .finishReloading(version, granted):
9799
state.xpcServiceVersion = version

ExtensionService/AppDelegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
5353
@objc func quit() {
5454
Task { @MainActor in
5555
await service.prepareForExit()
56+
await xpcController?.quit()
5657
NSApp.terminate(self)
5758
}
5859
}

ExtensionService/XPCController.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ final class XPCController: XPCServiceDelegate {
2020
self.bridge = bridge
2121

2222
Task {
23-
await bridge.setDelegate(self)
23+
bridge.setDelegate(self)
2424
createPingTask()
2525
}
2626
}
27+
28+
func quit() async {
29+
bridge.setDelegate(nil)
30+
pingTask?.cancel()
31+
try? await bridge.quit()
32+
}
2733

2834
deinit {
2935
xpcListener.invalidate()

Tool/Sources/XPCShared/XPCCommunicationBridge.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class XPCCommunicationBridge {
3434
self.logger = logger
3535
}
3636

37-
public func setDelegate(_ delegate: XPCServiceDelegate) {
37+
public func setDelegate(_ delegate: XPCServiceDelegate?) {
3838
service.delegate = delegate
3939
}
4040

0 commit comments

Comments
 (0)