Skip to content
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
6 changes: 5 additions & 1 deletion src/deno_sandbox/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ async def _listener(self) -> None:
del self._pending_processes[stream_id]

except ConnectionClosed:
pass
# Cancel all pending requests when connection closes
for future in self._pending_requests.values():
if not future.done():
future.cancel()
self._pending_requests.clear()
except Exception as e:
for future in self._pending_requests.values():
if not future.done():
Expand Down
17 changes: 15 additions & 2 deletions src/deno_sandbox/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def __init__(
self.returncode = None
self._rpc = rpc
self._stdout_task = _stdout_task
self._stderr_task: Optional[asyncio.Task] = None
self._stderr_task = _stderr_task

@classmethod
async def create(
Expand All @@ -253,14 +253,21 @@ async def wait(self) -> ChildProcessStatus:
)

async def kill(self) -> None:
"""Kill the process."""
"""Kill the process and cancel all associated tasks."""

try:
await self._rpc.call("processKill", {"pid": self.pid})
except ProcessAlreadyExited:
# Process already exited
pass

# Cancel all associated tasks
self._wait_task.cancel()
if self._stdout_task is not None:
self._stdout_task.cancel()
if self._stderr_task is not None:
self._stderr_task.cancel()

async def __aenter__(self):
return self

Expand Down Expand Up @@ -397,6 +404,12 @@ async def create(
)
return p

async def kill(self) -> None:
"""Kill the process and cancel all associated tasks."""
if self._listening_task is not None:
self._listening_task.cancel()
await super().kill()

async def wait_http_ready(self) -> bool:
"""Whether the Deno process is ready to accept HTTP requests."""

Expand Down