@@ -443,7 +443,7 @@ async def shutdown_integration_delete(req):
443
443
loop = util .get_event_loop ()
444
444
# Run the current batch of coroutines in the event loop and then exit.
445
445
# This completes the loop.run_forever() blocking call and subsequent code
446
- # in main () resumes execution.
446
+ # in create_and_start_app () resumes execution.
447
447
loop .stop ()
448
448
449
449
return res
@@ -807,8 +807,8 @@ async def cleanup_background_tasks(app):
807
807
await state .stop_matlab (force_quit = True )
808
808
809
809
# Cleanup server tasks
810
- all_tasks = state .server_tasks
811
- await util .cancel_tasks (all_tasks )
810
+ server_tasks = state .server_tasks
811
+ await util .cancel_tasks (server_tasks )
812
812
813
813
814
814
def configure_and_start (app ):
@@ -936,6 +936,11 @@ def configure_no_proxy_in_env():
936
936
937
937
938
938
def create_and_start_app (config_name ):
939
+ """Creates and start the web server. Will block until the server is interrupted or is shut down
940
+
941
+ Args:
942
+ config_name (str): Name of the configuration to use with matlab-proxy.
943
+ """
939
944
configure_no_proxy_in_env ()
940
945
941
946
# Create, configure and start the app.
@@ -947,23 +952,25 @@ def create_and_start_app(config_name):
947
952
# Add signal handlers for the current python process
948
953
loop = util .add_signal_handlers (loop )
949
954
try :
955
+ # Further execution is stopped here until an interrupt is raised
950
956
loop .run_forever ()
957
+
951
958
except SystemExit :
952
959
pass
953
960
954
- async def shutdown ():
955
- """Shuts down the app in the event of a signal interrupt."""
956
- logger .info ("Shutting down MATLAB proxy-app" )
957
-
958
- await app .shutdown ()
959
- await app .cleanup ()
960
-
961
- # Shutdown any running tasks.
962
- await util .cancel_tasks (asyncio .all_tasks ())
963
-
961
+ # After handling the interrupt, proceed with shutting down the server gracefully.
964
962
try :
965
- loop .run_until_complete (shutdown ())
966
- except :
963
+ running_tasks = asyncio .all_tasks (loop )
964
+ loop .run_until_complete (
965
+ asyncio .gather (
966
+ app .shutdown (),
967
+ app .cleanup (),
968
+ util .cancel_tasks (running_tasks ),
969
+ return_exceptions = False ,
970
+ )
971
+ )
972
+
973
+ except Exception :
967
974
pass
968
975
969
976
logger .info ("Finished shutting down. Thank you for using the MATLAB proxy." )
0 commit comments