Skip to content
This repository has been archived by the owner on May 8, 2019. It is now read-only.

The server crashed 5 times in last 3 minutes #27

Open
BProg opened this issue Feb 10, 2017 · 8 comments
Open

The server crashed 5 times in last 3 minutes #27

BProg opened this issue Feb 10, 2017 · 8 comments

Comments

@BProg
Copy link

BProg commented Feb 10, 2017

I have forked both projects, the vscode-swift and langserver, when trying to run vscode-swift , I get this error

And here is the top of backtrace :

fatal error: unexpectedly found nil while unwrapping an Optional value
Current stack trace:
0    libswiftCore.dylib                 0x0000000104838ce0 swift_reportError + 132
1    libswiftCore.dylib                 0x0000000104856090 _swift_stdlib_reportFatalError + 61
2    libswiftCore.dylib                 0x000000010464c0c0 specialized specialized StaticString.withUTF8Buffer<A> ((UnsafeBufferPointer<UInt8>) -> A) -> A + 355
3    libswiftCore.dylib                 0x00000001047c8230 partial apply for (_fatalErrorMessage(StaticString, StaticString, StaticString, UInt, flags : UInt32) -> Never).(closure #2) + 109
4    libswiftCore.dylib                 0x000000010464c0c0 specialized specialized StaticString.withUTF8Buffer<A> ((UnsafeBufferPointer<UInt8>) -> A) -> A + 355
5    libswiftCore.dylib                 0x00000001047803f0 specialized _fatalErrorMessage(StaticString, StaticString, StaticString, UInt, flags : UInt32) -> Never + 96
6    LanguageServer                     0x0000000104053530 handle(Request) -> Response + 2030
7    LanguageServer                     0x0000000104051010 (closure #1) + 2132
@RLovelett
Copy link
Owner

First off I want to say. I'm sorry you are experiencing this!

Unfortunately, at this time there isn't a great story around debugging these sorts of errors. Would you be able to provide me with either the code or a snippet of the code, that reproduces the issue?

If so I'd probably be able to quickly turn something around for you.

@BProg
Copy link
Author

BProg commented Feb 12, 2017

I didn't write any code, just an empty file. If manually changing file type from plain text to swift, I get this crash

@BProg
Copy link
Author

BProg commented Feb 21, 2017

Could you please write the steps you take to debug server app.
What I tried was:

  1. Change LanguageServer scheme to attach to process when run
  2. Put a breakpoint in main.swift:18
  3. Run client
  4. Create New file
  5. Change file type from text to swift

Only one time server stopped at breakpoint. Then I tried a lot of times and nothing happend, the server just crashes and no breakpoints stops.

@BProg
Copy link
Author

BProg commented Feb 21, 2017

Never mind, found solution. I should attach as root not as user.

@RLovelett
Copy link
Owner

RLovelett commented Feb 22, 2017

Not sure why running as root should be necessary. I never need to do that to make it work. 🤔

I'll go ahead and write up how I debug/develop this package. 👨‍💻

Xcode (e.g., langserver-swift)

In my langserver-swift local clone I generate an Xcode project.

% swift package generate-xcodeproj --xcconfig-overrides settings.xcconfig

I then open the generated project in Xcode. From there I make a few changes to the way the debugger attaches to the program. See the screenshot below.

screen shot 2017-02-22 at 8 55 57 am

I then build and run the program. Xcode should report something like "Waiting to attach to LanguageServer : LanguageServer".

screen shot 2017-02-22 at 9 40 33 am

VSCode (e.g., vscode-swift)

In my vscode-swift local clone I open VScode (e.g., code .).

I then start the TypeScript compiler or the build task (e.g., ⇧⌘B or Tasks: Run Build Task).

Figure out where Xcode built the LanguageServer executable above. One way to do this is to ask xcodebuild.

$ xcodebuild -project langserver-swift.xcodeproj -target "LanguageServer" -showBuildSettings | grep "TARGET_BUILD_DIR"
   TARGET_BUILD_DIR = /Users/ryan/Library/Developer/Xcode/DerivedData/langserver-swift-gellhgzzpradfqbgjnbtkvzjqymv/Build/Products/Debug

Get the fully qualified path then open src/extension.ts and make a change similar to the patch I'm showing here.

diff --git a/src/extension.ts b/src/extension.ts
index b5ad751..7970ae1 100644
--- a/src/extension.ts
+++ b/src/extension.ts
@@ -13,7 +13,7 @@ export function activate(context: ExtensionContext) {
         .get("languageServerPath", "/usr/local/bin/LanguageServer");

     let run: Executable = { command: executableCommand };
-    let debug: Executable = run;
+    let debug: Executable = { command: "/Users/ryan/Library/Developer/Xcode/DerivedData/langserver-swift-gellhgzzpradfqbgjnbtkvzjqymv/Build/Products/Debug/LanguageServer" };
     let serverOptions: ServerOptions = {
         run: run,
         debug: debug

Once this is complete you should be able to open the VSCode debugger and and select Launch Extension. This should start both the language server (Xcode/Swift) and the extension (VScode/TypeScript) in debug mode.

Caveats

  1. As you've noted above you might not be able to capture all the commands upon the language server initially starting up. My guess is that this has something to do with it taking a little bit of time for LLDB (the Swift debugger) to actually attach to the running process.

My recommendation is to put a break-point in handle.swift as this is likely where the server is getting into to trouble.

  1. Another thing I sometimes do is apply a patch to Sources/LanguageServer/main.swift that logs all the messages coming from the VSCode extension.

It is worth noting two things:

  1. It is going to be writing logs to a directory on the running user's desktop in a new folder called "logs".
  2. At the point this logger is injected it is before any processing is done on the text buffers. Which means that a full command may actually span multiple logs.
From f5142fc6c296b72d777646ce0cac487bd6731686 Mon Sep 17 00:00:00 2001
From: Ryan Lovelett <[email protected]>
Date: Sat, 10 Dec 2016 10:16:39 -0500
Subject: [PATCH 1/9] [DEBUG ONLY DO NOT MERGE] Save the buffers as they come
 in

---
 Sources/LanguageServer/main.swift | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/Sources/LanguageServer/main.swift b/Sources/LanguageServer/main.swift
index d99bf54..4d63e70 100644
--- a/Sources/LanguageServer/main.swift
+++ b/Sources/LanguageServer/main.swift
@@ -11,6 +11,7 @@ let main = OperationQueue.main
 let stdin = FileHandle.standardInput
 var iterator = RequestIterator(Data())
 stdin.waitForDataInBackgroundAndNotify()
+var index = 0

 // When new data is available
 var dataAvailable : NSObjectProtocol!
@@ -25,6 +26,13 @@ dataAvailable = NotificationCenter.default.addObserver(forName: .NSFileHandleDat

     let requests = AnySequence<Data>() { iterator }

+    let home = URL(fileURLWithPath: NSHomeDirectory(), isDirectory: true)
+    let desktop = URL(fileURLWithPath: "Desktop", isDirectory: true, relativeTo: home)
+    let logs = URL(fileURLWithPath: "logs", isDirectory: true, relativeTo: desktop)
+    let log = URL(fileURLWithPath: "buffer-\(index).txt", relativeTo: logs)
+    try! buffer.write(to: log)
+    index += 1
+
     for requestBuffer in requests {
         let str = String(data: buffer, encoding: .utf8) ?? "Expected UTF-8 encoding."

--
2.11.1

Finally

Thank you for the feedback and question! I hope that provides a enough information to be useful to someone.

I'm going to try and transfer this answer into the README for both vscode-swift and language-server as I think it'll lower the barrier to entry to someone else who experiences a problem such as yours.

Again thank you for the question! 🙏

Edit on May 11, 2017

I figured out how to get the path the debug executable using xcodebuild.

@BProg
Copy link
Author

BProg commented Feb 22, 2017

Thank you 👍.

@BProg
Copy link
Author

BProg commented Feb 22, 2017

Again thanks a lot man, It was EXTREMELY usefull.

@RLovelett
Copy link
Owner

Glad you found it useful! Hopefully you can find the cause of the crash. Even if you can't fix it if you can document it then I might be able to help you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants