Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix #790: preview server - Uses Thread instead of Process for Linux #837

Merged
merged 13 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading