@@ -443,7 +443,7 @@ async def shutdown_integration_delete(req):
443443 loop = util .get_event_loop ()
444444 # Run the current batch of coroutines in the event loop and then exit.
445445 # This completes the loop.run_forever() blocking call and subsequent code
446- # in main () resumes execution.
446+ # in create_and_start_app () resumes execution.
447447 loop .stop ()
448448
449449 return res
@@ -807,8 +807,8 @@ async def cleanup_background_tasks(app):
807807 await state .stop_matlab (force_quit = True )
808808
809809 # 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 )
812812
813813
814814def configure_and_start (app ):
@@ -936,6 +936,11 @@ def configure_no_proxy_in_env():
936936
937937
938938def 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+ """
939944 configure_no_proxy_in_env ()
940945
941946 # Create, configure and start the app.
@@ -947,23 +952,25 @@ def create_and_start_app(config_name):
947952 # Add signal handlers for the current python process
948953 loop = util .add_signal_handlers (loop )
949954 try :
955+ # Further execution is stopped here until an interrupt is raised
950956 loop .run_forever ()
957+
951958 except SystemExit :
952959 pass
953960
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.
964962 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 :
967974 pass
968975
969976 logger .info ("Finished shutting down. Thank you for using the MATLAB proxy." )
0 commit comments