mirror of
https://github.com/yudai/gotty.git
synced 2026-03-02 20:00:17 +01:00
bug: fix issues w/ closed connection not closing running process
This commit is contained in:
parent
71b9db14e2
commit
c830287cb0
4 changed files with 12 additions and 8 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package localcommand
|
package localcommand
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -38,12 +39,12 @@ func (factory *Factory) Name() string {
|
||||||
return "local command"
|
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))
|
argv := make([]string, len(factory.argv))
|
||||||
copy(argv, factory.argv)
|
copy(argv, factory.argv)
|
||||||
if params["arg"] != nil && len(params["arg"]) > 0 {
|
if len(params["arg"]) > 0 {
|
||||||
argv = append(argv, params["arg"]...)
|
argv = append(argv, params["arg"]...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return New(factory.command, argv, factory.opts...)
|
return New(ctx, factory.command, argv, factory.opts...)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package localcommand
|
package localcommand
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
@ -28,8 +29,8 @@ type LocalCommand struct {
|
||||||
ptyClosed chan struct{}
|
ptyClosed chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(command string, argv []string, options ...Option) (*LocalCommand, error) {
|
func New(ctx context.Context, command string, argv []string, options ...Option) (*LocalCommand, error) {
|
||||||
cmd := exec.Command(command, argv...)
|
cmd := exec.CommandContext(ctx, command, argv...)
|
||||||
|
|
||||||
pty, err := pty.Start(cmd)
|
pty, err := pty.Start(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@ func (server *Server) generateHandleWS(ctx context.Context, cancel context.Cance
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
err = server.processWSConn(ctx, conn)
|
err = server.processWSConn(r.Context(), conn)
|
||||||
|
|
||||||
switch err {
|
switch err {
|
||||||
case ctx.Err():
|
case ctx.Err():
|
||||||
|
|
@ -117,7 +117,7 @@ func (server *Server) processWSConn(ctx context.Context, conn *websocket.Conn) e
|
||||||
params := query.Query()
|
params := query.Query()
|
||||||
|
|
||||||
var slave Slave
|
var slave Slave
|
||||||
slave, err = server.factory.New(params, conn)
|
slave, err = server.factory.New(ctx, params, conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "failed to create backend")
|
return errors.Wrapf(err, "failed to create backend")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"github.com/ghthor/gotty/v2/webtty"
|
"github.com/ghthor/gotty/v2/webtty"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
|
@ -15,5 +17,5 @@ type Slave interface {
|
||||||
|
|
||||||
type Factory interface {
|
type Factory interface {
|
||||||
Name() string
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue