From 0896be2b95d4c47e59a6fc0c2519f721d999a28c Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Wed, 3 Mar 2021 11:16:22 -0500 Subject: [PATCH] add react change https://github.com/python-trio/sniffio/pull/10#issuecomment-572566364 --- src/twisted/internet/task.py | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/twisted/internet/task.py b/src/twisted/internet/task.py index 69a35d62df8..e8c51893fe4 100644 --- a/src/twisted/internet/task.py +++ b/src/twisted/internet/task.py @@ -878,34 +878,30 @@ async def main(reactor, username, password): """ if _reactor is None: from twisted.internet import reactor as _reactor - finished = defer.ensureDeferred(main(_reactor, *argv)) codes = [0] - stopping = [] - _reactor.addSystemEventTrigger("before", "shutdown", stopping.append, True) + def runMain(): + try: + finished = main(_reactor, *argv) + except BaseException as exc: + cbFinish(Failure(exc)) + else: + finished.addBoth(cbFinish) - def stop(result, stopReactor): - if stopReactor: - try: - _reactor.stop() - except ReactorNotRunning: - pass + def cbFinish(result): + try: + _reactor.stop() + except ReactorNotRunning: + pass if isinstance(result, Failure): if result.check(SystemExit) is not None: - code = result.value.code + codes[0] = result.value.code else: log.err(result, "main function encountered error") - code = 1 - codes[0] = code - - def cbFinish(result): - if stopping: - stop(result, False) - else: - _reactor.callWhenRunning(stop, result, True) + codes[0] = 1 - finished.addBoth(cbFinish) + _reactor.callWhenRunning(runMain) _reactor.run() sys.exit(codes[0])