Skip to content

Commit

Permalink
Fix interoperability with Esbonio language server (#163) (#258)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
tim-nordell-nimbelink authored May 5, 2023
1 parent 6d4b9eb commit f21d928
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions sphinx_immaterial/google_fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def add_google_fonts(app: sphinx.application.Sphinx, fonts: List[str]):
font_dir = os.path.join(static_dir, "fonts")
os.makedirs(font_dir, exist_ok=True)

with concurrent.futures.ThreadPoolExecutor(max_workers=32) as executor:
with concurrent.futures.ThreadPoolExecutor(max_workers=33) as executor:

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

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

# Write fonts css file
ttf_font_paths = {}
Expand Down

0 comments on commit f21d928

Please sign in to comment.