Skip to content

Commit 0896be2

Browse files
committed
1 parent 6360101 commit 0896be2

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

src/twisted/internet/task.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -878,34 +878,30 @@ async def main(reactor, username, password):
878878
"""
879879
if _reactor is None:
880880
from twisted.internet import reactor as _reactor
881-
finished = defer.ensureDeferred(main(_reactor, *argv))
882881
codes = [0]
883882

884-
stopping = []
885-
_reactor.addSystemEventTrigger("before", "shutdown", stopping.append, True)
883+
def runMain():
884+
try:
885+
finished = main(_reactor, *argv)
886+
except BaseException as exc:
887+
cbFinish(Failure(exc))
888+
else:
889+
finished.addBoth(cbFinish)
886890

887-
def stop(result, stopReactor):
888-
if stopReactor:
889-
try:
890-
_reactor.stop()
891-
except ReactorNotRunning:
892-
pass
891+
def cbFinish(result):
892+
try:
893+
_reactor.stop()
894+
except ReactorNotRunning:
895+
pass
893896

894897
if isinstance(result, Failure):
895898
if result.check(SystemExit) is not None:
896-
code = result.value.code
899+
codes[0] = result.value.code
897900
else:
898901
log.err(result, "main function encountered error")
899-
code = 1
900-
codes[0] = code
901-
902-
def cbFinish(result):
903-
if stopping:
904-
stop(result, False)
905-
else:
906-
_reactor.callWhenRunning(stop, result, True)
902+
codes[0] = 1
907903

908-
finished.addBoth(cbFinish)
904+
_reactor.callWhenRunning(runMain)
909905
_reactor.run()
910906
sys.exit(codes[0])
911907

0 commit comments

Comments
 (0)