Skip to content
This repository was archived by the owner on Sep 18, 2025. It is now read-only.

Commit 9004b91

Browse files
Merge pull request #28 from naxodev/refactor/improve-process-management
refactor: use context for automatic lsp process cleanup
2 parents 6fe85c6 + bd2cb4b commit 9004b91

File tree

3 files changed

+3
-20
lines changed

3 files changed

+3
-20
lines changed

cmd/root.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ var rootCmd = &cobra.Command{
4848
ctx := context.Background()
4949

5050
app := app.New(ctx, conn)
51-
defer app.Close()
5251
logging.Info("Starting termai...")
5352
zone.NewGlobal()
5453
tui := tea.NewProgram(

internal/app/services.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package app
33
import (
44
"context"
55
"database/sql"
6-
"log/slog"
76

87
"github.com/kujtimiihoxha/termai/internal/config"
98
"github.com/kujtimiihoxha/termai/internal/db"
@@ -23,8 +22,6 @@ type App struct {
2322
Permissions permission.Service
2423

2524
LSPClients map[string]*lsp.Client
26-
27-
ceanups []func()
2825
}
2926

3027
func New(ctx context.Context, conn *sql.DB) *App {
@@ -44,10 +41,7 @@ func New(ctx context.Context, conn *sql.DB) *App {
4441
}
4542

4643
for name, client := range cfg.LSP {
47-
lspClient, err := lsp.NewClient(client.Command, client.Args...)
48-
app.ceanups = append(app.ceanups, func() {
49-
lspClient.Close()
50-
})
44+
lspClient, err := lsp.NewClient(ctx, client.Command, client.Args...)
5145
workspaceWatcher := watcher.NewWorkspaceWatcher(lspClient)
5246
if err != nil {
5347
logging.Error("Failed to create LSP client for", name, err)
@@ -64,13 +58,3 @@ func New(ctx context.Context, conn *sql.DB) *App {
6458
}
6559
return app
6660
}
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ type Client struct {
4848
openFilesMu sync.RWMutex
4949
}
5050

51-
func NewClient(command string, args ...string) (*Client, error) {
52-
cmd := exec.Command(command, args...)
51+
func NewClient(ctx context.Context, command string, args ...string) (*Client, error) {
52+
cmd := exec.CommandContext(ctx, command, args...)
5353
// Copy env
5454
cmd.Env = os.Environ()
5555

0 commit comments

Comments
 (0)