blob: 6643e2ffb8e697b5a5aad1cff3ffa6adc363bc2c [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
15package build
16
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//
Dan Willemsen9af5fb92017-02-14 12:40:56 -080034// 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.
40//
Dan Willemsen1e704462016-08-21 15:17:17 -070041func SetupSignals(log logger.Logger, cancel, cleanup func()) {
42 signals := make(chan os.Signal, 5)
Dan Willemsen9af5fb92017-02-14 12:40:56 -080043 signal.Notify(signals, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM)
Dan Willemsen1e704462016-08-21 15:17:17 -070044 go handleSignals(signals, log, cancel, cleanup)
45}
46
47func handleSignals(signals chan os.Signal, log logger.Logger, cancel, cleanup func()) {
Dan Willemsen9af5fb92017-02-14 12:40:56 -080048 var timeouts int
49 var timeout <-chan time.Time
Dan Willemsen1e704462016-08-21 15:17:17 -070050
Dan Willemsen9af5fb92017-02-14 12:40:56 -080051 handleTimeout := func() {
52 timeouts += 1
53 switch timeouts {
54 case 1:
55 // Things didn't exit cleanly, cancel our ctx (SIGKILL to subprocesses)
56 // Do this asynchronously to ensure it won't block and prevent us from
57 // taking more drastic measures.
58 log.Println("Still alive, killing subprocesses...")
59 go cancel()
60 case 2:
61 // Cancel didn't work. Try to run cleanup manually, then we'll panic
62 // at the next timer whether it finished or not.
63 log.Println("Still alive, cleaning up...")
64
65 // Get all stacktraces to see what was stuck
66 debug.SetTraceback("all")
67
68 go func() {
69 defer log.Panicln("Timed out exiting...")
70 cleanup()
71 }()
72 default:
73 // In case cleanup() deadlocks, the next tick will panic.
74 log.Panicln("Got signal, but timed out exiting...")
75 }
76 }
Dan Willemsen1e704462016-08-21 15:17:17 -070077
78 for {
Dan Willemsen9af5fb92017-02-14 12:40:56 -080079 select {
80 case s := <-signals:
Dan Willemsen1e704462016-08-21 15:17:17 -070081 log.Println("Got signal:", s)
Dan Willemsen9af5fb92017-02-14 12:40:56 -080082
83 // Another signal triggers our next timeout handler early
84 if timeout != nil {
85 handleTimeout()
86 }
87
88 // Wait 2 seconds for everything to exit cleanly.
89 timeout = time.Tick(time.Second * 2)
90 case <-timeout:
91 handleTimeout()
Dan Willemsen1e704462016-08-21 15:17:17 -070092 }
93 }
94}