Skip to content

Commit

Permalink
fix: Kurtosis shell exec panics if stdin is not terminal (#686)
Browse files Browse the repository at this point in the history
## Description:
<!-- Describe this change, how it works, and the motivation behind it.
-->
Fixes a bug where if `os.Stdin` is not a terminal (for example, if you
pipe a file) during `kurtosis service shell ...`, we call
`terminal.Restore(stdinFd, oldState)` with `oldState` uninitialized.

Repro steps:
```console
$ kurtosis enclave add --name test-enclave
$ kurtosis service add test-enclave test2 httpd --ports http=80
$ kurtosis service shell test-enclave test2 --exec "echo 1" < /dev/null
```

Causing the following error:
```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x18a0854]

goroutine 1 [running]:
golang.org/x/term.restore(...)
	/go/pkg/mod/golang.org/x/term@v0.6.0/term_unix.go:57
golang.org/x/term.Restore(...)
	/go/pkg/mod/golang.org/x/term@v0.6.0/term.go:45
golang.org/x/crypto/ssh/terminal.Restore(0xc0001d1dd0?, 0x0?)
	/go/pkg/mod/golang.org/x/crypto@v0.7.0/ssh/terminal/terminal.go:64 +0x14
github.com/kurtosis-tech/kurtosis/cli/cli/commands/service/shell.run({0x20e01a0, 0xc00032a420}, {0x20f6388, 0xc000361420}, {0x1?, 0xa?}, {0xc0000cbaa0?, 0xc0000cba70?}, 0xc0000cbad0?, 0xc000360b60)
	/root/project/cli/cli/commands/service/shell/shell.go:147 +0x68a
github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/highlevel/engine_consuming_kurtosis_command.(*EngineConsumingKurtosisCommand).getRunFunc.func1({0x20e01a0, 0xc00032a420}, 0x0?, 0xc000[13](https://github.com/victorcolombo/sei-chain/actions/runs/5201948897/jobs/9382840002?pr=1#step:8:14)9100?)
	/root/project/cli/cli/command_framework/highlevel/engine_consuming_kurtosis_command/engine_consuming_kurtosis_command.go:2[19](https://github.com/victorcolombo/sei-chain/actions/runs/5201948897/jobs/9382840002?pr=1#step:8:20) +0x224
github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel.(*LowlevelKurtosisCommand).MustGetCobraCommand.func2(0xc000334c00?, {0xc000139100, 0x2, 0x4})
	/root/project/cli/cli/command_framework/lowlevel/lowlevel_kurtosis_command.go:294 +0x44e
github.com/spf13/cobra.(*Command).execute(0xc000334c00, {0xc0001390c0, 0x4, 0x4})
	/go/pkg/mod/github.com/spf13/cobra@v1.6.1-0.[20](https://github.com/victorcolombo/sei-chain/actions/runs/5201948897/jobs/9382840002?pr=1#step:8:21)230225[21](https://github.com/victorcolombo/sei-chain/actions/runs/5201948897/jobs/9382840002?pr=1#step:8:22)3037-567ea8ebc9b4/command.go:940 +0x862
github.com/spf13/cobra.(*Command).ExecuteC(0x30bc[22](https://github.com/victorcolombo/sei-chain/actions/runs/5201948897/jobs/9382840002?pr=1#step:8:23)0)
	/go/pkg/mod/github.com/spf13/cobra@v1.6.1-0.20[23](https://github.com/victorcolombo/sei-chain/actions/runs/5201948897/jobs/9382840002?pr=1#step:8:24)02[25](https://github.com/victorcolombo/sei-chain/actions/runs/5201948897/jobs/9382840002?pr=1#step:8:26)21[30](https://github.com/victorcolombo/sei-chain/actions/runs/5201948897/jobs/9382840002?pr=1#step:8:31)37-567ea8ebc9b4/command.go:1068 +0x3bd
github.com/spf13/cobra.(*Command).Execute(...)
	/go/pkg/mod/github.com/spf13/cobra@v1.6.1-0.20230225213037-567ea8ebc9b4/command.go:992
main.main()
	/root/project/cli/cli/main.go:52 +0x7d
```

This is *blocking*, given that Github actions does not use a terminal as
stdin:
https://github.com/victorcolombo/sei-chain/actions/runs/5201948897/jobs/9382840002

## Is this change user facing?
NO
<!-- If yes, please add the "user facing" label to the PR -->
<!-- If yes, don't forget to include docs changes where relevant -->

## References (if applicable):
<!-- Add relevant Github Issues, Discord threads, or other helpful
information. -->
  • Loading branch information
victorcolombo committed Jun 7, 2023
1 parent 68a1ad1 commit 5fad486
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .circleci/config.yml
Expand Up @@ -687,6 +687,11 @@ jobs:
- run: "${KURTOSIS_BINPATH} service add test-enclave test1 httpd --ports http=80"
- run: "${KURTOSIS_BINPATH} service add test-enclave test2 httpd --ports http=80"

# Shell
# Emulates GitHub Actions
- run: "${KURTOSIS_BINPATH} service shell test-enclave test1 --exec 'echo 1' < /dev/null > /dev/null"
- run: "${KURTOSIS_BINPATH} service shell test-enclave test1 --exec 'echo 1'"

# File commands
- run: "${KURTOSIS_BINPATH} files rendertemplate test-enclave << pipeline.parameters.rendertemplate-cli-test-template-relative-path >> << pipeline.parameters.rendertemplate-cli-test-data-json-relative-path >> ./rendered --name rendered-file"
- run: "${KURTOSIS_BINPATH} files download test-enclave rendered-file ."
Expand Down
4 changes: 1 addition & 3 deletions cli/cli/commands/service/shell/shell.go
Expand Up @@ -142,9 +142,7 @@ func run(
defer terminal.Restore(stdinFd, oldState)
}

_ = <-finishChan

terminal.Restore(stdinFd, oldState)
<-finishChan

return nil
}

0 comments on commit 5fad486

Please sign in to comment.