diff options
author | 2021-08-11 11:10:28 +0200 | |
---|---|---|
committer | 2021-08-11 15:15:02 +0200 | |
commit | f656b8434bcb0035902000408d0b59d45eded73a (patch) | |
tree | 78baf8f57dfdcb39c48393a228c38fdb8ff41455 /ui/signal | |
parent | 2c40569db0fe26084e634753586bf5f6b18b36ef (diff) |
Cut the multiproduct_kati -> soong-ui-build dep.
This is done by moving SetupSignals() to its own little package.
There are a number of tiny little utility packages for soong_ui we might
be better of merging, but that's for another change (maybe)
Test: Presubmits.
Change-Id: I07b0ca98bfb8884ef4223d665e632183b9896a0d
Diffstat (limited to 'ui/signal')
-rw-r--r-- | ui/signal/Android.bp | 28 | ||||
-rw-r--r-- | ui/signal/signal.go | 94 |
2 files changed, 122 insertions, 0 deletions
diff --git a/ui/signal/Android.bp b/ui/signal/Android.bp new file mode 100644 index 000000000..08933a130 --- /dev/null +++ b/ui/signal/Android.bp @@ -0,0 +1,28 @@ +// Copyright 2021 Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package { + default_applicable_licenses: ["Android-Apache-2.0"], +} + +bootstrap_go_package { + name: "soong-ui-signal", + pkgPath: "android/soong/ui/signal", + srcs: [ + "signal.go", + ], + deps: [ + "soong-ui-logger", + ], +} diff --git a/ui/signal/signal.go b/ui/signal/signal.go new file mode 100644 index 000000000..4929a7bfe --- /dev/null +++ b/ui/signal/signal.go @@ -0,0 +1,94 @@ +// Copyright 2017 Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package signal + +import ( + "os" + "os/signal" + "runtime/debug" + "syscall" + + "android/soong/ui/logger" + "time" +) + +// SetupSignals sets up signal handling to ensure all of our subprocesses are killed and that +// our log/trace buffers are flushed to disk. +// +// All of our subprocesses are in the same process group, so they'll receive a SIGINT at the +// same time we do. Most of the time this means we just need to ignore the signal and we'll +// just see errors from all of our subprocesses. But in case that fails, when we get a signal: +// +// 1. Wait two seconds to exit normally. +// 2. Call cancel() which is normally the cancellation of a Context. This will send a SIGKILL +// to any subprocesses attached to that context. +// 3. Wait two seconds to exit normally. +// 4. Call cleanup() to close the log/trace buffers, then panic. +// 5. If another two seconds passes (if cleanup got stuck, etc), then panic. +// +func SetupSignals(log logger.Logger, cancel, cleanup func()) { + signals := make(chan os.Signal, 5) + signal.Notify(signals, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM) + go handleSignals(signals, log, cancel, cleanup) +} + +func handleSignals(signals chan os.Signal, log logger.Logger, cancel, cleanup func()) { + var timeouts int + var timeout <-chan time.Time + + handleTimeout := func() { + timeouts += 1 + switch timeouts { + case 1: + // Things didn't exit cleanly, cancel our ctx (SIGKILL to subprocesses) + // Do this asynchronously to ensure it won't block and prevent us from + // taking more drastic measures. + log.Println("Still alive, killing subprocesses...") + go cancel() + case 2: + // Cancel didn't work. Try to run cleanup manually, then we'll panic + // at the next timer whether it finished or not. + log.Println("Still alive, cleaning up...") + + // Get all stacktraces to see what was stuck + debug.SetTraceback("all") + + go func() { + defer log.Panicln("Timed out exiting...") + cleanup() + }() + default: + // In case cleanup() deadlocks, the next tick will panic. + log.Panicln("Got signal, but timed out exiting...") + } + } + + for { + select { + case s := <-signals: + log.Println("Got signal:", s) + + // Another signal triggers our next timeout handler early + if timeout != nil { + handleTimeout() + } + + // Wait 2 seconds for everything to exit cleanly. + timeout = time.Tick(time.Second * 2) + case <-timeout: + handleTimeout() + } + } +} |