Skip to content

Commit f21d928

Browse files
Fix interoperability with Esbonio language server (#163) (#258)
The Esbonio language server runs its own asyncio loop context when it invokes the Sphinx code. This causes the asyncio.run() call to complain since it's already being run in the same thread. Since we're already running a set of threads here using PoolExecutor, we can run one more thread which runs the asyncio.run() context in a separate thread. This mitigates the interoperability issue with the Esbonio language server at the cost of an additional thread, and at the cost of blocking the coroutine loop within the Esbonio language server for the closing of the download set. (See also swyddfa/esbonio#451)
1 parent 6d4b9eb commit f21d928

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

sphinx_immaterial/google_fonts.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def add_google_fonts(app: sphinx.application.Sphinx, fonts: List[str]):
6767
font_dir = os.path.join(static_dir, "fonts")
6868
os.makedirs(font_dir, exist_ok=True)
6969

70-
with concurrent.futures.ThreadPoolExecutor(max_workers=32) as executor:
70+
with concurrent.futures.ThreadPoolExecutor(max_workers=33) as executor:
7171

7272
def to_thread(fn, *args, **kwargs) -> asyncio.Future:
7373
return asyncio.wrap_future(executor.submit(fn, *args, **kwargs))
@@ -166,7 +166,11 @@ async def do_fetch():
166166
css_content = dict(zip(css_future_keys, await asyncio.gather(*css_futures)))
167167
return css_content
168168

169-
css_content = asyncio.run(do_fetch())
169+
# Note: Placing the asyncio.run() into a separate thread mitigates
170+
# issues if we're running in an environment with a loop already
171+
# running (like within the Esbonio language server). Technically
172+
# that'll block that loop, but it's better than causing a crash.
173+
css_content = executor.submit(lambda: asyncio.run(do_fetch())).result()
170174

171175
# Write fonts css file
172176
ttf_font_paths = {}

0 commit comments

Comments
 (0)