# Fix race condition in test_collaborating_with_completion \(\#44806\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/a51585d2daaa975409a835f43574c8bb5bcc9d5b)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: a51585d2daaa975409a835f43574c8bb5bcc9d5b

Requested commit: a51585d2daaa975409a835f43574c8bb5bcc9d5b

Commit: a51585d2daaa975409a835f43574c8bb5bcc9d5b

Tree: a2e3b995f04cac637eab42ec841701678ac2afbb

Author: Nathan Sobo

Committer: GitHub

## Message

````
Fix race condition in test_collaborating_with_completion (#44806)

The test `test_collaborating_with_completion` has a latent race
condition that hasn't manifested on CI yet but could cause hangs with
certain task orderings.

## The Bug

Commit `fd1494c31a` set up LSP request handlers AFTER typing the trigger
character:

```rust
// Type trigger first - spawns async tasks to send completion request
editor_b.update_in(cx_b, |editor, window, cx| {
    editor.handle_input(".", window, cx);
});

// THEN set up handlers (race condition!)
fake_language_server
    .set_request_handler::<lsp::request::Completion, _, _>(...)
    .next().await.unwrap();  // Waits for handler to receive a request
```

Whether this works depends on task scheduling order, which varies by
seed. If the completion request is processed before the handler is
registered, the request goes to `on_unhandled_notification` which claims
to handle it but sends no response, causing a hang.

## Changes

- Move handler setup BEFORE typing the trigger character
- Make `TestDispatcher::spawn_realtime` panic to prevent future
non-determinism from real OS threads
- Add `execution_hash()` and `execution_count()` to TestDispatcher for
debugging
- Add `DEBUG_SCHEDULER=1` logging for task execution tracing
- Document the investigation in `situation.md`

cc @localcc @SomeoneToIgnore (authors of related commits)

Release Notes:

- N/A

---------

Co-authored-by: Kirill Bulatov <kirill@zed.dev>
````

## Parents

- [26b261a33645f20993fd8f819109b37cabcdc67c](https://git.cafe/gitcafe/zed/commit/26b261a33645f20993fd8f819109b37cabcdc67c?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/a51585d2daaa975409a835f43574c8bb5bcc9d5b?format=markdown)
