Giter Site home page Giter Site logo

ramr / go-reaper Goto Github PK

View Code? Open in Web Editor NEW
120.0 2.0 16.0 27 KB

Process (Grim) reaper library for golang - this is useful for cleaning up zombie processes inside docker containers (which do not have an init process running as pid 1).

License: MIT License

Makefile 8.83% Go 91.17%

go-reaper's Introduction

go-reaper

Process (grim) reaper library for golang - this is useful for cleaning up zombie processes inside docker containers (which do not have an init process running as pid 1).

tl;dr

   import reaper "github.com/ramr/go-reaper"

   func main() {
	//  Start background reaping of orphaned child processes.
	go reaper.Reap()

	//  Rest of your code ...

	//  Note: If you also manage processes within your code aka
	//        exec commands or include some code that does do that,
	//        please refer to the section titled
	//        "[Into The Woods]"(https://github.com/ramr/go-reaper#into-the-woods)
   }

How and Why

If you run a container without an init process (pid 1) which would normally reap zombie processes, you could well end up with a lot of zombie processes and eventually exhaust the max process limit on your system.

If you have a golang program that runs as pid 1, then this library allows the golang program to setup a background signal handling mechanism to handle the death of those orphaned children and not create a load of zombies inside the pid namespace your container runs in.

Usage:

For basic usage, see the tl;dr section above. This should be the most commonly used route you'd need to take.

Road Less Traveled

But for those that prefer to go down "the road less traveled", you can control whether to disable pid 1 checks and/or control the options passed to the wait4 (or waitpid) system call by passing configuration to the reaper.

import reaper "github.com/ramr/go-reaper"

func main() {
	config := reaper.Config{
		Pid:              0,
		Options:          0,
		Debug:            true,
		DisablePid1Check: false,
	}

	//  Start background reaping of orphaned child processes.
	go reaper.Start(config)

	//  Rest of your code ...
}

The Pid and Options fields in the configuration are the pid and options passed to the linux wait4 system call.

See the man pages for the wait4 or waitpid system call for details.

Into The Woods

And finally, this part is for those folks that want to go into the woods. This could be required when you need to manage the processes you invoke inside your code (ala with os.exec.Command* or syscall.ForkExec or any variants) or basically include some libraries/code that need to do the same. In such a case, it is better to run the reaper in a separate process as pid 1 and run your code inside a child process. This will still be part of the same code base but just forked off so that the reaper runs inside a different process ...

import "os"
import "syscall"
import reaper "github.com/ramr/go-reaper"

func main() {
	// Use an environment variable REAPER to indicate whether or not
	// we are the child/parent.
	if _, hasReaper := os.LookupEnv("REAPER"); !hasReaper {
		//  Start background reaping of orphaned child processes.
		go reaper.Reap()

		// Note: Optionally add an argument to the end to more
		//       easily distinguish the parent and child in
		//       something like `ps` etc.
		args := os.Args
		// args := append(os.Args, "#kiddo")

		pwd, err := os.Getwd()
		if err != nil {
			// Note: Better to use a default dir ala "/tmp".
			panic(err)
		}

		kidEnv := []string{ fmt.Sprintf("REAPER=%d", os.Getpid()) }

		var wstatus syscall.WaitStatus
		pattrs := &syscall.ProcAttr{
			Dir:   pwd,
			Env:   append(os.Environ(), kidEnv...),
			Sys:   &syscall.SysProcAttr{Setsid: true},
			Files: []uintptr{
				uintptr(syscall.Stdin),
				uintptr(syscall.Stdout),
				uintptr(syscall.Stderr),
			},
		}

		pid, _ := syscall.ForkExec(args[0], args, pattrs)

		// fmt.Printf("kiddo-pid = %d\n", pid)
		_, err = syscall.Wait4(pid, &wstatus, 0, nil)
		for syscall.EINTR == err {
			_, err = syscall.Wait4(pid, &wstatus, 0, nil)
		}

		// If you put this code into a function, then exit here.
		os.Exit(0)
		return
	}

	//  Rest of your code goes here ...

}  /*  End of func  main.  */

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.