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

Commit

Permalink
agent: lock subreaper agent thread
Browse files Browse the repository at this point in the history
Child subreaper is a thread local property. We need to lock os
thread to make sure the underlying go runtime M thread is not
scheduled off and then quits. Otherwise it is possible that we
lose the subreaper M thread and fail to wait child threads.

Fixes: #582
Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Jun 18, 2019
1 parent f935878 commit 47476d4
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 47476d4

Please sign in to comment.