Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/runtime/relay/relay-control-client-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ export type RelayControlClientOptions = {
createSocket?: (url: string, relayJwt: string) => WebSocket
connectDeadlineMs?: number
silenceLimitMs?: number
probeIntervalMs?: number
// Test seam: deterministic probe jitter.
livenessRandom?: () => number
}
162 changes: 161 additions & 1 deletion src/main/runtime/relay/relay-control-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,32 @@ class FakeControlSocket extends EventEmitter {
this.close(1006)
}

pings = 0

ping(): void {
if (this.readyState !== 1) {
throw new Error('socket_not_open')
}
this.pings += 1
}

/** The RFC 6455 reply a live peer owes any ping, delivered out of band. */
pong(): void {
this.emit('pong')
}

deliver(message: ControlFrame): void {
this.emit('message', JSON.stringify(message), false)
}
}

function scriptedControl(options: { closeWithAck?: boolean; issuedAtOffsetMs?: number } = {}): {
function scriptedControl(
options: {
closeWithAck?: boolean
issuedAtOffsetMs?: number
livenessRandom?: () => number
} = {}
): {
client: RelayControlClient
socket: FakeControlSocket
onConnectionOpen: ReturnType<typeof vi.fn>
Expand Down Expand Up @@ -492,6 +512,8 @@ function scriptedControl(options: { closeWithAck?: boolean; issuedAtOffsetMs?: n
const onConnectionOpen = vi.fn()
const client = new RelayControlClient({
cellUrl: origin,
// Midpoint random => no jitter, so probe boundaries are exact in tests.
livenessRandom: options.livenessRandom ?? (() => 0.5),
relayJwt: 'scoped-token',
relayHostId,
assignmentEpoch: 3,
Expand Down Expand Up @@ -631,3 +653,141 @@ describe('RelayControlClient scripted-socket lifecycle', () => {
expect(onClose).toHaveBeenCalledWith(MOBILE_RELAY_CLOSE_CODE.BAD_OUTER_CREDENTIAL)
})
})

// STA-7672: a Windows desktop behind NAT/VPN (or resuming from sleep) can hold a
// half-open control socket that send() writes into happily while nothing comes
// back. Every pairing request then failed at its 10s deadline against a socket
// the 75s silence watchdog would not reap for another minute-plus.
describe('RelayControlClient half-open recovery', () => {
afterEach(() => {
vi.useRealTimers()
})

it('tears down only after a run of unanswered probes, not the first one', async () => {
vi.useFakeTimers()
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
const { client, socket, onClose } = scriptedControl()
await client.connect()
const invite = client.createInvite('device-1').catch((error: Error) => error.message)

await vi.advanceTimersByTimeAsync(10_000)

// The request deadline alone must not close the control — a close would have
// rejected as relay_control_closed_<code> instead.
expect(await invite).toBe('relay_control_request_timeout')
expect(socket.pings).toBe(1)
expect(socket.readyState).toBe(1)

// One unanswered probe is UNKNOWN, not death (STA-3320): a lone swallowed
// pong is routine on exactly the VPN/cellular paths this detection targets.
await vi.advanceTimersByTimeAsync(8_000)
expect(socket.pings).toBe(2)
expect(socket.readyState).toBe(1)
await vi.advanceTimersByTimeAsync(8_000)
expect(socket.pings).toBe(3)
expect(socket.readyState).toBe(1)

// Third consecutive miss is evidence.
await vi.advanceTimersByTimeAsync(8_000)
expect(socket.readyState).toBe(3)
expect(onClose).toHaveBeenCalledWith(1006)
expect(client.isLive()).toBe(false)
// Named in the log so a fleet-wide false positive would be visible.
expect(warn).toHaveBeenCalledWith(expect.stringContaining('reason=probe-unanswered'))
warn.mockRestore()
})

it('retires the whole probe run on a single pong', async () => {
vi.useFakeTimers()
const { client, socket, onClose } = scriptedControl()
await client.connect()
const invite = client.createInvite('device-1').catch((error: Error) => error.message)

await vi.advanceTimersByTimeAsync(10_000)
await invite
await vi.advanceTimersByTimeAsync(8_000)
expect(socket.pings).toBe(2)
socket.pong()

// A later probe run must start from zero, not inherit the earlier miss.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This comment names a property the test never exercises: the test never starts a second probe run, so a clearProbe() that cleared the timer but left missedProbes intact would still pass. That mutation is harmless anyway because armProbe() zeroes missedProbes on every run entry (relay-control-liveness.ts:108), which is exactly why the clearProbe reset is unobservable — so either drop the comment or add a second request that re-arms and shows the count restarts at zero.

await vi.advanceTimersByTimeAsync(40_000)
expect(socket.readyState).toBe(1)
expect(onClose).not.toHaveBeenCalled()
expect(client.isLive()).toBe(true)
})

it("clears an armed probe on the relay's next ping, with no pong involved", async () => {
vi.useFakeTimers()
const { client, socket, onClose } = scriptedControl()
await client.connect()
const invite = client.createInvite('device-1').catch((error: Error) => error.message)

await vi.advanceTimersByTimeAsync(10_000)
expect(socket.pings).toBe(1)
await invite

// The probe run (3 x 8s) outlasts the relay's 15s ping cadence on purpose:
// relay liveness runs at the application layer, so a middlebox that swallows
// RFC 6455 control frames must not be able to make this a reconnect loop.
await vi.advanceTimersByTimeAsync(15_000)
socket.deliver({ type: 'ping', t: Date.now() })
await vi.advanceTimersByTimeAsync(40_000)

expect(socket.readyState).toBe(1)
expect(onClose).not.toHaveBeenCalled()
expect(client.isLive()).toBe(true)
})

it('does not probe a control that kept talking while a request went unanswered', async () => {
vi.useFakeTimers()
const { client, socket } = scriptedControl()
await client.connect()
const invite = client.createInvite('device-1').catch((error: Error) => error.message)

await vi.advanceTimersByTimeAsync(5_000)
socket.deliver({ type: 'ping', t: Date.now() })
await vi.advanceTimersByTimeAsync(5_000)

// A reply running past its deadline under relay DB load is not a dead
// socket; tearing this control down would strand every phone on the cell.
expect(await invite).toBe('relay_control_request_timeout')
expect(socket.pings).toBe(0)
expect(client.isLive()).toBe(true)
})

it('spreads probe deadlines so one slow cell cannot synchronize a cohort', async () => {
vi.useFakeTimers()
// Earliest jitter (-10%) fires at 7.2s; the unjittered boundary is 8s.
const { client, socket } = scriptedControl({ livenessRandom: () => 0 })
await client.connect()
void client.createInvite('device-1').catch(() => undefined)

await vi.advanceTimersByTimeAsync(10_000)
expect(socket.pings).toBe(1)
await vi.advanceTimersByTimeAsync(7_300)
expect(socket.pings).toBe(2)
})

it('logs the cell and the silence without altering the rejection', async () => {
vi.useFakeTimers()
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
const { client } = scriptedControl()
await client.connect()
const invite = client.createInvite('device-1').catch((error: Error) => error.message)

await vi.advanceTimersByTimeAsync(10_000)

// The message is a classification key: mobile-relay-mint-failure.ts matches
// it against an anchored /^relay_[a-z0-9_]{1,74}$/, so a diagnostic suffix
// silently downgrades this to the generic relay_mint_failed fallback.
expect(await invite).toBe('relay_control_request_timeout')

const logged = warn.mock.calls.map((call) => String(call[0])).join('\n')
expect(logged).toContain('reqKind=invite')
expect(logged).toContain('cell=http://relay.test')
expect(logged).toContain('socketAgeMs=10000')
expect(logged).toContain('sinceInboundMs=10000')
expect(logged).toContain('probe=armed')
warn.mockRestore()
})
})
43 changes: 29 additions & 14 deletions src/main/runtime/relay/relay-control-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import {
import { RelayControlRequests } from './relay-control-requests'
import type { DeviceCredentialInstallAuthorization } from './relay-control-requests'
import { answerRelayHostChallenge } from './relay-host-proof'
import {
RELAY_CONTROL_SILENCE_LIMIT_MS,
RelayControlSilenceWatchdog
} from './relay-control-silence-watchdog'
import { RelayControlLiveness } from './relay-control-liveness'
import type { RelayControlLivenessTeardown } from './relay-control-liveness'
import { closeRelayControlSocket } from './relay-control-socket-close'
import { controlWebSocketUrl } from './relay-control-url'

Expand All @@ -40,18 +38,26 @@ export class RelayControlClient {
private connectResolve: ((ack: RelayHostHelloAckMessage) => void) | null = null
private connectReject: ((error: Error) => void) | null = null
private connectTimer: ReturnType<typeof setTimeout> | null = null
private readonly silenceWatchdog: RelayControlSilenceWatchdog
private readonly liveness: RelayControlLiveness

constructor(options: RelayControlClientOptions) {
this.options = options
this.requests = new RelayControlRequests(options.onPendingChanged)
this.requests = new RelayControlRequests(options.onPendingChanged, (timeout) => {
this.liveness.noteRequestTimeout(timeout, this.isLive())
})
const endpoint = controlWebSocketUrl(options.cellUrl)
this.relayOrigin = endpoint.origin
this.controlUrl = endpoint.url
this.silenceWatchdog = new RelayControlSilenceWatchdog(
options.silenceLimitMs ?? RELAY_CONTROL_SILENCE_LIMIT_MS,
() => this.socket?.terminate()
)
this.liveness = new RelayControlLiveness({
cellUrl: this.relayOrigin,
ping: () => this.socket?.ping(),
onDead: (reason) => this.terminateForLiveness(reason),
...(options.silenceLimitMs !== undefined ? { silenceLimitMs: options.silenceLimitMs } : {}),
...(options.probeIntervalMs !== undefined
? { probeIntervalMs: options.probeIntervalMs }
: {}),
...(options.livenessRandom !== undefined ? { random: options.livenessRandom } : {})
})
this.createSocket =
options.createSocket ??
((url, token) =>
Expand All @@ -70,8 +76,9 @@ export class RelayControlClient {
const socket = this.createSocket(this.controlUrl, this.options.relayJwt)
this.socket = socket
socket.once('open', () => this.sendHostHello())
socket.on('pong', () => this.liveness.notePong())
socket.on('message', (raw, isBinary) => {
this.silenceWatchdog.noteInbound()
this.liveness.noteInbound()
if (isBinary) {
this.failProtocol('binary control message')
return
Expand Down Expand Up @@ -154,7 +161,7 @@ export class RelayControlClient {
closeNow(hostCloseReason?: RelayHostCloseReason): void {
const wasConnecting = this.state === 'opening' || this.state === 'proving'
this.state = 'closed'
this.silenceWatchdog.stop()
this.liveness.stop()
if (wasConnecting) {
this.connectReject?.(new Error('relay_control_closed'))
this.clearConnectPromise()
Expand Down Expand Up @@ -264,11 +271,19 @@ export class RelayControlClient {
return
}
this.state = 'active'
this.silenceWatchdog.start()
this.liveness.start()
this.connectResolve?.(ack.data)
this.clearConnectPromise()
}

// A liveness teardown lands on the origin as an ordinary 1006 close, so name
// the cause here: without it a probe-driven reconnect is indistinguishable
// from any other drop, and a fleet-wide false positive would be invisible.
private terminateForLiveness(reason: RelayControlLivenessTeardown): void {
console.warn(`[relay] control torn down cell=${this.relayOrigin} reason=${reason}`)
this.socket?.terminate()
}

private sendActive(payload: Record<string, unknown>): void {
if (!this.socket || (this.state !== 'active' && this.state !== 'draining')) {
throw new Error('relay_control_not_active')
Expand All @@ -285,7 +300,7 @@ export class RelayControlClient {
private handleClose(code: number): void {
const wasConnecting = this.state === 'opening' || this.state === 'proving'
this.state = 'closed'
this.silenceWatchdog.stop()
this.liveness.stop()
if (wasConnecting) {
this.connectReject?.(new Error(`relay_control_closed_${code}`))
this.clearConnectPromise()
Expand Down
Loading
Loading