Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exec deadlock when emitter is not Typer intf #216

Merged
merged 1 commit into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,46 @@ func (x *executor) Execute(req *Request, re ResponseEmitter, env Environment) er
return err
}
}
maybeStartPostRun := func(formatters PostRunMap) <-chan error {
var (
postRun func(Response, ResponseEmitter) error
postRunCh = make(chan error)
)

// contains the error returned by PostRun
errCh := make(chan error, 1)
if cmd.PostRun != nil {
if typer, ok := re.(interface {
Type() PostRunType
}); ok && cmd.PostRun[typer.Type()] != nil {
var (
res Response
lower = re
)

re, res = NewChanResponsePair(req)
if postRun == nil {
close(postRunCh)
return postRunCh
}

go func() {
defer close(errCh)
errCh <- lower.CloseWithError(cmd.PostRun[typer.Type()](res, lower))
}()
// check if we have a formatter for this emitter type
typer, isTyper := re.(interface {
Type() PostRunType
})
if isTyper &&
formatters[typer.Type()] != nil {
postRun = formatters[typer.Type()]
} else {
close(postRunCh)
return postRunCh
}
} else {
// not using this channel today
close(errCh)

// redirect emitter to us
// and start waiting for emissions
var (
postRes Response
postEmitter = re
)
re, postRes = NewChanResponsePair(req)
go func() {
defer close(postRunCh)
postRunCh <- postEmitter.CloseWithError(postRun(postRes, postEmitter))
}()
return postRunCh
}

postRunCh := maybeStartPostRun(cmd.PostRun)
runCloseErr := re.CloseWithError(cmd.Run(req, re, env))
postCloseErr := <-errCh
postCloseErr := <-postRunCh
switch runCloseErr {
case ErrClosingClosedEmitter, nil:
default:
Expand Down
34 changes: 34 additions & 0 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,37 @@ func TestExecutorError(t *testing.T) {
t.Fatalf("expected error message %q but got: %s", expErr, err)
}
}

func TestExecutorNotTyper(t *testing.T) {
testCmd := &Command{
Run: func(*Request, ResponseEmitter, Environment) error {
return nil
},
PostRun: PostRunMap{
CLI: func(response Response, emitter ResponseEmitter) error { return nil },
},
}
testRoot := &Command{
Subcommands: map[string]*Command{
"test": testCmd,
},
}
req, err := NewRequest(context.Background(), []string{"test"}, nil, nil, nil, testRoot)
if err != nil {
t.Fatal(err)
}

emitter, resp := NewChanResponsePair(req)

x := NewExecutor(testRoot)

err = x.Execute(req, emitter, nil)
if err != nil {
t.Fatal(err)
}

_, err = resp.Next()
if err != io.EOF {
t.Fatalf("expected EOF but got: %s", err)
}
}