Skip to content

Wait for cancellation to propagate in testDontReturnEmptyDiagnosticsIfDiagnosticRequestIsCancelled #1412

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

Merged
merged 1 commit into from
Jun 4, 2024

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Jun 3, 2024

I saw a few non-deterministic test failures. I think the issue was that handling of the CancelRequestNotification is done asynchronously, which left a short window in which the sourcekitd diagnostics request could run and return results instead of being cancelled. Wait for the diagnostic request to actually be cancelled before running it for real.

While doing this, also introduce proper sourcekitd test hooks instead of relying on the preparation test hooks, which just got run as a side effect.

@ahoppen ahoppen requested review from bnbarham and hamishknight June 3, 2024 21:38
@ahoppen ahoppen requested a review from benlangmuir as a code owner June 3, 2024 21:38
@ahoppen
Copy link
Member Author

ahoppen commented Jun 3, 2024

@swift-ci Please test

Comment on lines 122 to 123
// `log(request:)` is called in `SourceKitD.send` before the request is sent, so we're hooking in here.
testHooks.sourcekitdRequestDidStart?(request)
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason not to put this in func send? I guess to avoid exposing it in the protocol?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking because protocols can’t have stored properties. But somehow didn’t occur to me before that we could just make the test hooks a protocol requirement… 🤦🏽

@ahoppen ahoppen force-pushed the wait-for-prepration-to-propagate branch 2 times, most recently from 420dc8a to 03b8a8a Compare June 4, 2024 14:21
@ahoppen
Copy link
Member Author

ahoppen commented Jun 4, 2024

@swift-ci Please test

1 similar comment
@ahoppen
Copy link
Member Author

ahoppen commented Jun 4, 2024

@swift-ci Please test

…IfDiagnosticRequestIsCancelled`

I saw a few non-deterministic test failures. I think the issue was that handling of the `CancelRequestNotification` is done asynchronously, which left a short window in which the sourcekitd diagnostics request could run and return results instead of being cancelled. Wait for the diagnostic request to actually be cancelled before running it for real.

While doing this, also introduce proper sourcekitd test hooks instead of relying on the preparation test hooks, which just got run as a side effect.
@ahoppen ahoppen force-pushed the wait-for-prepration-to-propagate branch from 03b8a8a to 37e6a8a Compare June 4, 2024 18:34
@ahoppen
Copy link
Member Author

ahoppen commented Jun 4, 2024

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Jun 4, 2024

@swift-ci Please test Windows

@ahoppen ahoppen merged commit 5ac5b56 into swiftlang:main Jun 4, 2024
3 checks passed
@ahoppen ahoppen deleted the wait-for-prepration-to-propagate branch June 4, 2024 22:12
ahoppen added a commit to ahoppen/sourcekit-lsp that referenced this pull request Jun 5, 2024
Turns out that sourcekitd test hooks were a bad idea because of the following comment that I wrote:

```
`testHooks` are only considered when an instance is being created. If a sourcekitd instance at the given path already exists, its test hooks will be used.
```

During test execution in Xcode, we generate a bunch of `SourceKitServer` instances in the same process that all call `DynamicallyLoadedSourceKitD.getOrCreate`. Now, if `testDontReturnEmptyDiagnosticsIfDiagnosticRequestIsCancelled` is not the first test being executed in the process (which usually it is not), the test hooks in it won’t get used.

Switch back to using the preparation hooks, essentially reverting swiftlang#1412 and keeping the following snippet to fix the underlying issue

```swift
// Poll until the `CancelRequestNotification` has been propagated to the request handling.
for _ in 0..<Int(defaultTimeout * 100) {
  if Task.isCancelled {
    break
  }
  usleep(10_000)
}
```
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.

2 participants