zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
51.31k stars 3.2k forks source link

Zed hangs at 100% CPU when opening a JSON file if node and npm aren't on the PATH #531

Closed maxbrunsfeld closed 2 years ago

maxbrunsfeld commented 2 years ago

This happens to me when I launched Zed from finder, but also if I run Zed with the PATH environment variable set to the empty string.

Sampling the process, I see that we're in a busy loop calling memset and memmove inside of postage::Stream::recv. I see this in the call stack when attaching to the process with LLDB:

frame #1: 0x000000010155a62c zed`_$LT$core..future..from_generator..GenFuture$LT$T$GT$$u20$as$u20$core..future..future..Future$GT$::poll::hd3ac21d0419cdbb7 [inlined] language::buffer::Buffer::set_language_server::_$u7b$$u7b$closure$u7d$$u7d$::_$u7b$$u7b$closure$u7d$$u7d$::h6e06b734bce4968f((null)=ResumeTy @ x24) at buffer.rs:594:72 [opt]
   591                  _maintain_server: cx.spawn_weak(|this, mut cx| async move {
   592                      let mut capabilities = server.capabilities();
   593                      loop {
-> 594                          if let Some(capabilities) = capabilities.recv().await.flatten() {
   595                              if let Some(this) = this.upgrade(&cx) {
   596                                  let triggers = capabilities
   597                                      .completion_provider
maxbrunsfeld commented 2 years ago

Oh, it's because the future returned by watch::Receiver::recv() resolves immediately with None if the sender has been dropped, so we're busy looping in that line of code.

Kethku commented 2 years ago

Side note: whats the solution to this. I don't understand macos enough yet to know, but it seems like this issue where launching from finder doesn't have a setup terminal environment causes problems for many apps (neovide included). Is there a correct way to launch such that the path is set up even when you run from finder?

maxbrunsfeld commented 2 years ago

In Atom, we would check if the app was launched from finder by inspecting certain environment variables like SHELL. I think we were able to detect it pretty reliably. Then, if we concluded that it was launched from Finder, we would actually spawn a login shell process that just printed its PATH environment variable (after loading the user's initialization script).

I think we'll need to do something like that in Zed on macOS, if we want to continue to rely on any external system-installed programs.