blob: 552545d8bd9e120e7cc24330800b194a75881edf [file] [log] [blame]
Dan Willemsen1e704462016-08-21 15:17:17 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Lukacs T. Berkif656b842021-08-11 11:10:28 +020015package signal
Dan Willemsen1e704462016-08-21 15:17:17 -070016
17import (
18 "os"
19 "os/signal"
20 "runtime/debug"
21 "syscall"
22
23 "android/soong/ui/logger"
Dan Willemsen9af5fb92017-02-14 12:40:56 -080024 "time"
Dan Willemsen1e704462016-08-21 15:17:17 -070025)
26
Dan Willemsen9af5fb92017-02-14 12:40:56 -080027// SetupSignals sets up signal handling to ensure all of our subprocesses are killed and that
28// our log/trace buffers are flushed to disk.
Dan Willemsen1e704462016-08-21 15:17:17 -070029//
Dan Willemsen9af5fb92017-02-14 12:40:56 -080030// All of our subprocesses are in the same process group, so they'll receive a SIGINT at the
31// same time we do. Most of the time this means we just need to ignore the signal and we'll
32// just see errors from all of our subprocesses. But in case that fails, when we get a signal:
Dan Willemsen1e704462016-08-21 15:17:17 -070033//
Colin Crossd079e0b2022-08-16 10:27:33 -070034// 1. Wait two seconds to exit normally.
35// 2. Call cancel() which is normally the cancellation of a Context. This will send a SIGKILL
36// to any subprocesses attached to that context.
37// 3. Wait two seconds to exit normally.
38// 4. Call cleanup() to close the log/trace buffers, then panic.
39// 5. If another two seconds passes (if cleanup got stuck, etc), then panic.
Dan Willemsen1e704462016-08-21 15:17:17 -070040func SetupSignals(log logger.Logger, cancel, cleanup func()) {
41 signals := make(chan os.Signal, 5)
Dan Willemsen9af5fb92017-02-14 12:40:56 -080042 signal.Notify(signals, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM)
Dan Willemsen1e704462016-08-21 15:17:17 -070043 go handleSignals(signals, log, cancel, cleanup)
44}
45
46func handleSignals(signals chan os.Signal, log logger.Logger, cancel, cleanup func()) {
Dan Willemsen9af5fb92017-02-14 12:40:56 -080047 var timeouts int
48 var timeout <-chan time.Time
Dan Willemsen1e704462016-08-21 15:17:17 -070049
Dan Willemsen9af5fb92017-02-14 12:40:56 -080050 handleTimeout := func() {
51 timeouts += 1
52 switch timeouts {
53 case 1:
54 // Things didn't exit cleanly, cancel our ctx (SIGKILL to subprocesses)
55 // Do this asynchronously to ensure it won't block and prevent us from
56 // taking more drastic measures.
57 log.Println("Still alive, killing subprocesses...")
58 go cancel()
59 case 2:
60 // Cancel didn't work. Try to run cleanup manually, then we'll panic
61 // at the next timer whether it finished or not.
62 log.Println("Still alive, cleaning up...")
63
64 // Get all stacktraces to see what was stuck
65 debug.SetTraceback("all")
66
67 go func() {
68 defer log.Panicln("Timed out exiting...")
69 cleanup()
70 }()
71 default:
72 // In case cleanup() deadlocks, the next tick will panic.
73 log.Panicln("Got signal, but timed out exiting...")
74 }
75 }
Dan Willemsen1e704462016-08-21 15:17:17 -070076
77 for {
Dan Willemsen9af5fb92017-02-14 12:40:56 -080078 select {
79 case s := <-signals:
Dan Willemsen1e704462016-08-21 15:17:17 -070080 log.Println("Got signal:", s)
Dan Willemsen9af5fb92017-02-14 12:40:56 -080081
82 // Another signal triggers our next timeout handler early
83 if timeout != nil {
84 handleTimeout()
85 }
86
87 // Wait 2 seconds for everything to exit cleanly.
88 timeout = time.Tick(time.Second * 2)
89 case <-timeout:
90 handleTimeout()
Dan Willemsen1e704462016-08-21 15:17:17 -070091 }
92 }
93}