on main help sign in
refactor: use context for automatic lsp process cleanup
bd2cb4bNacho Vazquez
cmd/root.go−1
@@ -48,7 +48,6 @@ var rootCmd = &cobra.Command{
48 ctx := context.Background()
48 ctx := context.Background()
49
49
50 app := app.New(ctx, conn)
50 app := app.New(ctx, conn)
51− defer app.Close()
52 logging.Info("Starting termai...")
51 logging.Info("Starting termai...")
53 zone.NewGlobal()
52 zone.NewGlobal()
54 tui := tea.NewProgram(
53 tui := tea.NewProgram(
internal/app/services.go+1−17
@@ -3,7 +3,6 @@ package app
3import (
3import (
4 "context"
4 "context"
5 "database/sql"
5 "database/sql"
6− "log/slog"
7
6
8 "github.com/kujtimiihoxha/termai/internal/config"
7 "github.com/kujtimiihoxha/termai/internal/config"
9 "github.com/kujtimiihoxha/termai/internal/db"
8 "github.com/kujtimiihoxha/termai/internal/db"
@@ -23,8 +22,6 @@ type App struct {
23 Permissions permission.Service
22 Permissions permission.Service
24
23
25 LSPClients map[string]*lsp.Client
24 LSPClients map[string]*lsp.Client
26−
27− ceanups []func()
28}
25}
29
26
30func New(ctx context.Context, conn *sql.DB) *App {
27func New(ctx context.Context, conn *sql.DB) *App {
@@ -44,10 +41,7 @@ func New(ctx context.Context, conn *sql.DB) *App {
44 }
41 }
45
42
46 for name, client := range cfg.LSP {
43 for name, client := range cfg.LSP {
47− lspClient, err := lsp.NewClient(client.Command, client.Args...)
44+ lspClient, err := lsp.NewClient(ctx, client.Command, client.Args...)
48− app.ceanups = append(app.ceanups, func() {
49− lspClient.Close()
50− })
51 workspaceWatcher := watcher.NewWorkspaceWatcher(lspClient)
45 workspaceWatcher := watcher.NewWorkspaceWatcher(lspClient)
52 if err != nil {
46 if err != nil {
53 logging.Error("Failed to create LSP client for", name, err)
47 logging.Error("Failed to create LSP client for", name, err)
@@ -64,13 +58,3 @@ func New(ctx context.Context, conn *sql.DB) *App {
64 }
58 }
65 return app
59 return app
66}
60}
67−
68−func (a *App) Close() {
69− for _, cleanup := range a.ceanups {
70− cleanup()
71− }
72− for _, client := range a.LSPClients {
73− client.Close()
74− }
75− slog.Info("App closed")
76−}
internal/lsp/client.go+2−2
@@ -48,8 +48,8 @@ type Client struct {
48 openFilesMu sync.RWMutex
48 openFilesMu sync.RWMutex
49}
49}
50
50
51−func NewClient(command string, args ...string) (*Client, error) {
51+func NewClient(ctx context.Context, command string, args ...string) (*Client, error) {
52− cmd := exec.Command(command, args...)
52+ cmd := exec.CommandContext(ctx, command, args...)
53 // Copy env
53 // Copy env
54 cmd.Env = os.Environ()
54 cmd.Env = os.Environ()
55
55