Home | History | Annotate | Download | only in build
      1 // 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 
     15 package build
     16 
     17 import (
     18 	"os"
     19 	"os/signal"
     20 	"runtime/debug"
     21 	"syscall"
     22 
     23 	"android/soong/ui/logger"
     24 	"time"
     25 )
     26 
     27 // SetupSignals sets up signal handling to ensure all of our subprocesses are killed and that
     28 // our log/trace buffers are flushed to disk.
     29 //
     30 // 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:
     33 //
     34 //   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 //
     41 func SetupSignals(log logger.Logger, cancel, cleanup func()) {
     42 	signals := make(chan os.Signal, 5)
     43 	signal.Notify(signals, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM)
     44 	go handleSignals(signals, log, cancel, cleanup)
     45 }
     46 
     47 func handleSignals(signals chan os.Signal, log logger.Logger, cancel, cleanup func()) {
     48 	var timeouts int
     49 	var timeout <-chan time.Time
     50 
     51 	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 	}
     77 
     78 	for {
     79 		select {
     80 		case s := <-signals:
     81 			log.Println("Got signal:", s)
     82 
     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()
     92 		}
     93 	}
     94 }
     95