From 1302228707d71d1d7674e582aee04c2292d283ec Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Mon, 17 Apr 2017 17:34:02 -0400 Subject: [PATCH 01/10] init Signed-off-by: Daniel Nephin From 5e0b977a6738e75a3410dc209bf85786dad89abc Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Sun, 2 Jul 2017 21:21:40 -0700 Subject: [PATCH 02/10] buildd: graceful shutdown on termination signals Signed-off-by: Tonis Tiigi --- appcontext/appcontext.go | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 appcontext/appcontext.go diff --git a/appcontext/appcontext.go b/appcontext/appcontext.go new file mode 100644 index 00000000..842f659e --- /dev/null +++ b/appcontext/appcontext.go @@ -0,0 +1,42 @@ +package appcontext + +import ( + "context" + "os" + "os/signal" + "sync" + + "github.com/Sirupsen/logrus" + "golang.org/x/sys/unix" +) + +var appContextCache context.Context +var appContextOnce sync.Once + +// Context returns a static context that reacts to termination signals of the +// running process. Useful in CLI tools. +func Context() context.Context { + appContextOnce.Do(func() { + signals := make(chan os.Signal, 2048) + signal.Notify(signals, unix.SIGTERM, unix.SIGINT) + + const exitLimit = 3 + retries := 0 + + ctx, cancel := context.WithCancel(context.Background()) + appContextCache = ctx + + go func() { + for { + <-signals + cancel() + retries++ + if retries >= exitLimit { + logrus.Errorf("got %d SIGTERM/SIGINTs, forcing shutdown", retries) + os.Exit(1) + } + } + }() + }) + return appContextCache +} From 6bca2cb3a16d6e7dd6229932c724cc2d0767f0df Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Wed, 12 Jul 2017 05:08:53 +0000 Subject: [PATCH 03/10] support building on windows (no functional worker yet) Signed-off-by: Akihiro Suda --- appcontext/appcontext.go | 3 +-- appcontext/appcontext_unix.go | 11 +++++++++++ appcontext/appcontext_windows.go | 7 +++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 appcontext/appcontext_unix.go create mode 100644 appcontext/appcontext_windows.go diff --git a/appcontext/appcontext.go b/appcontext/appcontext.go index 842f659e..09e4448e 100644 --- a/appcontext/appcontext.go +++ b/appcontext/appcontext.go @@ -7,7 +7,6 @@ import ( "sync" "github.com/Sirupsen/logrus" - "golang.org/x/sys/unix" ) var appContextCache context.Context @@ -18,7 +17,7 @@ var appContextOnce sync.Once func Context() context.Context { appContextOnce.Do(func() { signals := make(chan os.Signal, 2048) - signal.Notify(signals, unix.SIGTERM, unix.SIGINT) + signal.Notify(signals, terminationSignals...) const exitLimit = 3 retries := 0 diff --git a/appcontext/appcontext_unix.go b/appcontext/appcontext_unix.go new file mode 100644 index 00000000..b586e2f6 --- /dev/null +++ b/appcontext/appcontext_unix.go @@ -0,0 +1,11 @@ +// +build !windows + +package appcontext + +import ( + "os" + + "golang.org/x/sys/unix" +) + +var terminationSignals = []os.Signal{unix.SIGTERM, unix.SIGINT} diff --git a/appcontext/appcontext_windows.go b/appcontext/appcontext_windows.go new file mode 100644 index 00000000..0a8bcbe7 --- /dev/null +++ b/appcontext/appcontext_windows.go @@ -0,0 +1,7 @@ +package appcontext + +import ( + "os" +) + +var terminationSignals = []os.Signal{os.Interrupt} From da67cdc26bd5920b43dac29f85f4b422e31dd862 Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Tue, 18 Jul 2017 18:05:19 -0700 Subject: [PATCH 04/10] Update containerd version Fix for logrus rename, use fork until fixed in moby. Removed unused tar stream. Signed-off-by: Derek McGowan --- appcontext/appcontext.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appcontext/appcontext.go b/appcontext/appcontext.go index 09e4448e..e74da2cd 100644 --- a/appcontext/appcontext.go +++ b/appcontext/appcontext.go @@ -6,7 +6,7 @@ import ( "os/signal" "sync" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) var appContextCache context.Context From 0a5c8458668850608ed1a8a1e7d45e7803a08fb3 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Tue, 8 Jun 2021 20:01:18 -0700 Subject: [PATCH 05/10] add current tracing context detection and exec propagation Signed-off-by: Tonis Tiigi (cherry picked from commit bc9a83144c83e9fd78007b7bfe92e8082c59d40e) --- appcontext/appcontext.go | 7 ++++++- appcontext/register.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 appcontext/register.go diff --git a/appcontext/appcontext.go b/appcontext/appcontext.go index e74da2cd..3c67f923 100644 --- a/appcontext/appcontext.go +++ b/appcontext/appcontext.go @@ -22,7 +22,12 @@ func Context() context.Context { const exitLimit = 3 retries := 0 - ctx, cancel := context.WithCancel(context.Background()) + ctx := context.Background() + for _, f := range inits { + ctx = f(ctx) + } + + ctx, cancel := context.WithCancel(ctx) appContextCache = ctx go func() { diff --git a/appcontext/register.go b/appcontext/register.go new file mode 100644 index 00000000..22f9e836 --- /dev/null +++ b/appcontext/register.go @@ -0,0 +1,14 @@ +package appcontext + +import ( + "context" +) + +type Initializer func(context.Context) context.Context + +var inits []Initializer + +// Register stores a new context initializer that runs on app context creation +func Register(f Initializer) { + inits = append(inits, f) +} From f981fdd1fd91b3ebd9899f79b3e394e091f80c0a Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 28 Oct 2021 13:26:43 +0200 Subject: [PATCH 06/10] go fmt: add //go:build Signed-off-by: CrazyMax --- appcontext/appcontext_unix.go | 1 + 1 file changed, 1 insertion(+) diff --git a/appcontext/appcontext_unix.go b/appcontext/appcontext_unix.go index b586e2f6..366edc68 100644 --- a/appcontext/appcontext_unix.go +++ b/appcontext/appcontext_unix.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package appcontext From 30079635379c70bcf953e974bb6e989bcd050c09 Mon Sep 17 00:00:00 2001 From: coryb Date: Sun, 12 Mar 2023 11:21:27 -0700 Subject: [PATCH 07/10] use bklog.G(ctx) instead of logrus directly Signed-off-by: coryb --- appcontext/appcontext.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/appcontext/appcontext.go b/appcontext/appcontext.go index 3c67f923..f9cf0ba8 100644 --- a/appcontext/appcontext.go +++ b/appcontext/appcontext.go @@ -6,7 +6,7 @@ import ( "os/signal" "sync" - "github.com/sirupsen/logrus" + "github.com/moby/buildkit/util/bklog" ) var appContextCache context.Context @@ -36,7 +36,7 @@ func Context() context.Context { cancel() retries++ if retries >= exitLimit { - logrus.Errorf("got %d SIGTERM/SIGINTs, forcing shutdown", retries) + bklog.G(ctx).Errorf("got %d SIGTERM/SIGINTs, forcing shutdown", retries) os.Exit(1) } } From f66c8d6ce1dbc21267738c7b2cc7d9daeae4c287 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 31 Aug 2023 09:34:09 +0200 Subject: [PATCH 08/10] appcontext: remove log Signed-off-by: CrazyMax --- appcontext/appcontext.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/appcontext/appcontext.go b/appcontext/appcontext.go index f9cf0ba8..7563b136 100644 --- a/appcontext/appcontext.go +++ b/appcontext/appcontext.go @@ -5,8 +5,6 @@ import ( "os" "os/signal" "sync" - - "github.com/moby/buildkit/util/bklog" ) var appContextCache context.Context @@ -36,7 +34,6 @@ func Context() context.Context { cancel() retries++ if retries >= exitLimit { - bklog.G(ctx).Errorf("got %d SIGTERM/SIGINTs, forcing shutdown", retries) os.Exit(1) } } From d7b42cfba0b551b772fbd11cce02b198968a4eab Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 31 Aug 2023 09:37:53 +0200 Subject: [PATCH 09/10] appcontext: init module Signed-off-by: CrazyMax --- Makefile | 2 +- appcontext/go.mod | 5 +++++ appcontext/go.sum | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 appcontext/go.mod create mode 100644 appcontext/go.sum diff --git a/Makefile b/Makefile index 80c6ee9d..04f4ded5 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -PACKAGES ?= mountinfo mount sequential signal symlink +PACKAGES ?= appcontext mountinfo mount sequential signal symlink BINDIR ?= _build/bin CROSS ?= linux/arm linux/arm64 linux/ppc64le linux/s390x \ freebsd/amd64 openbsd/amd64 darwin/amd64 darwin/arm64 windows/amd64 diff --git a/appcontext/go.mod b/appcontext/go.mod new file mode 100644 index 00000000..c72b39af --- /dev/null +++ b/appcontext/go.mod @@ -0,0 +1,5 @@ +module github.com/moby/sys/appcontext + +go 1.17 + +require golang.org/x/sys v0.11.0 diff --git a/appcontext/go.sum b/appcontext/go.sum new file mode 100644 index 00000000..f7ca90d8 --- /dev/null +++ b/appcontext/go.sum @@ -0,0 +1,2 @@ +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= From 392d95243968ab3e6d9c7106d0bffa75885f55e7 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 31 Aug 2023 09:50:28 +0200 Subject: [PATCH 10/10] appcontext: fix lint Signed-off-by: CrazyMax --- appcontext/appcontext.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/appcontext/appcontext.go b/appcontext/appcontext.go index 7563b136..6da5cf5e 100644 --- a/appcontext/appcontext.go +++ b/appcontext/appcontext.go @@ -7,8 +7,10 @@ import ( "sync" ) -var appContextCache context.Context -var appContextOnce sync.Once +var ( + appContextCache context.Context + appContextOnce sync.Once +) // Context returns a static context that reacts to termination signals of the // running process. Useful in CLI tools.