mirror of
https://github.com/outbackdingo/firezone.git
synced 2026-01-28 10:18:51 +00:00
When a user sends the first packet to a resource, we generate a "connection intent" and consult the portal, which gateway to use for this resource. This process is throttled to only generate a new intent every 2s. Once we know, which gateway to use for a certain resource, we initiate a connection via snownet. This involves an OFFER-ANSWER handshake with the gateway. A connection for which we have sent an offer and have not yet received an answer is what we call a "pending connection". In case the connection setup takes longer than 2s, we will generate another connection intent which can point to the same gateway that we are currently setting up a connection with. Currently, encountering a "pending connection" during another connection setup is treated as an error which results in some state being cleaned-up / removed. This is where the bug surfaces: If we remove the state for a resource as a result of a 2nd connection intent and then receive the response of the first one, we will be left with no state that knows about this resource. We fix this by refactoring `create_or_reuse_connection` to be atomic in regards to its state changes: All checks that fail the function are moved to the top which means there is no state to clean up in case of an error. Additionally, we model the case of a "pending connection" using an `Option` to not flood the logs with "pending connection" warnings as those are expected during normal operation. Fixes: #5385