@@ -59,6 +59,7 @@ interface ActiveConnector {
5959 readonly configKey : string ;
6060 readonly config : RelayManagedEndpointRuntimeConfig ;
6161 readonly startedAtMillis : number ;
62+ readonly connected : Ref . Ref < boolean > ;
6263}
6364
6465// A connector that exits before running this long is treated as part of a
@@ -71,6 +72,11 @@ const RELAY_RESTART_BACKOFF_BASE_MS = 1_000;
7172const RELAY_RESTART_BACKOFF_MAX_MS = 60_000 ;
7273// Newly created tunnels can fail authorization briefly while Cloudflare propagates their token.
7374const TUNNEL_AUTHORIZATION_FAILURES_BEFORE_RECOVERY = 4 ;
75+ // A connector that never registers a connection may hold a token for a tunnel
76+ // that no longer exists, rejected in wording the output check does not know.
77+ // Ask for recovery after this long, and again at this interval while it stays
78+ // unconnected; the relay hands back the same tunnel when it is still live.
79+ const CONNECTOR_REGISTRATION_TIMEOUT = Duration . minutes ( 3 ) ;
7480
7581export function classifyRelayClientOutput ( line : string ) : "connected" | "warning" | "debug" {
7682 if ( / \b R e g i s t e r e d t u n n e l c o n n e c t i o n \b / iu. test ( line ) ) {
@@ -84,14 +90,16 @@ export function classifyRelayClientOutput(line: string): "connected" | "warning"
8490
8591/**
8692 * Cloudflare's edge rejects a connector whose tunnel was deleted or whose
87- * token no longer matches. Current edge output is
88- * `error="Failed to get tunnel"` with no prefix; older edges prefixed the
89- * same messages with `Unauthorized:`. Match both so recovery fires on either.
93+ * token no longer matches. The edge words this differently over time
94+ * (`Failed to get tunnel`, `Tunnel not found`, ...), sometimes prefixed with
95+ * `Unauthorized:`. Treat any `Unauthorized:` registration error as a rejection,
96+ * plus the unprefixed messages seen so far. Transient rejections while a new
97+ * tunnel's token propagates are absorbed by requiring several in a row.
9098 */
9199export function isRejectedRelayClientTunnelOutput ( line : string ) : boolean {
92100 return (
93101 / \b R e g i s t e r t u n n e l e r r o r f r o m s e r v e r s i d e \b / iu. test ( line ) &&
94- / e r r o r = " (?: U n a u t h o r i z e d : \s * ) ? (?: F a i l e d t o g e t t u n n e l | R e c o r d f o r t u n n e l n o t f o u n d | I n v a l i d t u n n e l s e c r e t ) " / iu. test (
102+ / e r r o r = " (?: U n a u t h o r i z e d : [ ^ " ] * | F a i l e d t o g e t t u n n e l | T u n n e l n o t f o u n d | R e c o r d f o r t u n n e l n o t f o u n d | I n v a l i d t u n n e l s e c r e t ) " / iu. test (
95103 line ,
96104 )
97105 ) ;
@@ -220,6 +228,24 @@ export const make = Effect.gen(function* () {
220228 Effect . catchCause ( ( cause ) => Effect . logWarning ( "Relay client supervisor failed" , { cause } ) ) ,
221229 ) ;
222230
231+ // Requests recovery while the connector has not registered a connection,
232+ // once per timeout, until it connects or is replaced.
233+ const watchConnectorRegistration = ( connector : ActiveConnector ) =>
234+ Effect . gen ( function * ( ) {
235+ yield * Effect . sleep ( CONNECTOR_REGISTRATION_TIMEOUT ) ;
236+ if ( yield * Ref . get ( connector . connected ) ) return true ;
237+ yield * Effect . logWarning (
238+ "Relay client has not registered a tunnel connection; requesting recovery" ,
239+ {
240+ pid : Number ( connector . child . pid ) ,
241+ tunnelId : connector . config . tunnelId ,
242+ tunnelName : connector . config . tunnelName ,
243+ } ,
244+ ) ;
245+ yield * Queue . offer ( recoveryRequests , connector . config ) ;
246+ return false ;
247+ } ) . pipe ( Effect . repeat ( { until : ( connected ) => connected } ) ) ;
248+
223249 const observeConnectorOutput = ( connector : ActiveConnector ) => {
224250 let rejectedRegistrations = 0 ;
225251
@@ -239,7 +265,10 @@ export const make = Effect.gen(function* () {
239265 switch ( classifyRelayClientOutput ( line ) ) {
240266 case "connected" :
241267 rejectedRegistrations = 0 ;
242- return Effect . logInfo ( "Relay client tunnel connection registered" , attributes ) . pipe (
268+ return Ref . set ( connector . connected , true ) . pipe (
269+ Effect . andThen (
270+ Effect . logInfo ( "Relay client tunnel connection registered" , attributes ) ,
271+ ) ,
243272 Effect . andThen ( Queue . offer ( tunnelConnections , undefined ) ) ,
244273 Effect . asVoid ,
245274 ) ;
@@ -370,10 +399,12 @@ export const make = Effect.gen(function* () {
370399 configKey : nextConfigKey ,
371400 config,
372401 startedAtMillis : yield * Clock . currentTimeMillis ,
402+ connected : yield * Ref . make ( false ) ,
373403 } satisfies ActiveConnector ;
374404 yield * Ref . set ( activeRef , connector ) ;
375405 yield * Effect . forkIn ( observeConnectorOutput ( connector ) , connectorScope ) ;
376406 yield * Effect . forkIn ( superviseConnector ( connector ) , connectorScope ) ;
407+ yield * Effect . forkIn ( watchConnectorRegistration ( connector ) , connectorScope ) ;
377408 return {
378409 status : "running" ,
379410 providerKind : "cloudflare_tunnel" ,
0 commit comments