Skip to content

Commit 398be00

Browse files
committed
[sourcekit] Prevent race between getInitializationInfo and requests
Request handling happens on a global queue, so we need to use a barrier_async on the same queue to ensure the initialization finishes before requests are handled.
1 parent b43c671 commit 398be00

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ static std::string getDiagnosticDocumentationPath() {
222222
return path.str().str();
223223
}
224224

225+
static dispatch_queue_t msgHandlingQueue;
226+
225227
static void sourcekitdServer_peer_event_handler(xpc_connection_t peer,
226228
xpc_object_t event) {
227229
xpc_type_t type = xpc_get_type(event);
@@ -245,8 +247,7 @@ static void sourcekitdServer_peer_event_handler(xpc_connection_t peer,
245247
assert(type == XPC_TYPE_DICTIONARY);
246248
// Handle the message
247249
xpc_retain(event);
248-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),
249-
^{
250+
dispatch_async(msgHandlingQueue, ^{
250251
xpc_object_t contents = xpc_dictionary_get_value(event, "msg");
251252

252253
if (!contents) {
@@ -324,7 +325,7 @@ static void sourcekitdServer_event_handler(xpc_connection_t peer) {
324325
// you can defer this call until after that initialization is done.
325326
xpc_connection_resume(peer);
326327

327-
dispatch_async(dispatch_get_main_queue(), ^{
328+
dispatch_barrier_async(msgHandlingQueue, ^{
328329
getInitializationInfo(MainConnection);
329330
});
330331
}
@@ -368,6 +369,10 @@ int main(int argc, const char *argv[]) {
368369
LOG_WARN_FUNC("getrlimit failed: " << llvm::sys::StrError());
369370
}
370371

372+
auto attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_CONCURRENT,
373+
QOS_CLASS_DEFAULT, 0);
374+
msgHandlingQueue = dispatch_queue_create("request-handling", attr);
375+
371376
xpc_main(sourcekitdServer_event_handler);
372377
return 0;
373378
}

0 commit comments

Comments
 (0)