Skip to content

Commit a3fb4a3

Browse files
committed
fix(analyzer): decrease load load library timeout and better error messages if time out occurs
1 parent 7b3eb0c commit a3fb4a3

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

packages/robot/src/robotcode/robot/diagnostics/imports_manager.py

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import zlib
1010
from abc import ABC, abstractmethod
1111
from collections import OrderedDict
12-
from concurrent.futures import ProcessPoolExecutor
12+
from concurrent.futures import ProcessPoolExecutor, TimeoutError
1313
from dataclasses import dataclass
1414
from pathlib import Path
1515
from typing import (
@@ -83,9 +83,8 @@
8383
REST_EXTENSIONS = (".rst", ".rest")
8484

8585

86-
LOAD_LIBRARY_TIME_OUT = 30
87-
FIND_FILE_TIME_OUT = 10
88-
COMPLETE_LIBRARY_IMPORT_TIME_OUT = COMPLETE_RESOURCE_IMPORT_TIME_OUT = COMPLETE_VARIABLES_IMPORT_TIME_OUT = 10
86+
LOAD_LIBRARY_TIME_OUT = 10
87+
COMPLETE_LIBRARY_IMPORT_TIME_OUT = COMPLETE_RESOURCE_IMPORT_TIME_OUT = COMPLETE_VARIABLES_IMPORT_TIME_OUT = 5
8988

9089

9190
class _EntryKey:
@@ -1155,6 +1154,7 @@ def _get_library_libdoc(
11551154
meta, _source, ignore_arguments = self.get_library_meta(name, base_dir, variables)
11561155

11571156
if meta is not None and not meta.has_errors:
1157+
11581158
meta_file = Path(self.lib_doc_cache_path, meta.filepath_base + ".meta.json")
11591159
if meta_file.exists():
11601160
try:
@@ -1164,14 +1164,19 @@ def _get_library_libdoc(
11641164
if saved_meta.has_errors:
11651165
self._logger.debug(
11661166
lambda: "Saved library spec for {name}{args!r} is not used "
1167-
"due to errors in meta data"
1167+
"due to errors in meta data",
1168+
context_name="import",
11681169
)
11691170

11701171
if not saved_meta.has_errors and saved_meta == meta:
11711172
spec_path = Path(
11721173
self.lib_doc_cache_path,
11731174
meta.filepath_base + ".spec.json",
11741175
)
1176+
1177+
self._logger.debug(
1178+
lambda: f"Use cached library meta data for {name}", context_name="import"
1179+
)
11751180
return from_json(spec_path.read_text("utf-8"), LibraryDoc)
11761181

11771182
except (SystemExit, KeyboardInterrupt):
@@ -1185,17 +1190,22 @@ def _get_library_libdoc(
11851190
except BaseException as e:
11861191
self._logger.exception(e)
11871192

1193+
self._logger.debug(lambda: f"Load library in process {name}{args!r}", context_name="import")
11881194
executor = ProcessPoolExecutor(max_workers=1, mp_context=mp.get_context("spawn"))
11891195
try:
1190-
result = executor.submit(
1191-
get_library_doc,
1192-
name,
1193-
args if not ignore_arguments else (),
1194-
working_dir,
1195-
base_dir,
1196-
self.get_resolvable_command_line_variables(),
1197-
variables,
1198-
).result(LOAD_LIBRARY_TIME_OUT)
1196+
try:
1197+
result = executor.submit(
1198+
get_library_doc,
1199+
name,
1200+
args if not ignore_arguments else (),
1201+
working_dir,
1202+
base_dir,
1203+
self.get_resolvable_command_line_variables(),
1204+
variables,
1205+
).result(LOAD_LIBRARY_TIME_OUT)
1206+
1207+
except TimeoutError as e:
1208+
raise RuntimeError(f"Timeout loading library {name}({args!r})") from e
11991209

12001210
except (SystemExit, KeyboardInterrupt):
12011211
raise
@@ -1369,15 +1379,20 @@ def _get_variables_libdoc(
13691379

13701380
executor = ProcessPoolExecutor(max_workers=1, mp_context=mp.get_context("spawn"))
13711381
try:
1372-
result = executor.submit(
1373-
get_variables_doc,
1374-
name,
1375-
args,
1376-
working_dir,
1377-
base_dir,
1378-
self.get_resolvable_command_line_variables() if resolve_command_line_vars else None,
1379-
variables,
1380-
).result(LOAD_LIBRARY_TIME_OUT)
1382+
try:
1383+
result = executor.submit(
1384+
get_variables_doc,
1385+
name,
1386+
args,
1387+
working_dir,
1388+
base_dir,
1389+
self.get_resolvable_command_line_variables() if resolve_command_line_vars else None,
1390+
variables,
1391+
).result(LOAD_LIBRARY_TIME_OUT)
1392+
1393+
except TimeoutError as e:
1394+
raise RuntimeError(f"Timeout loading library {name}({args!r})") from e
1395+
13811396
except (SystemExit, KeyboardInterrupt):
13821397
raise
13831398
except BaseException as e:
@@ -1553,7 +1568,7 @@ def complete_library_import(
15531568
base_dir,
15541569
self.get_resolvable_command_line_variables(),
15551570
variables,
1556-
).result(LOAD_LIBRARY_TIME_OUT)
1571+
).result(COMPLETE_LIBRARY_IMPORT_TIME_OUT)
15571572

15581573
def complete_resource_import(
15591574
self,
@@ -1568,7 +1583,7 @@ def complete_resource_import(
15681583
base_dir,
15691584
self.get_resolvable_command_line_variables(),
15701585
variables,
1571-
).result(LOAD_LIBRARY_TIME_OUT)
1586+
).result(COMPLETE_RESOURCE_IMPORT_TIME_OUT)
15721587

15731588
def complete_variables_import(
15741589
self,
@@ -1583,7 +1598,7 @@ def complete_variables_import(
15831598
base_dir,
15841599
self.get_resolvable_command_line_variables(),
15851600
variables,
1586-
).result(LOAD_LIBRARY_TIME_OUT)
1601+
).result(COMPLETE_VARIABLES_IMPORT_TIME_OUT)
15871602

15881603
def resolve_variable(
15891604
self,

0 commit comments

Comments
 (0)