Skip to content

Commit

Permalink
Add RemotePdb service
Browse files Browse the repository at this point in the history
  • Loading branch information
isra17 committed Jul 25, 2023
1 parent 85ea99b commit 2957042
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
3 changes: 3 additions & 0 deletions mypy_stubs/remote_pdb/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class RemotePdb:
def __init__(self, host: str, port: int) -> None: ...
def set_trace(self) -> None: ...
4 changes: 2 additions & 2 deletions src/saturn_engine/utils/asyncutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ def __get__(self, instance: t.Any, owner: t.Any) -> t.Any:
async def _get_attribute(self, instance: t.Any) -> T:
future = instance.__dict__.get(self._name)
if future is None:
future: asyncio.Future[T] = asyncio.Future()
future = asyncio.Future()
instance.__dict__[self._name] = future

async def wrapper():
async def wrapper() -> None:
try:
value = await self.__wrapped__(instance)
future.set_result(value)
Expand Down
41 changes: 41 additions & 0 deletions src/saturn_engine/worker/services/remote_pdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import typing as t

import dataclasses
import signal
import threading

from saturn_engine.utils.log import getLogger
from saturn_engine.worker.services import BaseServices
from saturn_engine.worker.services import Service


@dataclasses.dataclass
class Options:
host: str = "localhost"
port: int = 31337
signal: int = signal.SIGUSR2


class RemotePdb(Service[BaseServices, Options]):
name = "remote_pdb"

Options = Options

def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
super().__init__(*args, **kwargs)
self.logger = getLogger(__name__, self)

async def open(self) -> None:
try:
pass

if threading.current_thread() != threading.main_thread():
raise ValueError("Must be on main thread")
signal.signal(self.options.signal, self.remote_debug_signal)
except Exception:
self.logger.exception("Failed to init remote pdb")

def remote_debug_signal(self, *args: t.Any) -> None:
from remote_pdb import RemotePdb

RemotePdb(self.options.host, self.options.port).set_trace()

0 comments on commit 2957042

Please sign in to comment.