# Why sourcekit-lsp.exe consumes so much CPU even when idle?

**URL:** <https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140>\
**Category:** Using Swift\
**Tags:** windows, sourcekit-lsp\
**Created:** [February 20, 2024, 3:13am UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140 "2024-02-20T03:13:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [February 20, 2024, 3:13am UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/1 "2024-02-20T03:13:37Z")

</div>

Hello everyone,

My Windows app uses sourcekit-lsp. It processes requests from my app. There are not many requests, and if there is no input from the user in my app, there are no requests at all (I checked, my application is not at fault). Despite this, the sourcekit-lsp process constantly consumes ~24% of CPU power all the time, even when idle. What can cause this? What intensive calculations it does when nothing is requested from it?

---

<div class="post-metadata">

**Author:** ![ahoppen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahoppen/32/36046_2.png) [@ahoppen](https://forums.swift.org/u/ahoppen)\
**Post date:** [February 20, 2024, 4:24am UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/2 "2024-02-20T04:24:23Z")

</div>

Which version of sourcekit-lsp are you using? You might be hitting [Issues on Windows Swift 5.9 · Issue #982 · apple/sourcekit-lsp · GitHub](https://github.com/apple/sourcekit-lsp/issues/982#issuecomment-1836879329). That issue is present in all Swift 5.9 toolchains. Could you try if you see the same issue with a 5.10 or `main` development snapshot from [Swift.org - Download Swift](https://www.swift.org/download/)?

---

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [February 20, 2024, 5:29am UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/3 "2024-02-20T05:29:26Z")

</div>

I am using Swift 5.9.2. Just tried both Swift 5.10 Development and Trunk Development (main) which shows version number 5.11-something. Unfortunately the problem persists in both 🙁

---

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [February 20, 2024, 7:10am UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/4 "2024-02-20T07:10:36Z")

</div>

I've tested older versions, starting from v5.6.1:

```swift
Swift version CPU usage by sourcekit-lsp
5.6.1 0%
5.6.2 not tested
5.6.3 not tested
5.7 not tested
5.7.1 not tested
5.8 0%
5.8.1 0%
5.9 24%
5.9.1 not tested
5.9.2 24%
5.10 dev 24%
trunk dev 24%

```

It seems the bug was introduced in version 5.9 and is still not fixed in either Swift 5.10 Development or Trunk Development (main).

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [February 20, 2024, 6:27pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/5 "2024-02-20T18:27:34Z")

</div>

This is definitely interesting. I wonder if we can isolate this a bit further. I think that it might be possible to isolate libdispatch (though it is unlikely to be the cause as it doesn't change very much). I wonder if the issue is in SourceKit-LSP, sourcekitd, or somewhere else. Hopefully @z2oh might have some time to look into this.

---

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [March 6, 2024, 2:03pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/6 "2024-03-06T14:03:18Z")

</div>

> [@compnerd](#):
>
> Hopefully @z2oh might have some time to look into this.

Swift 5.10 is out, and the bug is still there 😢  
Please sourcekit-lsp developers take a look at this issue.

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [March 6, 2024, 2:45pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/7 "2024-03-06T14:45:59Z")

</div>

Do you know if a corresponding issue with self-contained reproduction steps tracking this exists at [Issues · apple/sourcekit-lsp · GitHub](http://github.com/apple/sourcekit-lsp/issues)? If it doesn't, would you mind filing one?

---

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [March 6, 2024, 4:26pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/8 "2024-03-06T16:26:23Z")

</div>

Thanks for pointing me in the right direction. I have checked the [Issues · apple/sourcekit-lsp · GitHub](https://github.com/apple/sourcekit-lsp/issues) and didn't find a corresponding issue.

I think that I will file the issue there after preparing the steps to reproduce it.

---

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [March 6, 2024, 7:49pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/9 "2024-03-06T19:49:13Z")

</div>

> [@Max\_Desiatov](#):
>
> would you mind filing one?

Done. [Excessive CPU usage even when idle (Windows platform) · Issue #1121 · apple/sourcekit-lsp · GitHub](https://github.com/apple/sourcekit-lsp/issues/1121)

---

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [August 8, 2025, 7:57pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/10 "2025-08-08T19:57:38Z")

</div>

As of April 15 2025 the fix has been [merged](https://github.com/swiftlang/swift-corelibs-libdispatch/issues/820#issuecomment-2807037672). I was happily waiting for Swift 6.2 release, but today… the issue has been [reopened](https://github.com/swiftlang/swift-corelibs-libdispatch/issues/820#issuecomment-3168985937) since it's been reverted from main (and being reverted from 6.2) as well.

Please someone familiar with the issue and the codebase, fix it again.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [August 8, 2025, 10:27pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/11 "2025-08-08T22:27:13Z")

</div>

> [@dima\_kozhinov](#):
>
> Please someone familiar with the issue and the codebase, fix it again.

It really is impossible to fix. The problem is that anonymous pipes on Windows do not function as they do on Unix. There is no mechanism to know the amount of free space in the pipe buffer at any given moment, and thus we cannot perform the partial write to the pipe synchronously. We can do the complete write, but that would block and break the semantics of dispatch’s pipe handling.

I think that one option that we may have at our disposal is for someone in the community to step up and help with the problem in an alternative direction: refactor sourcekit-lsp to not use pipes for communication. We could use a named pipe or some other mechanism where we do not need to rely on dispatch to do the communication.

Edit:  
To be clear, I am suggesting that we remove the stdio communication channel and require a TCP or a named pipe connection always.

---

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [August 11, 2025, 7:36pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/12 "2025-08-11T19:36:04Z")

</div>

@z2oh In the [Switch Win32 pipes to PIPE\_WAIT with sentinel bufsize by z2oh · Pull Request #854 · swiftlang/swift-corelibs-libdispatch · GitHub](https://github.com/swiftlang/swift-corelibs-libdispatch/pull/854#issue-2810557520) you explained:

> The Windows pipe synchronization thread, `_dispatch_pipe_monitor_thread`, uses a blocking 0-byte read as a synchronization mechanism to signal to the actual reader threads that there is data waiting in the pipe. Of course, for this to work, the read must actually be blocking. With a `PIPE_NOWAIT` pipe, the Read will not block, causing the thread to spin endlessly and constantly wake the actual reader thread to perform 0 byte reads. This spinning thread is the root source of the problem in this issue.

Can we get away without trying [to read 0 bytes](https://github.com/swiftlang/swift-corelibs-libdispatch/blob/bb7641e19e64d4bddcf43ca529a54361d55ce210/src/event/event_windows.c#L288), and instead call `PeekNamedPipe` right away (which is [called there later](https://github.com/swiftlang/swift-corelibs-libdispatch/blob/bb7641e19e64d4bddcf43ca529a54361d55ce210/src/event/event_windows.c#L298)) to find out `dwBytesAvailable` ?

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [August 11, 2025, 8:14pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/13 "2025-08-11T20:14:43Z")

</div>

IIRC, that is from my original implementation and no, that is not reasonable to remove. That 0-byte read is the trick to not have the high CPU usage. By reading 0-bytes, we can effectively suspend the monitor and avoid any CPU activity. When that succeeds, we rely on some implementation specifics to determine how much we can safely read. That works great for the _reader_, but that causes problems for the writer, because we do not know how much free space there is in the writer and that effectively means that _all_ writers, including anyone expecting a non-blocking call should become blocking because I can block on any non-zero-sized write.

---

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [August 11, 2025, 9:09pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/14 "2025-08-11T21:09:12Z")

</div>

> [@compnerd](#):
>
> That 0-byte read is the trick to not have the high CPU usage. By reading 0-bytes, we can effectively suspend the monitor and avoid any CPU activity.

As far as I understand, this was true for `PIPE_WAIT` pipes, that is for Swift 5.8.1 and earlier. Since Swift 5.9 these pipes were changed to `PIPE_NOWAIT` and **we have the exact opposite** : The sourcekit-lsp.exe consumes ~50% CPU on 2-core machines even when idle (after the `init` request).

> [@compnerd](#):
>
> That works great for the _reader_, but that causes problems for the writer

As far as I understand, this is why the pipes were changed to `PIPE_NOWAIT` since Swift 5.9.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [August 11, 2025, 9:21pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/15 "2025-08-11T21:21:54Z")

</div>

Correct; the conversion to `PIPE_NOWAIT` was to avoid another issue - we would sometimes fail to write the content and effectively fail to send the message if the buffer was ever filled and we were trying to perform a write. The problem is that the pipe model does not work on Windows because the pipe model is designed for Unix pipes. I suspect that there is no good solution which will solve both sides of the problem.

My recommendation is to simply migrate away from anonymous pipes for the communication channel and switch to a RPC mechanism using COM or gRPC. That should avoid the excessive CPU utilization and do it in a consistent manner across the platforms.

---

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [August 11, 2025, 9:22pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/16 "2025-08-11T21:22:32Z")

</div>

In an attempt to fix [the issue](https://github.com/swiftlang/swift-corelibs-libdispatch/issues/820) the pipes were briefly changed to `PIPE_WAIT`, but these changes did not make it to any release, and were reverted back to `PIPE_NOWAIT`, IIRC due to problems on the write side.

~25% CPU load for 4-core machines or ~50% for 2-core machines indicates that one core is 100% busy. This is a serious performance issue, and everyone seems to ignore it.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [August 11, 2025, 9:29pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/17 "2025-08-11T21:29:13Z")

</div>

> [@dima\_kozhinov](#):
>
> ~25% CPU load for 4-core machines or ~50% for 2-core machines indicates that one core is 100% busy. This is a serious performance issue, and everyone seems to ignore it.

Support for Windows is generally dependent on a very tiny number of engineers. For a long time, it has been just myself, and even now, is primarily ~2.5 engineers. I too would like to see this problem resolved, but it will be when the time is right for the current engineering resources to be spent on it. However, if this is a blocker for you, you are welcome to provide engineering resources to work on the problem.

---

<div class="post-metadata">

**Author:** ![nesrve](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nesrve/32/42463_2.png) [@nesrve](https://forums.swift.org/u/nesrve)\
**Post date:** [August 12, 2025, 10:43am UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/18 "2025-08-12T10:43:45Z")

</div>

May be off-topic, but has it ever been considered a windows working group? If so, where can we vouch for it?

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [August 12, 2025, 2:33pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/19 "2025-08-12T14:33:29Z")

</div>

> [@nesrve](#):
>
> May be off-topic, but has it ever been considered a windows working group?

This has been brought up before. I’m more than happy to form a workgroup. However, I don’t particularly see the value in forming the workgroup if there is not an active set of toolchain developers. The workgroup is valuable when there is the need for more technical conversations which need to occur or coordination needed.

There have been some occasional drive by contributions for windows, but if there were more consistent contributions to windows (e.g. improvements to the prologue/epilogue inserter, improvements to the swift linking model, major design work like removal of exec from the driver, etc), I think that a workgroup would become far more interesting.

---

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [August 12, 2025, 7:51pm UTC](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140/20 "2025-08-12T19:51:26Z")

</div>

Apple could form the workgroup of its employees. It is in their best interest to ensure that their product works perfectly on all officially supported platforms.

[Next page](https://forums.swift.org/t/why-sourcekit-lsp-exe-consumes-so-much-cpu-even-when-idle/70140.md?page=2)
