pkg/signal: move signal.DumpStacks() to a separate package

It is not directly related to signal-handling, so can well live
in its own package.

Also added a variant that doesn't take a directory to write files
to, for easier consumption / better match to how it's used.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-07-15 17:33:55 +02:00
parent b2e31eb416
commit ea5c94cdb9
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
10 changed files with 108 additions and 68 deletions

View file

@ -54,7 +54,7 @@ import (
"github.com/docker/docker/daemon/cluster/controllers/plugin"
executorpkg "github.com/docker/docker/daemon/cluster/executor"
lncluster "github.com/docker/docker/libnetwork/cluster"
"github.com/docker/docker/pkg/signal"
"github.com/docker/docker/pkg/stack"
swarmapi "github.com/docker/swarmkit/api"
swarmnode "github.com/docker/swarmkit/node"
"github.com/pkg/errors"
@ -393,7 +393,7 @@ func (c *Cluster) Cleanup() {
if err := node.Stop(); err != nil {
logrus.Errorf("failed to shut down cluster node: %v", err)
signal.DumpStacks("")
stack.Dump()
}
c.mu.Lock()

View file

@ -13,7 +13,7 @@ import (
"github.com/docker/docker/daemon/cluster/convert"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/opts"
"github.com/docker/docker/pkg/signal"
"github.com/docker/docker/pkg/stack"
swarmapi "github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/manager/encryption"
swarmnode "github.com/docker/swarmkit/node"
@ -399,7 +399,7 @@ func (c *Cluster) Leave(force bool) error {
// release readers in here
if err := nr.Stop(); err != nil {
logrus.Errorf("failed to shut down cluster node: %v", err)
signal.DumpStacks("")
stack.Dump()
return err
}

View file

@ -6,7 +6,7 @@ import (
"os"
"os/signal"
stackdump "github.com/docker/docker/pkg/signal"
"github.com/docker/docker/pkg/stack"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
@ -16,7 +16,7 @@ func (daemon *Daemon) setupDumpStackTrap(root string) {
signal.Notify(c, unix.SIGUSR1)
go func() {
for range c {
path, err := stackdump.DumpStacks(root)
path, err := stack.DumpToFile(root)
if err != nil {
logrus.WithError(err).Error("failed to write goroutines dump")
} else {

View file

@ -5,7 +5,7 @@ import (
"os"
"unsafe"
"github.com/docker/docker/pkg/signal"
"github.com/docker/docker/pkg/stack"
"github.com/sirupsen/logrus"
"golang.org/x/sys/windows"
)
@ -34,7 +34,7 @@ func (daemon *Daemon) setupDumpStackTrap(root string) {
logrus.Debugf("Stackdump - waiting signal at %s", event)
for {
windows.WaitForSingleObject(h, windows.INFINITE)
path, err := signal.DumpStacks(root)
path, err := stack.DumpToFile(root)
if err != nil {
logrus.WithError(err).Error("failed to write goroutines dump")
} else {

View file

@ -9,7 +9,7 @@ import (
"sync/atomic"
"github.com/docker/docker/libnetwork/internal/caller"
stackdump "github.com/docker/docker/pkg/signal"
"github.com/docker/docker/pkg/stack"
"github.com/sirupsen/logrus"
)
@ -169,7 +169,7 @@ func stackTrace(ctx interface{}, w http.ResponseWriter, r *http.Request) {
log := logrus.WithFields(logrus.Fields{"component": "diagnostic", "remoteIP": r.RemoteAddr, "method": caller.Name(0), "url": r.URL.String()})
log.Info("stack trace")
path, err := stackdump.DumpStacks("/tmp/")
path, err := stack.DumpToFile("/tmp/")
if err != nil {
log.WithError(err).Error("failed to write goroutines dump")
HTTPReply(w, FailCommand(err), json) // nolint:errcheck

View file

@ -0,0 +1,8 @@
package signal
import "github.com/docker/docker/pkg/stack"
// DumpStacks appends the runtime stack into file in dir and returns full path
// to that file.
// Deprecated: use github.com/docker/docker/pkg/stack.Dump instead.
var DumpStacks = stack.DumpToFile

View file

@ -4,14 +4,10 @@ import (
"fmt"
"os"
gosignal "os/signal"
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"syscall"
"time"
"github.com/pkg/errors"
"github.com/docker/docker/pkg/stack"
)
// Trap sets up a simplified signal "trap", appropriate for common
@ -58,7 +54,7 @@ func Trap(cleanup func(), logger interface {
logger.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received")
}
case syscall.SIGQUIT:
DumpStacks("")
stack.Dump()
logger.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT")
}
// for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal #
@ -67,38 +63,3 @@ func Trap(cleanup func(), logger interface {
}
}()
}
const stacksLogNameTemplate = "goroutine-stacks-%s.log"
// DumpStacks appends the runtime stack into file in dir and returns full path
// to that file.
func DumpStacks(dir string) (string, error) {
var (
buf []byte
stackSize int
)
bufferLen := 16384
for stackSize == len(buf) {
buf = make([]byte, bufferLen)
stackSize = runtime.Stack(buf, true)
bufferLen *= 2
}
buf = buf[:stackSize]
var f *os.File
if dir != "" {
path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1)))
var err error
f, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return "", errors.Wrap(err, "failed to open file to write the goroutine stacks")
}
defer f.Close()
defer f.Sync()
} else {
f = os.Stderr
}
if _, err := f.Write(buf); err != nil {
return "", errors.Wrap(err, "failed to write goroutine stacks")
}
return f.Name(), nil
}

View file

@ -64,20 +64,3 @@ func TestTrap(t *testing.T) {
}
}
func TestDumpStacks(t *testing.T) {
directory, err := ioutil.TempDir("", "test-dump-tasks")
assert.Check(t, err)
defer os.RemoveAll(directory)
dumpPath, err := DumpStacks(directory)
assert.Check(t, err)
readFile, _ := ioutil.ReadFile(dumpPath)
fileData := string(readFile)
assert.Check(t, is.Contains(fileData, "goroutine"))
}
func TestDumpStacksWithEmptyInput(t *testing.T) {
path, err := DumpStacks("")
assert.Check(t, err)
assert.Check(t, is.Equal(os.Stderr.Name(), path))
}

57
pkg/stack/stackdump.go Normal file
View file

@ -0,0 +1,57 @@
package stack // import "github.com/docker/docker/pkg/stack"
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/pkg/errors"
)
const stacksLogNameTemplate = "goroutine-stacks-%s.log"
// Dump outputs the runtime stack to os.StdErr.
func Dump() {
_ = dump(os.Stderr)
}
// DumpToFile appends the runtime stack into a file named "goroutine-stacks-<timestamp>.log"
// in dir and returns the full path to that file. If no directory name is
// provided, it outputs to os.Stderr.
func DumpToFile(dir string) (string, error) {
var f *os.File
if dir != "" {
path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1)))
var err error
f, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return "", errors.Wrap(err, "failed to open file to write the goroutine stacks")
}
defer f.Close()
defer f.Sync()
} else {
f = os.Stderr
}
return f.Name(), dump(f)
}
func dump(f *os.File) error {
var (
buf []byte
stackSize int
)
bufferLen := 16384
for stackSize == len(buf) {
buf = make([]byte, bufferLen)
stackSize = runtime.Stack(buf, true)
bufferLen *= 2
}
buf = buf[:stackSize]
if _, err := f.Write(buf); err != nil {
return errors.Wrap(err, "failed to write goroutine stacks")
}
return nil
}

View file

@ -0,0 +1,31 @@
package stack // import "github.com/docker/docker/pkg/stack"
import (
"io/ioutil"
"os"
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestDump(t *testing.T) {
Dump()
}
func TestDumpToFile(t *testing.T) {
directory, err := ioutil.TempDir("", "test-dump-tasks")
assert.Check(t, err)
defer os.RemoveAll(directory)
dumpPath, err := DumpToFile(directory)
assert.Check(t, err)
readFile, _ := ioutil.ReadFile(dumpPath)
fileData := string(readFile)
assert.Check(t, is.Contains(fileData, "goroutine"))
}
func TestDumpToFileWithEmptyInput(t *testing.T) {
path, err := DumpToFile("")
assert.Check(t, err)
assert.Check(t, is.Equal(os.Stderr.Name(), path))
}