Skip to content

Commit

Permalink
shell: support cancel with double ctrl-c
Browse files Browse the repository at this point in the history
  • Loading branch information
criyle committed Feb 3, 2024
1 parent 010c30f commit cb1256f
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 96 deletions.
18 changes: 15 additions & 3 deletions cmd/go-judge-shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"

"github.com/criyle/go-judge/pb"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
Expand Down Expand Up @@ -107,11 +107,11 @@ func run(sc pb.Executor_ExecStreamClient, args []string) (*pb.Response, error) {
}

// Set stdin in raw mode.
oldState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
defer func() { _ = terminal.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort.
defer func() { _ = term.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort.

// pump msg
sendCh := make(chan *pb.StreamRequest, 64)
Expand All @@ -127,6 +127,7 @@ func run(sc pb.Executor_ExecStreamClient, args []string) (*pb.Response, error) {
}()

// pump stdin
forceQuit := false
go func() {
buf := make([]byte, 4096)
for {
Expand All @@ -142,6 +143,17 @@ func run(sc pb.Executor_ExecStreamClient, args []string) (*pb.Response, error) {
}
continue
}
if n == 1 && buf[0] == 3 {
if forceQuit {
sendCh <- &pb.StreamRequest{
Request: &pb.StreamRequest_ExecCancel{},
}
}
forceQuit = true
} else {
forceQuit = false
}

if err != nil {
log.Println("stdin", err)
return
Expand Down
2 changes: 2 additions & 0 deletions cmd/go-judge/grpc_executor/grpc_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func (sw *streamWrapper) Recv() (*stream.StreamRequest, error) {
X: int(i.ExecResize.X),
Y: int(i.ExecResize.Y),
}}, nil
case *pb.StreamRequest_ExecCancel:
return &stream.StreamRequest{Cancel: &struct{}{}}, nil
}
return nil, errors.ErrUnsupported
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/go-judge/stream/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type StreamRequest struct {
Request *model.Request
Resize *ResizeRequest
Input *InputRequest
Cancel *bool
Cancel *struct{}
}

type StreamResponse struct {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ require (
github.com/prometheus/client_golang v1.18.0
github.com/zsais/go-gin-prometheus v0.1.0
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.18.0
golang.org/x/net v0.20.0
golang.org/x/sync v0.6.0
golang.org/x/sys v0.16.0
golang.org/x/term v0.16.0
google.golang.org/grpc v1.61.0
google.golang.org/protobuf v1.32.0
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -59,7 +59,7 @@ require (
github.com/ugorji/go/codec v1.2.12 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit cb1256f

Please sign in to comment.