Skip to content

Commit 7c0d881

Browse files
krisctlPrabhakar Kumar
authored and
Prabhakar Kumar
committed
Terminates MATLAB when stuck in starting state for more than 2 minutes for all licensing options. This previously only occurred with the "Existing License" option.
1 parent 055e519 commit 7c0d881

File tree

1 file changed

+31
-40
lines changed

1 file changed

+31
-40
lines changed

matlab_proxy/app_state.py

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class AppState:
3434

3535
# Constants that are applicable to AppState class
3636
MATLAB_PORT_CHECK_DELAY_IN_SECONDS = 1
37+
# The maximum amount of time in seconds the Embedded Connector can take
38+
# for launching, before the matlab-proxy server concludes that something is wrong.
3739
EMBEDDED_CONNECTOR_MAX_STARTUP_DURATION_IN_SECONDS = 120
3840

3941
def __init__(self, settings):
@@ -82,15 +84,11 @@ def __init__(self, settings):
8284
self.embedded_connector_start_time = None
8385

8486
# Keep track of the state of the Embedded Connector.
85-
# If there is some problem with lauching the Embedded Connector(say an issue with licensing),
87+
# If there is some problem with launching the Embedded Connector(say an issue with licensing),
8688
# the state of MATLAB process in app_state will continue to be in a 'starting' indefinitely.
8789
# This variable can be either "up" or "down"
8890
self.embedded_connector_state = "down"
8991

90-
# The maximum amount of time in seconds the Embedded Connector can take
91-
# for lauching, before the matlab-proxy server concludes that something is wrong.
92-
self.embedded_connector_max_startup_duration = 120
93-
9492
def __get_cached_licensing_file(self):
9593
"""Get the cached licensing file
9694
@@ -773,7 +771,7 @@ async def __track_embedded_connector_state():
773771
"""track_embedded_connector_state is an asyncio task to track the status of MATLAB Embedded Connector.
774772
This task will start and stop with the MATLAB process.
775773
"""
776-
this_task = "track_embedded_connector_state() task"
774+
this_task = "track_embedded_connector_state:"
777775
logger.debug(f"{this_task}: Starting task...")
778776

779777
while True:
@@ -794,57 +792,50 @@ async def __track_embedded_connector_state():
794792
continue
795793

796794
else:
797-
# Compute the time difference
798795
time_diff = time.time() - self.embedded_connector_start_time
799-
if time_diff > self.embedded_connector_max_startup_duration:
800-
# MATLAB has been up but the Embedded Connector is not responding for more than embedded_connector_max_startup_duration seconds.
801-
# Create/raise a generic error
802-
logger.error(
803-
f":{this_task}: MATLAB has been in a 'starting' state for more than {self.embedded_connector_max_startup_duration} seconds!"
804-
)
805-
806-
licensing_error = "Unable to use Existing License to launch MATLAB. Please check if you can successfully launch MATLAB outside of matlab-proxy"
807-
808-
async def __force_stop_matlab():
796+
if (
797+
time_diff
798+
> self.EMBEDDED_CONNECTOR_MAX_STARTUP_DURATION_IN_SECONDS
799+
):
800+
# Since max allowed startup time has elapsed, it means that MATLAB is in a stuck state and cannot be launched.
801+
# Set the error and stop matlab.
802+
user_visible_error = "Unable to start MATLAB.\nTry again by clicking Start MATLAB."
803+
804+
async def __force_stop_matlab(error):
809805
"""A private method to update self.error and force stop matlab"""
810-
self.error = LicensingError(licensing_error)
811-
logger.error(f"{this_task}: {licensing_error}")
806+
self.error = MatlabError(error)
807+
logger.error(f"{this_task}: {error}")
812808

813809
# If force_quit is not set to True, stop_matlab() would try to
814810
# send a HTTP request to the Embedded Connector (which is already "down")
815811
await self.stop_matlab(force_quit=True)
816812

817-
# In WINDOWS systems, errors are raised as UI windows and cannot be captured programmatically.
818-
# So, raise a generic error wherever appropriate
819813
if system.is_windows():
820-
generic_error = f"MATLAB has been in a starting state for more than {int(self.embedded_connector_max_startup_duration)} seconds. Use Windows Remote Desktop to check for any errors"
821-
822-
# If licensing type is existing_license and there are no logs, then it means that MATLAB cannot be launched with an existing license
823-
# Set the error and stop matlab.
824-
if (
825-
self.licensing["type"] == "existing_license"
826-
and len(self.logs["matlab"]) == 0
827-
):
828-
await __force_stop_matlab()
829-
# Breaking out of the loop will end this task as matlab-proxy was unable to launch MATLAB successfully even after embedded_connector_max_startup_duration
814+
# In WINDOWS systems, errors are raised as UI windows and cannot be captured programmatically.
815+
# So, raise a generic error wherever appropriate
816+
generic_error = f"MATLAB did not start in {int(self.EMBEDDED_CONNECTOR_MAX_STARTUP_DURATION_IN_SECONDS)} seconds. Use Windows Remote Desktop to check for any errors."
817+
logger.error(f":{this_task}: {generic_error}")
818+
if len(self.logs["matlab"]) == 0:
819+
await __force_stop_matlab(user_visible_error)
820+
# Breaking out of the loop to end this task as matlab-proxy was unable to launch MATLAB successfully
821+
# even after waiting for EMBEDDED_CONNECTOR_MAX_STARTUP_DURATION_IN_SECONDS
830822
break
831-
832823
else:
833824
# Do not stop the MATLAB process or break from the loop (the error type is unknown)
834825
self.error = MatlabError(generic_error)
835-
logger.error(f"{this_task}: {generic_error}")
836826
await asyncio.sleep(5)
837827
continue
838828

839829
else:
840-
# If licensing type is existing license and then there are no error logs, then MATLAB cannot be launched with the existing license
830+
# If there are no logs after the max startup time has elapsed, it means that MATLAB is in a stuck state and cannot be launched.
841831
# Set the error and stop matlab.
842-
if (
843-
self.licensing["type"] == "existing_license"
844-
and len(self.logs["matlab"]) == 0
845-
):
846-
await __force_stop_matlab()
847-
# Breaking out of the loop will end this task as matlab-proxy was unable to launch MATLAB successfully even after embedded_connector_max_startup_duration
832+
logger.error(
833+
f":{this_task}: MATLAB did not start in {int(self.EMBEDDED_CONNECTOR_MAX_STARTUP_DURATION_IN_SECONDS)} seconds!"
834+
)
835+
if len(self.logs["matlab"]) == 0:
836+
await __force_stop_matlab(user_visible_error)
837+
# Breaking out of the loop to end this task as matlab-proxy was unable to launch MATLAB successfully
838+
# even after waiting for EMBEDDED_CONNECTOR_MAX_STARTUP_DURATION_IN_SECONDS
848839
break
849840

850841
else:

0 commit comments

Comments
 (0)