Skip to content

Commit

Permalink
lsp: Use Thread instead of Process for Preview Server (#837)
Browse files Browse the repository at this point in the history
* Preview process: Replaced Process with Thread if running on Python 3.12+
* More meaningfull variable name + add type hints
* Fixed types + store the HTTPServer to call shutdown() later
* Removed unused imports
* Removed unused line
* Removed useless type wrapper
* Restored terminate() call if Process does not have kill()
* Removed Process part, fully replaced with Thread
* Added changelog to track the issue
* Updated preview_server docstring
  • Loading branch information
Nitorac authored Sep 23, 2024
1 parent 0ecf59f commit 64c9097
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
1 change: 1 addition & 0 deletions lib/esbonio/changes/790.fix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed the preview server to use a Thread instead of a Process to prevent deadlocks on certain systems by @Nitorac
34 changes: 13 additions & 21 deletions lib/esbonio/esbonio/lsp/sphinx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import typing
import warnings
from functools import partial
from multiprocessing import Process
from multiprocessing import Queue
from http.server import HTTPServer
from threading import Thread
from typing import IO
from typing import Any
from typing import Dict
Expand Down Expand Up @@ -92,9 +92,12 @@ def __init__(self, *args, **kwargs):
self.sphinx_log: Optional[SphinxLogHandler] = None
"""Logging handler for sphinx messages."""

self.preview_process: Optional[Process] = None
self.preview_runnable: Optional[Thread] = None
"""The process hosting the preview server."""

self.preview_server: Optional[HTTPServer] = None
"""The handle of HTTPServer running the preview server."""

self.preview_port: Optional[int] = None
"""The port the preview server is running on."""

Expand Down Expand Up @@ -172,11 +175,8 @@ def _initialize_sphinx(self):
)

def on_shutdown(self, *args):
if self.preview_process:
if not hasattr(self.preview_process, "kill"):
self.preview_process.terminate()
else:
self.preview_process.kill()
if self.preview_runnable and self.preview_server is not None:
self.preview_server.shutdown()

def save(self, params: DidSaveTextDocumentParams):
super().save(params)
Expand Down Expand Up @@ -429,23 +429,15 @@ def preview(self, options: Dict[str, Any]) -> Dict[str, Any]:

return {}

if not self.preview_process and IS_LINUX:
if not self.preview_runnable:
self.logger.debug("Starting preview server.")
server = make_preview_server(self.app.outdir) # type: ignore[arg-type]
self.preview_port = server.server_port
# Remember the HTTPServer object to call shutdown when we want it to end
self.preview_server = server

self.preview_process = Process(target=server.serve_forever, daemon=True)
self.preview_process.start()

if not self.preview_process and not IS_LINUX:
self.logger.debug("Starting preview server")

q: Queue = Queue()
self.preview_process = Process(
target=start_preview_server, args=(q, self.app.outdir), daemon=True
)
self.preview_process.start()
self.preview_port = q.get()
self.preview_runnable = Thread(target=server.serve_forever, daemon=True)
self.preview_runnable.start()

if options.get("show", True):
self.show_document(
Expand Down

0 comments on commit 64c9097

Please sign in to comment.