Skip to content

improved tunnel cleanup#706

Draft
kcoopermiller wants to merge 3 commits into
mainfrom
improvement/tunnel-cleanup
Draft

improved tunnel cleanup#706
kcoopermiller wants to merge 3 commits into
mainfrom
improvement/tunnel-cleanup

Conversation

@kcoopermiller
Copy link
Copy Markdown
Member

@kcoopermiller kcoopermiller commented Jun 2, 2026

Note

Low Risk
Small defensive lifecycle change reusing existing sync_stop; atexit may run extra HTTP deletes on abrupt exit but does not alter normal start/stop behavior.

Overview
Adds an atexit backstop so tunnels that were started but never stopped still run cleanup when the process exits.

After a successful start(), each Tunnel is tracked in a module-level _active_tunnels set. On normal shutdown paths, sync_stop and _cleanup remove the instance from that set. At process exit, _stop_active_tunnels iterates any remaining entries and calls sync_stop() (errors ignored), which should tear down frpc, delete the backend registration, and remove the config file—reducing leaked registrations when cleanup is skipped due to crashes or forgotten stop().

Reviewed by Cursor Bugbot for commit 7784d73. Bugbot is set up for automated code reviews on this repo. Configure here.

Comment thread packages/prime-tunnel/src/prime_tunnel/tunnel.py Outdated
@kcoopermiller
Copy link
Copy Markdown
Member Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7784d73bc1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/prime-tunnel/src/prime_tunnel/tunnel.py
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 7784d73. Configure here.


async def _cleanup(self) -> None:
"""Clean up tunnel resources."""
_active_tunnels.discard(self)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Premature discard defeats atexit backstop on cancellation

Low Severity

_active_tunnels.discard(self) at the top of _cleanup() removes the tunnel from the atexit tracking set before any actual cleanup runs. If the async cleanup is interrupted by CancelledError at an await point (e.g., delete_tunnel or client.close), the error escapes the except Exception handlers (since CancelledError is a BaseException), skipping remaining cleanup steps. Because the tunnel was already discarded, the atexit backstop in _stop_active_tunnels can never retry the incomplete cleanup — defeating its stated purpose of catching "forgotten cleanup" and "unhandled exception" scenarios. Moving the discard to the end of _cleanup() would let the atexit handler pick up partially-cleaned tunnels via sync_stop.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 7784d73. Configure here.

@kcoopermiller kcoopermiller marked this pull request as draft June 2, 2026 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant