Skip to content

Commit 397053c

Browse files
committed
Better cleanup...
1 parent 72884b6 commit 397053c

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

Diff for: handler/handler.go

+27-12
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type InoHandler struct {
5151
clangdNotificationCount int64
5252
progressHandler *ProgressProxyHandler
5353

54+
closing chan bool
5455
clangdStarted *sync.Cond
5556
dataMux sync.RWMutex
5657
lspInitializeParams *lsp.InitializeParams
@@ -119,6 +120,7 @@ func NewInoHandler(stdio io.ReadWriteCloser, board lsp.Board) *InoHandler {
119120
handler := &InoHandler{
120121
docs: map[string]*lsp.TextDocumentItem{},
121122
inoDocsWithDiagnostics: map[lsp.DocumentURI]bool{},
123+
closing: make(chan bool),
122124
config: lsp.BoardConfig{
123125
SelectedBoard: board,
124126
},
@@ -158,15 +160,27 @@ type FileData struct {
158160
version int
159161
}
160162

161-
// StopClangd closes the connection to the clangd process.
162-
func (handler *InoHandler) StopClangd() {
163-
handler.ClangdConn.Close()
164-
handler.ClangdConn = nil
163+
// Close closes all the json-rpc connections.
164+
func (handler *InoHandler) Close() {
165+
if handler.ClangdConn != nil {
166+
handler.ClangdConn.Close()
167+
handler.ClangdConn = nil
168+
}
169+
if handler.closing != nil {
170+
close(handler.closing)
171+
handler.closing = nil
172+
}
173+
}
174+
175+
// CloseNotify returns a channel that is closed when the InoHandler is closed
176+
func (handler *InoHandler) CloseNotify() <-chan bool {
177+
return handler.closing
165178
}
166179

167180
// CleanUp performs cleanup of the workspace and temp files create by the language server
168181
func (handler *InoHandler) CleanUp() {
169182
if handler.buildPath != nil {
183+
log.Printf("removing buildpath")
170184
handler.buildPath.RemoveAll()
171185
handler.buildPath = nil
172186
}
@@ -489,12 +503,14 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
489503
// Exit the process and trigger a restart by the client in case of a severe error
490504
if err.Error() == "context deadline exceeded" {
491505
log.Println(prefix + "Timeout exceeded while waiting for a reply from clangd.")
492-
handler.exit()
506+
log.Println(prefix + "Please restart the language server.")
507+
handler.Close()
493508
}
494509
if strings.Contains(err.Error(), "non-added document") || strings.Contains(err.Error(), "non-added file") {
495510
log.Printf(prefix + "The clangd process has lost track of the open document.")
496511
log.Printf(prefix+" %s", err)
497-
handler.exit()
512+
log.Println(prefix + "Please restart the language server.")
513+
handler.Close()
498514
}
499515
}
500516

@@ -505,12 +521,6 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
505521
return result, err
506522
}
507523

508-
func (handler *InoHandler) exit() {
509-
log.Println("Please restart the language server.")
510-
handler.StopClangd()
511-
os.Exit(1)
512-
}
513-
514524
func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.InitializeParams) error {
515525
currCppTextVersion := 0
516526
if params != nil {
@@ -579,6 +589,11 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
579589
handler.ClangdConn = jsonrpc2.NewConn(context.Background(), clangdStream, clangdHandler,
580590
jsonrpc2.OnRecv(streams.JSONRPCConnLogOnRecv("IDE LS <-- CL:")),
581591
jsonrpc2.OnSend(streams.JSONRPCConnLogOnSend("IDE LS --> CL:")))
592+
go func() {
593+
<-handler.ClangdConn.DisconnectNotify()
594+
log.Printf("Lost connection with clangd!")
595+
handler.Close()
596+
}()
582597

583598
// Send initialization command to clangd
584599
ctx, cancel := context.WithTimeout(context.Background(), time.Second)

Diff for: main.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"log"
77
"os"
8+
"os/signal"
89

910
"github.com/arduino/go-paths-helper"
1011
"github.com/bcmi-labs/arduino-language-server/handler"
@@ -53,7 +54,16 @@ func main() {
5354
}
5455

5556
inoHandler := handler.NewInoHandler(stdio, initialBoard)
56-
defer inoHandler.StopClangd()
57-
defer inoHandler.CleanUp()
58-
<-inoHandler.StdioConn.DisconnectNotify()
57+
58+
// Intercept kill signal
59+
c := make(chan os.Signal, 2)
60+
signal.Notify(c, os.Interrupt, os.Kill)
61+
62+
select {
63+
case <-inoHandler.CloseNotify():
64+
case <-c:
65+
log.Println("INTERRUPTED")
66+
}
67+
inoHandler.CleanUp()
68+
inoHandler.Close()
5969
}

0 commit comments

Comments
 (0)