| OLD | NEW |
| 1 # Copyright 2013 The Swarming Authors. All rights reserved. | 1 # Copyright 2013 The Swarming Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 that | 2 # Use of this source code is governed under the Apache License, Version 2.0 that |
| 3 # can be found in the LICENSE file. | 3 # can be found in the LICENSE file. |
| 4 | 4 |
| 5 """subprocess42 is the answer to life the universe and everything. | 5 """subprocess42 is the answer to life the universe and everything. |
| 6 | 6 |
| 7 It has the particularity of having a Popen implementation that can yield output | 7 It has the particularity of having a Popen implementation that can yield output |
| 8 as it is produced while implementing a timeout and not requiring the use of | 8 as it is produced while implementing a timeout and not requiring the use of |
| 9 worker threads. | 9 worker threads. |
| 10 | 10 |
| (...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 try: | 447 try: |
| 448 yield | 448 yield |
| 449 finally: | 449 finally: |
| 450 for sig, h in previous.iteritems(): | 450 for sig, h in previous.iteritems(): |
| 451 signal.signal(sig, h) | 451 signal.signal(sig, h) |
| 452 | 452 |
| 453 | 453 |
| 454 @contextlib.contextmanager | 454 @contextlib.contextmanager |
| 455 def Popen_with_handler(args, **kwargs): | 455 def Popen_with_handler(args, **kwargs): |
| 456 proc = None | 456 proc = None |
| 457 def handler(_signum, _frame): | 457 def handler(signum, _frame): |
| 458 if proc: | 458 if proc: |
| 459 logging.info('Received signal %d; terminating', signum) |
| 460 # There could be a race condition where the process already exited. Our |
| 461 # subprocess implementation traps this. |
| 459 proc.terminate() | 462 proc.terminate() |
| 460 | 463 |
| 461 with set_signal_handler(STOP_SIGNALS, handler): | 464 with set_signal_handler(STOP_SIGNALS, handler): |
| 462 proc = Popen(args, detached=True, **kwargs) | 465 proc = Popen(args, detached=True, **kwargs) |
| 463 try: | 466 try: |
| 464 yield proc | 467 yield proc |
| 465 finally: | 468 finally: |
| 466 proc.kill() | 469 proc.kill() |
| OLD | NEW |