signal.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. package signal
  15. import (
  16. "os"
  17. "os/signal"
  18. "runtime/debug"
  19. "syscall"
  20. "android/soong/ui/logger"
  21. "time"
  22. )
  23. // SetupSignals sets up signal handling to ensure all of our subprocesses are killed and that
  24. // our log/trace buffers are flushed to disk.
  25. //
  26. // All of our subprocesses are in the same process group, so they'll receive a SIGINT at the
  27. // same time we do. Most of the time this means we just need to ignore the signal and we'll
  28. // just see errors from all of our subprocesses. But in case that fails, when we get a signal:
  29. //
  30. // 1. Wait two seconds to exit normally.
  31. // 2. Call cancel() which is normally the cancellation of a Context. This will send a SIGKILL
  32. // to any subprocesses attached to that context.
  33. // 3. Wait two seconds to exit normally.
  34. // 4. Call cleanup() to close the log/trace buffers, then panic.
  35. // 5. If another two seconds passes (if cleanup got stuck, etc), then panic.
  36. func SetupSignals(log logger.Logger, cancel, cleanup func()) {
  37. signals := make(chan os.Signal, 5)
  38. signal.Notify(signals, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM)
  39. go handleSignals(signals, log, cancel, cleanup)
  40. }
  41. func handleSignals(signals chan os.Signal, log logger.Logger, cancel, cleanup func()) {
  42. var timeouts int
  43. var timeout <-chan time.Time
  44. handleTimeout := func() {
  45. timeouts += 1
  46. switch timeouts {
  47. case 1:
  48. // Things didn't exit cleanly, cancel our ctx (SIGKILL to subprocesses)
  49. // Do this asynchronously to ensure it won't block and prevent us from
  50. // taking more drastic measures.
  51. log.Println("Still alive, killing subprocesses...")
  52. go cancel()
  53. case 2:
  54. // Cancel didn't work. Try to run cleanup manually, then we'll panic
  55. // at the next timer whether it finished or not.
  56. log.Println("Still alive, cleaning up...")
  57. // Get all stacktraces to see what was stuck
  58. debug.SetTraceback("all")
  59. go func() {
  60. defer log.Panicln("Timed out exiting...")
  61. cleanup()
  62. }()
  63. default:
  64. // In case cleanup() deadlocks, the next tick will panic.
  65. log.Panicln("Got signal, but timed out exiting...")
  66. }
  67. }
  68. for {
  69. select {
  70. case s := <-signals:
  71. log.Println("Got signal:", s)
  72. // Another signal triggers our next timeout handler early
  73. if timeout != nil {
  74. handleTimeout()
  75. }
  76. // Wait 2 seconds for everything to exit cleanly.
  77. timeout = time.Tick(time.Second * 2)
  78. case <-timeout:
  79. handleTimeout()
  80. }
  81. }
  82. }