Skip to content

Commit f7c3ecd

Browse files
authored
Merge pull request #446 from tomoto/fix/recursion-error-in-initialization
fix: catch RecursionError in initialization
2 parents 9a86506 + f00b8ea commit f7c3ecd

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

fortls/langserver.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,14 @@ def workspace_init(self):
14891489
pool.close()
14901490
pool.join()
14911491
for path, result in results.items():
1492-
result_obj = result.get()
1492+
try:
1493+
result_obj = result.get()
1494+
except Exception as e:
1495+
result_obj = (
1496+
"An exception has occured while initialising the workspace.\n"
1497+
f"Exception({(type(e))}): {e}\n"
1498+
+ f"Traceback: {traceback.format_exc()}"
1499+
)
14931500
if isinstance(result_obj, str):
14941501
self.post_message(
14951502
f"Initialization failed for file {path}: {result_obj}"

test/test_server_init.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
import tempfile
3+
4+
import pytest
5+
from setup_tests import Path, run_request, write_rpc_request
6+
7+
from fortls.constants import Severity
8+
9+
10+
@pytest.fixture()
11+
def setup_tmp_file():
12+
levels = 2000
13+
fd, filename = tempfile.mkstemp(suffix=".f90")
14+
try:
15+
with os.fdopen(fd, "w") as tmp:
16+
tmp.write(
17+
"program nested_if\n"
18+
+ str("if (.true.) then\n" * levels)
19+
+ str("end if\n" * levels)
20+
+ "end program nested_if"
21+
)
22+
yield filename
23+
finally:
24+
os.remove(filename)
25+
26+
27+
def test_recursion_error_handling(setup_tmp_file):
28+
root = Path(setup_tmp_file).parent
29+
request_string = write_rpc_request(1, "initialize", {"rootPath": str(root)})
30+
errcode, results = run_request(request_string)
31+
assert errcode == 0
32+
assert results[0]["type"] == Severity.error

0 commit comments

Comments
 (0)