gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/log/dump_unix.go (about)

     1  // +build darwin dragonfly freebsd linux netbsd openbsd
     2  
     3  package log
     4  
     5  import (
     6  	"os"
     7  	"os/signal"
     8  	"runtime"
     9  	"syscall"
    10  
    11  	"github.com/sirupsen/logrus"
    12  )
    13  
    14  func watchForGoroutinesDump(logger *logrus.Logger, stopCh chan bool) (chan bool, chan bool) {
    15  	dumpedCh := make(chan bool)
    16  	finishedCh := make(chan bool)
    17  
    18  	dumpStacksCh := make(chan os.Signal, 1)
    19  	// On USR1 dump stacks of all go routines
    20  	signal.Notify(dumpStacksCh, syscall.SIGUSR1)
    21  
    22  	go func() {
    23  		for {
    24  			select {
    25  			case <-dumpStacksCh:
    26  				buf := make([]byte, 1<<20)
    27  				len := runtime.Stack(buf, true)
    28  				logger.Printf("=== received SIGUSR1 ===\n*** goroutine dump...\n%s\n*** end\n", buf[0:len])
    29  
    30  				nonBlockingSend(dumpedCh, true)
    31  			case <-stopCh:
    32  				close(finishedCh)
    33  				return
    34  			}
    35  		}
    36  	}()
    37  
    38  	return dumpedCh, finishedCh
    39  }
    40  
    41  func nonBlockingSend(ch chan bool, value bool) {
    42  	select {
    43  	case ch <- value:
    44  	default:
    45  	}
    46  }