Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #583 from bergwolf/subreaper
Browse files Browse the repository at this point in the history
agent: lock subreaper agent thread
  • Loading branch information
lifupan authored Jun 19, 2019
2 parents e40d774 + 47476d4 commit bad7d1b
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,18 @@ func (s *sandbox) listenToUdevEvents() {
}

// This loop is meant to be run inside a separate Go routine.
func (s *sandbox) signalHandlerLoop(sigCh chan os.Signal) {
func (s *sandbox) signalHandlerLoop(sigCh chan os.Signal, errCh chan error) {
// Lock OS thread as subreaper is a thread local capability
// and is not inherited by children created by fork(2) and clone(2).
runtime.LockOSThread()
// Set agent as subreaper
err := unix.Prctl(unix.PR_SET_CHILD_SUBREAPER, uintptr(1), 0, 0, 0)
if err != nil {
errCh <- err
return
}
close(errCh)

for sig := range sigCh {
logger := agentLog.WithField("signal", sig)

Expand Down Expand Up @@ -790,22 +801,16 @@ func (s *sandbox) setupSignalHandler() error {
span, _ := s.trace("setupSignalHandler")
defer span.Finish()

// Set agent as subreaper
err := unix.Prctl(unix.PR_SET_CHILD_SUBREAPER, uintptr(1), 0, 0, 0)
if err != nil {
return err
}

sigCh := make(chan os.Signal, 512)
signal.Notify(sigCh, unix.SIGCHLD)

for _, sig := range handledSignals() {
signal.Notify(sigCh, sig)
}

go s.signalHandlerLoop(sigCh)

return nil
errCh := make(chan error, 1)
go s.signalHandlerLoop(sigCh, errCh)
return <-errCh
}

// getMemory returns a string containing the total amount of memory reported
Expand Down

0 comments on commit bad7d1b

Please sign in to comment.