Skip to content

Exit LSP process if parent process stops existing #1076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion cmd/tsgo/lsp.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package main

import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"runtime"
"syscall"
"time"

"github.com/microsoft/typescript-go/internal/bundled"
"github.com/microsoft/typescript-go/internal/core"
@@ -41,6 +45,9 @@ func runLSP(args []string) int {
defaultLibraryPath := bundled.LibPath()
typingsLocation := getGlobalTypingsCacheLocation()

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()

s := lsp.NewServer(&lsp.ServerOptions{
In: os.Stdin,
Out: os.Stdout,
@@ -49,9 +56,27 @@ func runLSP(args []string) int {
FS: fs,
DefaultLibraryPath: defaultLibraryPath,
TypingsLocation: typingsLocation,
SetParentPID: func(pid int) {
go func() {
for {
select {
case <-ctx.Done():
return
case <-time.After(10 * time.Second):
p, err := os.FindProcess(pid)
if err != nil {
os.Exit(1)
}
if p.Signal(syscall.Signal(0)) != nil {
os.Exit(1)
}
}
}
}()
},
})

if err := s.Run(); err != nil {
if err := s.Run(ctx); err != nil {
return 1
}
return 0
19 changes: 12 additions & 7 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
@@ -6,12 +6,9 @@ import (
"errors"
"fmt"
"io"
"os"
"os/signal"
"runtime/debug"
"slices"
"sync"
"syscall"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/ls"
@@ -31,6 +28,8 @@ type ServerOptions struct {
FS vfs.FS
DefaultLibraryPath string
TypingsLocation string

SetParentPID func(pid int)
}

func NewServer(opts *ServerOptions) *Server {
@@ -50,6 +49,7 @@ func NewServer(opts *ServerOptions) *Server {
fs: opts.FS,
defaultLibraryPath: opts.DefaultLibraryPath,
typingsLocation: opts.TypingsLocation,
setParentPID: opts.SetParentPID,
}
}

@@ -83,6 +83,8 @@ type Server struct {
defaultLibraryPath string
typingsLocation string

setParentPID func(pid int)

initializeParams *lsproto.InitializeParams
positionEncoding lsproto.PositionEncodingKind

@@ -187,10 +189,7 @@ func (s *Server) RefreshDiagnostics(ctx context.Context) error {
return nil
}

func (s *Server) Run() error {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()

func (s *Server) Run(ctx context.Context) error {
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error { return s.dispatchLoop(ctx) })
g.Go(func() error { return s.writeLoop(ctx) })
@@ -452,6 +451,12 @@ func (s *Server) handleRequestOrNotification(ctx context.Context, req *lsproto.R
func (s *Server) handleInitialize(req *lsproto.RequestMessage) {
s.initializeParams = req.Params.(*lsproto.InitializeParams)

if s.setParentPID != nil {
if pid := s.initializeParams.ProcessId.Value; pid != 0 {
s.setParentPID(int(pid))
}
}

s.positionEncoding = lsproto.PositionEncodingKindUTF16
if genCapabilities := s.initializeParams.Capabilities.General; genCapabilities != nil && genCapabilities.PositionEncodings != nil {
if slices.Contains(*genCapabilities.PositionEncodings, lsproto.PositionEncodingKindUTF8) {