Skip to content

Commit b08036a

Browse files
diningPhilosopher64Prabhakar Kumar
authored and
Prabhakar Kumar
committed
Fixes asyncio errors on shutdown.
1 parent 8a0f294 commit b08036a

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

Diff for: .gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ __pycache__/
44
build/
55
dist/
66
.venv*/
7-
.vscode/
87
htmlcov/
98
.coverage
109
*.key

Diff for: .vscode/launch.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2024 The MathWorks, Inc.
2+
{
3+
"version": "0.2.0",
4+
"configurations": [
5+
{
6+
"name": "Debug matlab-proxy server",
7+
"type": "debugpy",
8+
"request": "launch",
9+
// Always start debugging from the root file of the project
10+
"program": "${workspaceFolder}/matlab_proxy/app.py",
11+
"console": "integratedTerminal",
12+
"justMyCode": false
13+
}
14+
]
15+
}

Diff for: matlab_proxy/app.py

+22-15
Original file line numberDiff line numberDiff line change
@@ -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

814814
def configure_and_start(app):
@@ -936,6 +936,11 @@ def configure_no_proxy_in_env():
936936

937937

938938
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+
"""
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

Comments
 (0)