From c830287cb015ffbe7808dacec7e1983da6734cb6 Mon Sep 17 00:00:00 2001 From: Will Owens Date: Sun, 26 Oct 2025 08:58:19 -0500 Subject: [PATCH] bug: fix issues w/ closed connection not closing running process --- backend/localcommand/factory.go | 7 ++++--- backend/localcommand/local_command.go | 5 +++-- server/handlers.go | 4 ++-- server/slave.go | 4 +++- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/backend/localcommand/factory.go b/backend/localcommand/factory.go index 8c15ea3..782bdff 100644 --- a/backend/localcommand/factory.go +++ b/backend/localcommand/factory.go @@ -1,6 +1,7 @@ package localcommand import ( + "context" "syscall" "time" @@ -38,12 +39,12 @@ func (factory *Factory) Name() string { return "local command" } -func (factory *Factory) New(params map[string][]string, conn *websocket.Conn) (server.Slave, error) { +func (factory *Factory) New(ctx context.Context, params map[string][]string, conn *websocket.Conn) (server.Slave, error) { argv := make([]string, len(factory.argv)) copy(argv, factory.argv) - if params["arg"] != nil && len(params["arg"]) > 0 { + if len(params["arg"]) > 0 { argv = append(argv, params["arg"]...) } - return New(factory.command, argv, factory.opts...) + return New(ctx, factory.command, argv, factory.opts...) } diff --git a/backend/localcommand/local_command.go b/backend/localcommand/local_command.go index 4beca86..fa2e64d 100644 --- a/backend/localcommand/local_command.go +++ b/backend/localcommand/local_command.go @@ -1,6 +1,7 @@ package localcommand import ( + "context" "os" "os/exec" "syscall" @@ -28,8 +29,8 @@ type LocalCommand struct { ptyClosed chan struct{} } -func New(command string, argv []string, options ...Option) (*LocalCommand, error) { - cmd := exec.Command(command, argv...) +func New(ctx context.Context, command string, argv []string, options ...Option) (*LocalCommand, error) { + cmd := exec.CommandContext(ctx, command, argv...) pty, err := pty.Start(cmd) if err != nil { diff --git a/server/handlers.go b/server/handlers.go index cd964c9..ae51424 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -72,7 +72,7 @@ func (server *Server) generateHandleWS(ctx context.Context, cancel context.Cance } defer conn.Close() - err = server.processWSConn(ctx, conn) + err = server.processWSConn(r.Context(), conn) switch err { case ctx.Err(): @@ -117,7 +117,7 @@ func (server *Server) processWSConn(ctx context.Context, conn *websocket.Conn) e params := query.Query() var slave Slave - slave, err = server.factory.New(params, conn) + slave, err = server.factory.New(ctx, params, conn) if err != nil { return errors.Wrapf(err, "failed to create backend") } diff --git a/server/slave.go b/server/slave.go index def2cda..3ff76d3 100644 --- a/server/slave.go +++ b/server/slave.go @@ -1,6 +1,8 @@ package server import ( + "context" + "github.com/ghthor/gotty/v2/webtty" "github.com/gorilla/websocket" @@ -15,5 +17,5 @@ type Slave interface { type Factory interface { Name() string - New(params map[string][]string, conn *websocket.Conn) (Slave, error) + New(ctx context.Context, params map[string][]string, conn *websocket.Conn) (Slave, error) }