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

Use Go's cross-platform process killing instead of the UNIX-only syscall.Kill #161

Merged
merged 1 commit into from
Jun 25, 2018

Conversation

hsluoyz
Copy link
Contributor

@hsluoyz hsluoyz commented Jun 21, 2018

The system call syscall.Kill(pid, syscall.SIGKILL) is not available on Windows. So I replace it with a more cross-platform way which supports all OSes. Now Unik can be compiled on Windows.

GOROOT=C:\Go #gosetup
GOPATH=C:\GoPath #gosetup
C:\Go\bin\go.exe build -i -o C:\Users\xxx\AppData\Local\Temp\___go_build_unik_go.exe C:/GoPath/src/github.com/solo-io/unik/unik.go #gosetup
"C:\Program Files\JetBrains\GoLand 2018.1.5\bin\runnerw.exe" C:\Users\xxx\AppData\Local\Temp\___go_build_unik_go.exe #gosetup
Unik is a tool for compiling application source code
into bootable disk images. Unik also runs and manages unikernel
instances across infrastructures.

Create a client configuration file with 'unik target'

You may set a custom client configuration file
with the global flag --client-config=<path>

Usage:
  unik [command]

Available Commands:
  attach-volume     Attach a volume to a stopped instance
  build             Build a unikernel image from source code files
  compilers         List available unikernel compilers
  configure         A generate configuration file for daemon ('daemon.yaml')
  create-volume     Create a unik-managed data volume
  daemon            Runs the unik backend (daemon)
  delete-image      Delete a unikernel image
  delete-instance   Delete a unikernel instance
  delete-volume     Delete a unikernel volume
  describe-compiler Describe compiler usage
  describe-image    Get image info as a Json string
  describe-instance Get instance info as a Json string
  detach-volume     Detach an attached volume from a stopped instance
  images            List available unikernel images
  instances         List pending/running/stopped unik instances
  login             Log in to a Unik Repository to pull & push images
  logs              retrieve the logs (stdout) of a unikernel instance
  providers         List available unikernel providers
  pull              Pull an image from a Unik Image Repository
  push              Push an image to a Unik Image Repository
  remote-delete     Deleted a pushed image from a Unik Hub Repository
  run               Run a unikernel instance from a compiled image
  search            Search available images in the targeted Unik Image Repository
  start             Start a stopped unikernel instance
  stop              Stop a running unikernel instance
  target            Configure unik daemon URL for cli client commands
  volumes           List available unik-managed volumes

Flags:
      --client-config="/.unik/client-config.yaml": client config file
  -h, --help[=false]: help for unik
      --host="": <string, optional>: host/ip address of the host running the unik daemon
      --hub-config="/.unik/hub-config.yaml": hub config file

Use "unik [command] --help" for more information about a command.

Process finished with exit code 0

logrus.Warn("failed finding instance, assuming instance has externally terminated", err)
}

if err := process.Signal(syscall.SIGKILL); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'll get a panic here if process == nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing it out. I rechecked it. In Golang, the process will never be nil if err == nil, you can check the three implementations for windows, unix and plan9 here: https://github.com/golang/go/search?q=findProcess%28pid+int%29&unscoped_q=findProcess%28pid+int%29

And Golang also officially assume it in its test:

https://github.com/golang/go/blob/516e6f6d5d83dc3dcee6403fab25d5954bbf3f62/src/net/http/cgi/posix_test.go#L14-L20

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but there's no early return here; you check if err != nil, log a warning and then continue (in which case process == nil)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have fixed it in the new commit. Please review.

logrus.Warn("failed finding instance, assuming instance has externally terminated", err)
}

if err := process.Signal(syscall.SIGKILL); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above

@hsluoyz hsluoyz force-pushed the master branch 2 times, most recently from 192b555 to 990c28b Compare June 22, 2018 01:06
if err := syscall.Kill(pid, syscall.SIGKILL); err != nil {
process, err := os.FindProcess(pid)
if err != nil {
return errors.New("failed to find process", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't necessarily want to return here; we just want to throw a warning and move on (for example if the process was stopped externally). we still want to reach the final line return p.state.RemoveInstance(instance)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the PR. Please review.

@ilackarms
Copy link
Member

looks good! thanks for another contribution.

@ilackarms ilackarms merged commit c5c126a into solo-io:master Jun 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants