Skip to content

Commit

Permalink
fix!: handle socket name collisions (#125)
Browse files Browse the repository at this point in the history
This change adds a fix to address socket name collisions, as well as the following changes:
* Sockets are now created in the system temp directory.
* Added daemon bootstrap logging (via a new --log-file option only exposed in the _serve command) so that if the daemon bootstrap fails, we are able to look at the logs and determine what went wrong. Daemon bootstrap logging stops logging to the file after bootstrap is complete.
* Improved the --connection-file CLI help text
* Removed the worker path component from configuration file paths
* Add support for configuring connection data via environment variables. Currently, there is only one env var needed, OPENJD_ADAPTOR_SOCKET.
* The <adaptor> daemon start command has been updated to emit an openjd_env: OPENJD_ADAPTOR_SOCKET=<path-to-socket> line to stdout so that, when running in an OpenJD environment, the option is automatically set and subsequent commands within this OpenJD environment no longer need to pass in a --connection-file argument.
* The --connection-file argument is no longer necessary if OPENJD_ADAPTOR_SOCKET is provided
    * The <adaptor> daemon start command will still generate a connection file at a temporary directory when it is not provided, then delete it after daemon bootstrap is complete.

Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com>
  • Loading branch information
jericht committed Jun 3, 2024
1 parent eb15fa1 commit a123717
Show file tree
Hide file tree
Showing 24 changed files with 1,412 additions and 721 deletions.
26 changes: 20 additions & 6 deletions src/openjd/adaptor_runtime/_background/backend_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
import logging
import os
import signal
from pathlib import Path
from threading import Thread, Event
import traceback
from types import FrameType
from typing import Optional, Union
from typing import Callable, List, Optional, Union

from .server_response import ServerResponseGenerator
from .._osname import OSName
from ..adaptors import AdaptorRunner
from .._http import SocketDirectories
from .._http import SocketPaths
from .._utils import secure_open

if OSName.is_posix():
Expand All @@ -36,12 +38,13 @@ class BackendRunner:
def __init__(
self,
adaptor_runner: AdaptorRunner,
connection_file_path: str,
*,
connection_file_path: Path,
log_buffer: LogBuffer | None = None,
) -> None:
self._adaptor_runner = adaptor_runner
self._connection_file_path = connection_file_path

self._log_buffer = log_buffer
self._server: Optional[Union[BackgroundHTTPServer, WinBackgroundNamedPipeServer]] = None
signal.signal(signal.SIGINT, self._sigint_handler)
Expand All @@ -68,7 +71,7 @@ def _sigint_handler(self, signum: int, frame: Optional[FrameType]) -> None:
self._server, self._adaptor_runner._cancel, force_immediate=True
)

def run(self) -> None:
def run(self, *, on_connection_file_written: List[Callable[[], None]] | None = None) -> None:
"""
Runs the backend logic for background mode.
Expand All @@ -79,8 +82,9 @@ def run(self) -> None:
shutdown_event: Event = Event()

if OSName.is_posix(): # pragma: is-windows
server_path = SocketDirectories.for_os().get_process_socket_path(
"runtime", create_dir=True
server_path = SocketPaths.for_os().get_process_socket_path(
".openjd_adaptor_runtime",
create_dir=True,
)
else: # pragma: is-posix
server_path = NamedPipeHelper.generate_pipe_name("AdaptorNamedPipe")
Expand Down Expand Up @@ -123,6 +127,16 @@ def run(self) -> None:
_logger.info("Shutting down server...")
shutdown_event.set()
raise
except Exception as e:
_logger.critical(f"Unexpected error occurred when writing to connection file: {e}")
_logger.critical(traceback.format_exc())
_logger.info("Shutting down server")
shutdown_event.set()
else:
if on_connection_file_written:
callbacks = list(on_connection_file_written)
for cb in callbacks:
cb()
finally:
# Block until the shutdown_event is set
shutdown_event.wait()
Expand Down
Loading

0 comments on commit a123717

Please sign in to comment.