Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(vpn/tunnel): cancel updater ticks on tunnel stop #16598

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

Merged
merged 1 commit into from
Feb 19, 2025

Conversation

ethanndickson
Copy link
Member

@ethanndickson ethanndickson commented Feb 18, 2025

Closes coder/coder-desktop-macos#51.

The shutdown process for the VPN using the dylib is as follows:

  • The manager (Swift, C#) sends a stop request.
  • The tunnel receives the request & stops all tailnet goroutines using conn.Close()
  • Once finished, the tunnel sends a stop response
  • The tunnel closes it's speaker's sendCh.
  • The tunnel's serdes sendLoop discovers the sendCh is closed, and exits it's sendLoop, which, in turn closes the file descriptor for writing on the tunnel's side.

We note that during this process, no contexts are cancelled. If we were to cancel contexts, it would introduce a race between the cancellation, and the stop response being sent - it would be possible for the stop response to never get sent. Unfortunately, there's one context in this process that we do need to cancel, and it's the updater struct, whose context protects the sendCh from the ticker for periodically updating the manager with the network state of peers.

As we never cancelled the tunnel context (my bad!), it was possible for the updater to attempt to write to the sendCh after it was closed, causing a panic in the dylib. Since we can't cancel the tunnel context, we'll just cancel the updater context.

This meant stopping the VPN, and then starting it before that timer fire would cause the VPN to fail to start, as the panic would happen on the old process and crash the new VPN instance. If you just stopped the VPN, no error would be reported as the panic wouldn't be noticed once the OS stops caring about the status of the VPN.

This was an issue in two scenarios:

  • Stopping and then quickly starting the VPN via the GUI.
  • When we tried to implement starting and stopping the tunnel on wake & sleep.

This patch fixes both of these scenarios.

Copy link
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@ethanndickson ethanndickson merged commit 2a248b1 into main Feb 19, 2025
57 checks passed
@ethanndickson ethanndickson deleted the ethan/cancel-updater branch February 19, 2025 02:13
@github-actions github-actions bot locked and limited conversation to collaborators Feb 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tunnel can't be stopped and started while the NE is running
2 participants