-
Notifications
You must be signed in to change notification settings - Fork 44
improved tunnel cleanup #706
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import asyncio | ||
| import atexit | ||
| import fcntl | ||
| import os | ||
| import re | ||
|
|
@@ -29,6 +30,22 @@ | |
| ) | ||
| _ANSI_RE = re.compile(r"\x1b\[[0-9;]*m") | ||
|
|
||
| # Started-but-not-stopped tunnels, so an atexit backstop can delete their backend | ||
| # registrations if the caller never stops them (forgotten cleanup, unhandled | ||
| # exception) | ||
| _active_tunnels: "set[Tunnel]" = set() | ||
|
|
||
|
|
||
| def _stop_active_tunnels() -> None: | ||
| for tunnel in list(_active_tunnels): | ||
| try: | ||
| tunnel.sync_stop() | ||
| except Exception: | ||
| pass | ||
|
|
||
|
|
||
| atexit.register(_stop_active_tunnels) | ||
|
|
||
|
|
||
| def _parse_frpc_error( | ||
| output_lines: list[str], | ||
|
|
@@ -205,6 +222,8 @@ async def start(self) -> str: | |
|
|
||
| self._started = True | ||
|
|
||
| _active_tunnels.add(self) | ||
|
|
||
| return self.url | ||
|
|
||
| async def stop(self) -> None: | ||
|
|
@@ -220,6 +239,8 @@ def sync_stop(self) -> None: | |
| if not self._started: | ||
| return | ||
|
|
||
| _active_tunnels.discard(self) | ||
|
|
||
| if self._process is not None: | ||
| try: | ||
| self._process.terminate() | ||
|
|
@@ -258,6 +279,8 @@ def sync_stop(self) -> None: | |
|
|
||
| async def _cleanup(self) -> None: | ||
| """Clean up tunnel resources.""" | ||
| _active_tunnels.discard(self) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Premature discard defeats atexit backstop on cancellationLow Severity
Reviewed by Cursor Bugbot for commit 7784d73. Configure here. |
||
|
|
||
| # Stop frpc process (this will cause drain threads to exit via EOF) | ||
| if self._process is not None: | ||
| try: | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.