fix(vpn/tunnel): cancel updater ticks on tunnel stop #16598
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes coder/coder-desktop-macos#51.
The shutdown process for the VPN using the dylib is as follows:
conn.Close()
sendCh
.serdes
sendLoop
discovers thesendCh
is closed, and exits it'ssendLoop
, 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 thesendCh
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 thesendCh
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:
This patch fixes both of these scenarios.