Skip to content

Commit de7fd46

Browse files
committed
Crash SourceKitService to restart it
Shutdown an re-initialize would (sometimes?) give us back the previous XPC connection. The crash request is working even if SourceKit is busy fulfilling other requests and is guaranteed to spawn a new process.
1 parent dc3c77d commit de7fd46

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

SourceKitStressTester/Sources/StressTester/SourceKitDocument.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,10 @@ class SourceKitDocument {
457457
case .timedOut:
458458
/// We timed out waiting for the response. Any following requests would
459459
/// not be executed by SourceKit until this one finishes. To avoid this,
460-
/// and since we don't have cancellation support in SourceKit, set up a
461-
/// new connection that is free to execute requests.
462-
connection.restart()
460+
/// and since we don't have cancellation support in SourceKit, crash the
461+
/// current SourceKitService so we get a new instance that isn't
462+
/// processing any requests.
463+
connection.crash()
463464
throw SourceKitError.timedOut(request: info)
464465
}
465466
}

SourceKitStressTester/Sources/SwiftSourceKit/SourcekitdClient.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,15 @@ public class SourceKitdService {
8383
sourcekitd_shutdown()
8484
}
8585

86-
/// Restarts the service. This is a workaround to set up a new service in case
86+
/// Crash the service. This is a workaround to set up a new service in case
8787
/// we time out waiting for a request response and we want to handle it.
8888
/// Replace by proper cancellation once we have cancellation support in
8989
/// SourceKit.
90-
public func restart() {
91-
sourcekitd_shutdown()
92-
// We need to wait for the old service to fully shut down before we can
93-
// create a new one but we don't receive a notification when the old service
94-
// did shut down. Waiting for a second seems to give it enough time.
95-
sleep(1)
96-
initializeService()
90+
public func crash() {
91+
let request = SourceKitdRequest(uid: .request_CrashWithExit)
92+
_ = sourcekitd_send_request_sync(request.rawRequest)
9793
stateQueue.sync {
98-
self.state = .running
94+
self.state = .interrupted
9995
}
10096
}
10197

0 commit comments

Comments
 (0)