Skip to content

Commit c43c051

Browse files
committed
[SourceKit] Add function to dispose a request handle
This allows the client to dispose of a request handle manually, cleaning up memory leaks that could previoulsy resulted from cancelling a request that has finished. It also gives us more flexibility to change to a different representation of request handles that require manual disposal in the future.
1 parent 772485d commit c43c051

File tree

10 files changed

+32
-0
lines changed

10 files changed

+32
-0
lines changed

tools/SourceKit/bindings/python/sourcekitd/capi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ def __repr__(self):
315315
functionList = [
316316
("sourcekitd_cancel_request",
317317
[c_void_p]),
318+
("sourcekitd_request_handle_dispose",
319+
[c_void_p]),
318320

319321
("sourcekitd_initialize",
320322
None),

tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,8 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
12281228
^(sourcekitd_response_t resp) {
12291229
auto &info = asyncResponses[respIndex];
12301230
info.response = resp;
1231+
sourcekitd_request_handle_dispose(
1232+
info.requestHandle);
12311233
info.semaphore.signal(); // Ready to be handled!
12321234
});
12331235

tools/SourceKit/tools/sourcekitd/bin/InProc/sourcekitdInProc-darwin.exports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
sourcekitd_cancel_request
2+
sourcekitd_request_handle_dispose
23
sourcekitd_initialize
34
sourcekitd_request_array_create
45
sourcekitd_request_array_set_int64

tools/SourceKit/tools/sourcekitd/bin/InProc/sourcekitdInProc.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ void sourcekitd_cancel_request(sourcekitd_request_handle_t handle) {
162162
sourcekitd::cancelRequest(/*CancellationToken=*/handle);
163163
}
164164

165+
void sourcekitd_request_handle_dispose(sourcekitd_request_handle_t handle) {
166+
sourcekitd::disposeCancellationToken(/*CancellationToken=*/handle);
167+
}
168+
165169
void
166170
sourcekitd_set_interrupted_connection_handler(
167171
sourcekitd_interrupted_connection_handler_t handler) {

tools/SourceKit/tools/sourcekitd/bin/InProc/sourcekitdInProc.exports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
sourcekitd_cancel_request
2+
sourcekitd_request_handle_dispose
23
sourcekitd_initialize
34
sourcekitd_request_array_create
45
sourcekitd_request_array_set_int64

tools/SourceKit/tools/sourcekitd/bin/XPC/Client/sourcekitd.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,17 @@ void sourcekitd_cancel_request(sourcekitd_request_handle_t handle) {
227227
xpc_release(msg);
228228
}
229229

230+
void sourcekitd_request_handle_dispose(sourcekitd_request_handle_t handle) {
231+
xpc_connection_t conn = getGlobalConnection();
232+
xpc_object_t msg = xpc_dictionary_create(nullptr, nullptr, 0);
233+
xpc_dictionary_set_uint64(msg, xpc::KeyDisposeRequestHandle,
234+
reinterpret_cast<uint64_t>(handle));
235+
236+
xpc_connection_send_message(conn, msg);
237+
238+
xpc_release(msg);
239+
}
240+
230241
/// To avoid repeated crashes, used to notify the service to delay typechecking
231242
/// in the editor for a certain amount of seconds.
232243
static std::atomic<size_t> SemanticEditorDelaySecondsNum;

tools/SourceKit/tools/sourcekitd/bin/XPC/Client/sourcekitd.exports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
sourcekitd_cancel_request
2+
sourcekitd_request_handle_dispose
23
sourcekitd_initialize
34
sourcekitd_request_array_create
45
sourcekitd_request_array_set_int64

tools/SourceKit/tools/sourcekitd/bin/XPC/Service/XPCService.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ static void sourcekitdServer_peer_event_handler(xpc_connection_t peer,
275275
xpc_dictionary_get_uint64(event,
276276
xpc::KeyCancelRequest))) {
277277
sourcekitd::cancelRequest(/*CancellationToken=*/cancelToken);
278+
} else if (SourceKitCancellationToken cancelToken =
279+
reinterpret_cast<SourceKitCancellationToken>(
280+
xpc_dictionary_get_uint64(
281+
event, xpc::KeyDisposeRequestHandle))) {
282+
sourcekitd::disposeCancellationToken(/*CancellationToken=*/cancelToken);
278283
} else {
279284
assert(false && "unexpected message");
280285
}

tools/SourceKit/tools/sourcekitd/include/sourcekitd/Internal-XPC.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static const char *KeyTracingEnabled = "tracing_enabled";
2525
static const char *KeyMsgResponse = "response";
2626
static const char *KeyCancelToken = "cancel_token";
2727
static const char *KeyCancelRequest = "cancel_request";
28+
static const char *KeyDisposeRequestHandle = "dispose_request_handle";
2829

2930
enum class Message {
3031
Initialization,

tools/SourceKit/tools/sourcekitd/include/sourcekitd/sourcekitd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,10 @@ SOURCEKITD_PUBLIC
627627
void
628628
sourcekitd_cancel_request(sourcekitd_request_handle_t handle);
629629

630+
/// Dispose a request handle returned by \c sourcekitd_send_request.
631+
SOURCEKITD_PUBLIC
632+
void sourcekitd_request_handle_dispose(sourcekitd_request_handle_t handle);
633+
630634
#if SOURCEKITD_HAS_BLOCKS
631635

632636
/// Sets the handler which should be called to receive notifications.

0 commit comments

Comments
 (0)