1
- # Copyright 2020-2024 The MathWorks, Inc.
1
+ # Copyright 2020-2025 The MathWorks, Inc.
2
2
3
3
import asyncio
4
4
import contextlib
@@ -436,8 +436,10 @@ async def __update_matlab_state_based_on_endpoint_to_use(
436
436
437
437
else :
438
438
await self .matlab_state_updater_lock .release ()
439
- await self ._update_matlab_state_based_on_ready_file_and_connector_status (
440
- matlab_endpoint_to_use
439
+ await (
440
+ self ._update_matlab_state_based_on_ready_file_and_connector_status (
441
+ matlab_endpoint_to_use
442
+ )
441
443
)
442
444
logger .debug (
443
445
f"{ this_task } : Required processes are ready, Embedded Connector status is '{ self .get_matlab_state ()} '"
@@ -1243,7 +1245,7 @@ async def __update_matlab_port(self, delay: int):
1243
1245
delay (int): time delay in seconds before retrying the file read operation
1244
1246
"""
1245
1247
logger .debug (
1246
- f' updating matlab_port information from { self .matlab_session_files [" matlab_ready_file" ] } '
1248
+ f" updating matlab_port information from { self .matlab_session_files [' matlab_ready_file' ] } "
1247
1249
)
1248
1250
try :
1249
1251
await asyncio .wait_for (
@@ -1415,6 +1417,10 @@ async def stop_matlab(self, force_quit=False):
1415
1417
waiters = []
1416
1418
if matlab is not None :
1417
1419
if system .is_posix () and matlab .returncode is None :
1420
+ # Close the stderr stream to prevent indefinite hanging on it due to a child
1421
+ # process inheriting it, fixes https://github.com/mathworks/matlab-proxy/issues/44
1422
+ self ._close_matlab_stderr_stream (matlab )
1423
+
1418
1424
# Sending an exit request to the embedded connector takes time.
1419
1425
# When MATLAB is in a "starting" state (implies the Embedded connector is not up)
1420
1426
# OR
@@ -1427,23 +1433,9 @@ async def stop_matlab(self, force_quit=False):
1427
1433
else :
1428
1434
logger .debug ("Sending HTTP request to stop the MATLAB process..." )
1429
1435
try :
1430
- import sys
1431
-
1432
1436
# Send HTTP request
1433
1437
await self .__send_stop_request_to_matlab ()
1434
1438
1435
- # Close the stderr stream to prevent indefinite hanging on it due to a child
1436
- # process inheriting it, fixes https://github.com/mathworks/matlab-proxy/issues/44
1437
- stderr_stream = matlab ._transport .get_pipe_transport (
1438
- sys .stderr .fileno ()
1439
- )
1440
- if stderr_stream :
1441
- logger .debug (
1442
- "Closing matlab process stderr stream: %s" ,
1443
- stderr_stream ,
1444
- )
1445
- stderr_stream .close ()
1446
-
1447
1439
# Wait for matlab to shutdown gracefully
1448
1440
await matlab .wait ()
1449
1441
@@ -1461,9 +1453,10 @@ async def stop_matlab(self, force_quit=False):
1461
1453
try :
1462
1454
matlab .terminate ()
1463
1455
await matlab .wait ()
1464
- except :
1465
- pass
1466
-
1456
+ except Exception as ex :
1457
+ logger .debug (
1458
+ "Received an exception while terminating matlab: %s" , ex
1459
+ )
1467
1460
else :
1468
1461
# In a windows system
1469
1462
if system .is_windows () and matlab .is_running ():
@@ -1533,6 +1526,23 @@ async def stop_matlab(self, force_quit=False):
1533
1526
self .matlab_port = None
1534
1527
logger .debug ("Completed Shutdown!!!" )
1535
1528
1529
+ def _close_matlab_stderr_stream (self , matlab ):
1530
+ """
1531
+ This method attempts to close the stderr stream associated with the MATLAB process
1532
+ to prevent potential resource leaks. It logs a debug message if the stream is
1533
+ successfully closed.
1534
+
1535
+ Args:
1536
+ matlab: The MATLAB process reference.
1537
+ """
1538
+ stderr_stream = matlab ._transport .get_pipe_transport (sys .stderr .fileno ())
1539
+ if stderr_stream :
1540
+ logger .debug (
1541
+ "Closing matlab process stderr stream: %s" ,
1542
+ stderr_stream ,
1543
+ )
1544
+ stderr_stream .close ()
1545
+
1536
1546
async def handle_matlab_output (self ):
1537
1547
"""Parse MATLAB output from stdout and raise errors if any."""
1538
1548
matlab = self .processes ["matlab" ]
0 commit comments