on main help sign in
refactor: use context for automatic lsp process cleanup
commitbd2cb4b5b998602537ded61d1d8eae95f955d2df authorNacho Vazquez <nacho@naxo.dev> dateSat 12 Apr 2025, 12:43 (2 years ago) parent6fe85c6
3 files changed+3−20
cmd/root.go−1 internal/app/services.go+1−17 internal/lsp/client.go+2−2
cmd/root.go−1
@@ -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+1−17
@@ -3,7 +3,6 @@ package app
33import (
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
3027func 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+2−2
@@ -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