Skip to content

[4.1][sourcekitd] Fix crash in onDocumentUpdateNotification #14353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

nathawes
Copy link
Contributor

@nathawes nathawes commented Feb 2, 2018

CCC Info

  • Radar: rdar://problem/36077481

  • Explanation:
    SourceKit didn't xpc_retain a globally stored XPC connection reference, MainConnection, allowing it to be used in an invalid state. This happened if the client cancelled the connection or crashed while SourceKit was busy gathering semantic information for a document. SourceKit would use the invalid connection to try to notify the client when it finished and crash.

  • Risk: Low. This is simply adding a retain call.

  • Scope of issue: One of the top SourceKit crashes

  • Origination: It looks like this issue has always existed.

  • Reviewed by: Argyrios Kyrtzidis

  • Testing: Cancelled the connection from the client with an outstanding semantic operation before and after the changes to verify the fix. Manually tested changes against Xcode 9.2GM and 9.3 beta 1 with basic editing, completions, navigation etc. in new projects.

PR for swift-4.1-branch: #14353
PR for master: #14355
PR for swift-5.0-branch: #14354

@nathawes nathawes changed the title [4.1] Fix crash in onDocumentUpdateNotification [4.1][sourcekitd] Fix crash in onDocumentUpdateNotification Feb 2, 2018
@nathawes
Copy link
Contributor Author

nathawes commented Feb 2, 2018

@swift-ci please test

@nathawes nathawes requested a review from akyrtzi February 2, 2018 20:49
// Update the main connection
if (MainConnection)
xpc_release(MainConnection);
MainConnection = xpc_connection_t(xpc_retain(peer));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generally safe retain/release pattern, in case the 2 objects (new and old) happen to be the same, is to retain the new one before releasing the old one:

temp_object = retain(new)
release(old)
old = temp_object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll fix it up shortly!

@@ -208,8 +208,15 @@ std::string sourcekitd::getRuntimeLibPath() {
return Path;
}

static void releaseIfMainConnection(xpc_connection_t peer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary ? Seems risky to set the global main connection to null. What is the benefit ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nathan explained the reasoning for this.

@nathawes nathawes force-pushed the rdar36077481-crash-in-on-document-update-notification-4.1 branch from 9dabcb5 to fa15967 Compare February 2, 2018 23:55
@nathawes
Copy link
Contributor Author

nathawes commented Feb 2, 2018

@swift-ci please test

…g when cancelled from the other end

If a client crashes and we tried to post notifications to the XPC connection,
SourceKitService would go down too, as it never `xpc_retain`ed the connection.

Resolves rdar://problem/36077481
@nathawes
Copy link
Contributor Author

nathawes commented Feb 3, 2018

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Feb 3, 2018

Build failed
Swift Test OS X Platform
Git Sha - 9dabcb58d555f12ae50ba823e8c7d8e67aa30a24

@swift-ci
Copy link
Contributor

swift-ci commented Feb 3, 2018

Build failed
Swift Test Linux Platform
Git Sha - 9dabcb58d555f12ae50ba823e8c7d8e67aa30a24

@nathawes nathawes requested a review from akyrtzi February 3, 2018 02:53
@nathawes nathawes merged commit 23cbd11 into swiftlang:swift-4.1-branch Feb 5, 2018
@nathawes nathawes deleted the rdar36077481-crash-in-on-document-update-notification-4.1 branch February 5, 2018 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants