Skip to content

Commit 9a6ff68

Browse files
authored
Merge pull request #42641 from thaJeztah/make_signal_selfcontained
2 parents 627bbd3 + 6ff6913 commit 9a6ff68

14 files changed

Lines changed: 151 additions & 95 deletions

File tree

cmd/dockerd/daemon.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
buildkit "github.com/docker/docker/builder/builder-next"
3333
"github.com/docker/docker/builder/dockerfile"
3434
"github.com/docker/docker/cli/debug"
35+
"github.com/docker/docker/cmd/dockerd/trap"
3536
"github.com/docker/docker/daemon"
3637
"github.com/docker/docker/daemon/cluster"
3738
"github.com/docker/docker/daemon/config"
@@ -44,7 +45,6 @@ import (
4445
"github.com/docker/docker/pkg/jsonmessage"
4546
"github.com/docker/docker/pkg/pidfile"
4647
"github.com/docker/docker/pkg/plugingetter"
47-
"github.com/docker/docker/pkg/signal"
4848
"github.com/docker/docker/pkg/sysinfo"
4949
"github.com/docker/docker/pkg/system"
5050
"github.com/docker/docker/plugin"
@@ -183,7 +183,7 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
183183
stopc := make(chan bool)
184184
defer close(stopc)
185185

186-
signal.Trap(func() {
186+
trap.Trap(func() {
187187
cli.stop()
188188
<-stopc // wait for daemonCli.start() to return
189189
}, logrus.StandardLogger())
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"syscall"
66
"time"
77

8-
"github.com/docker/docker/pkg/signal"
8+
"github.com/docker/docker/cmd/dockerd/trap"
99
"github.com/sirupsen/logrus"
1010
)
1111

@@ -15,7 +15,7 @@ func main() {
1515
"QUIT": syscall.SIGQUIT,
1616
"INT": os.Interrupt,
1717
}
18-
signal.Trap(func() {
18+
trap.Trap(func() {
1919
time.Sleep(time.Second)
2020
os.Exit(99)
2121
}, logrus.StandardLogger())
Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
package signal // import "github.com/docker/docker/pkg/signal"
1+
package trap // import "github.com/docker/docker/cmd/dockerd/trap"
22

33
import (
44
"fmt"
55
"os"
66
gosignal "os/signal"
7-
"path/filepath"
8-
"runtime"
9-
"strings"
107
"sync/atomic"
118
"syscall"
12-
"time"
139

14-
"github.com/pkg/errors"
10+
"github.com/docker/docker/pkg/stack"
1511
)
1612

1713
// Trap sets up a simplified signal "trap", appropriate for common
@@ -58,7 +54,7 @@ func Trap(cleanup func(), logger interface {
5854
logger.Info("Forcing docker daemon shutdown without cleanup; 3 interrupts received")
5955
}
6056
case syscall.SIGQUIT:
61-
DumpStacks("")
57+
stack.Dump()
6258
logger.Info("Forcing docker daemon shutdown without cleanup on SIGQUIT")
6359
}
6460
// for the SIGINT/TERM, and SIGQUIT non-clean shutdown case, exit with 128 + signal #
@@ -67,38 +63,3 @@ func Trap(cleanup func(), logger interface {
6763
}
6864
}()
6965
}
70-
71-
const stacksLogNameTemplate = "goroutine-stacks-%s.log"
72-
73-
// DumpStacks appends the runtime stack into file in dir and returns full path
74-
// to that file.
75-
func DumpStacks(dir string) (string, error) {
76-
var (
77-
buf []byte
78-
stackSize int
79-
)
80-
bufferLen := 16384
81-
for stackSize == len(buf) {
82-
buf = make([]byte, bufferLen)
83-
stackSize = runtime.Stack(buf, true)
84-
bufferLen *= 2
85-
}
86-
buf = buf[:stackSize]
87-
var f *os.File
88-
if dir != "" {
89-
path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1)))
90-
var err error
91-
f, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
92-
if err != nil {
93-
return "", errors.Wrap(err, "failed to open file to write the goroutine stacks")
94-
}
95-
defer f.Close()
96-
defer f.Sync()
97-
} else {
98-
f = os.Stderr
99-
}
100-
if _, err := f.Write(buf); err != nil {
101-
return "", errors.Wrap(err, "failed to write goroutine stacks")
102-
}
103-
return f.Name(), nil
104-
}
Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// +build linux
22

3-
package signal // import "github.com/docker/docker/pkg/signal"
3+
package trap // import "github.com/docker/docker/cmd/dockerd/trap"
44

55
import (
66
"io/ioutil"
@@ -64,20 +64,3 @@ func TestTrap(t *testing.T) {
6464
}
6565

6666
}
67-
68-
func TestDumpStacks(t *testing.T) {
69-
directory, err := ioutil.TempDir("", "test-dump-tasks")
70-
assert.Check(t, err)
71-
defer os.RemoveAll(directory)
72-
dumpPath, err := DumpStacks(directory)
73-
assert.Check(t, err)
74-
readFile, _ := ioutil.ReadFile(dumpPath)
75-
fileData := string(readFile)
76-
assert.Check(t, is.Contains(fileData, "goroutine"))
77-
}
78-
79-
func TestDumpStacksWithEmptyInput(t *testing.T) {
80-
path, err := DumpStacks("")
81-
assert.Check(t, err)
82-
assert.Check(t, is.Equal(os.Stderr.Name(), path))
83-
}

daemon/cluster/cluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import (
5454
"github.com/docker/docker/daemon/cluster/controllers/plugin"
5555
executorpkg "github.com/docker/docker/daemon/cluster/executor"
5656
lncluster "github.com/docker/docker/libnetwork/cluster"
57-
"github.com/docker/docker/pkg/signal"
57+
"github.com/docker/docker/pkg/stack"
5858
swarmapi "github.com/docker/swarmkit/api"
5959
swarmnode "github.com/docker/swarmkit/node"
6060
"github.com/pkg/errors"
@@ -393,7 +393,7 @@ func (c *Cluster) Cleanup() {
393393

394394
if err := node.Stop(); err != nil {
395395
logrus.Errorf("failed to shut down cluster node: %v", err)
396-
signal.DumpStacks("")
396+
stack.Dump()
397397
}
398398

399399
c.mu.Lock()

daemon/cluster/swarm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/docker/docker/daemon/cluster/convert"
1414
"github.com/docker/docker/errdefs"
1515
"github.com/docker/docker/opts"
16-
"github.com/docker/docker/pkg/signal"
16+
"github.com/docker/docker/pkg/stack"
1717
swarmapi "github.com/docker/swarmkit/api"
1818
"github.com/docker/swarmkit/manager/encryption"
1919
swarmnode "github.com/docker/swarmkit/node"
@@ -399,7 +399,7 @@ func (c *Cluster) Leave(force bool) error {
399399
// release readers in here
400400
if err := nr.Stop(); err != nil {
401401
logrus.Errorf("failed to shut down cluster node: %v", err)
402-
signal.DumpStacks("")
402+
stack.Dump()
403403
return err
404404
}
405405

daemon/debugtrap_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"os"
77
"os/signal"
88

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

daemon/debugtrap_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"os"
66
"unsafe"
77

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

libnetwork/diagnostic/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"sync/atomic"
1010

1111
"github.com/docker/docker/libnetwork/internal/caller"
12-
stackdump "github.com/docker/docker/pkg/signal"
12+
"github.com/docker/docker/pkg/stack"
1313
"github.com/sirupsen/logrus"
1414
)
1515

@@ -169,7 +169,7 @@ func stackTrace(ctx interface{}, w http.ResponseWriter, r *http.Request) {
169169
log := logrus.WithFields(logrus.Fields{"component": "diagnostic", "remoteIP": r.RemoteAddr, "method": caller.Name(0), "url": r.URL.String()})
170170
log.Info("stack trace")
171171

172-
path, err := stackdump.DumpStacks("/tmp/")
172+
path, err := stack.DumpToFile("/tmp/")
173173
if err != nil {
174174
log.WithError(err).Error("failed to write goroutines dump")
175175
HTTPReply(w, FailCommand(err), json) // nolint:errcheck

pkg/signal/signal_deprecated.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package signal
2+
3+
import "github.com/docker/docker/pkg/stack"
4+
5+
// DumpStacks appends the runtime stack into file in dir and returns full path
6+
// to that file.
7+
// Deprecated: use github.com/docker/docker/pkg/stack.Dump instead.
8+
var DumpStacks = stack.DumpToFile

0 commit comments

Comments
 (0)