Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
13 changes: 12 additions & 1 deletion src/renderer/src/store/slices/runtime-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,10 @@
clearRuntimeCompatibilityCacheForTests()
})

it('records null and returns false when a runtime refresh fails', async () => {
// #19647: a failed status.get dials its own fresh socket, so a refresh must not overwrite a
// recorded live verdict with null — that retires the host's session-tabs mirror and dims its
// still-live rows on a fault the client could not even ask through.
it('preserves a recorded live verdict when a refresh probe fails', async () => {
const getStatus = vi.fn().mockRejectedValue(new Error('closed'))
stubRuntimeEnvironmentApi({ getStatus })
const store = createSliceStore()
Expand All @@ -720,6 +723,14 @@
const reachable = await store.getState().refreshRuntimeEnvironmentStatus('env-a')

expect(reachable).toBe(false)
expect(store.getState().runtimeStatusByEnvironmentId.get('env-a')?.status).toBe(cached)
})

it('records null on a first-contact refresh failure, so host coverage completes', async () => {
stubRuntimeEnvironmentApi({ getStatus: vi.fn().mockRejectedValue(new Error('closed')) })
const store = createSliceStore()

expect(await store.getState().refreshRuntimeEnvironmentStatus('env-a')).toBe(false)
expect(store.getState().runtimeStatusByEnvironmentId.get('env-a')?.status).toBe(null)
})

Expand Down Expand Up @@ -946,4 +957,4 @@
expect(store.getState().runtimeEnvironmentCatalogSettled).toBe(true)
expect(store.getState().runtimeEnvironmentCatalogHydrated).toBe(true)
})
})

Check failure on line 960 in src/renderer/src/store/slices/runtime-status.test.ts

View workflow job for this annotation

GitHub Actions / static analysis

eslint(max-lines)

src/renderer/src/store/slices/runtime-status.test.ts:960:3: File has too many lines (805).
9 changes: 9 additions & 0 deletions src/renderer/src/store/slices/runtime-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,15 @@ export const createRuntimeStatusSlice: StateCreator<AppState, [], [], RuntimeSta
get().applyRuntimeHostStatusSnapshot(entry.snapshot)
return
}
// A null entry with no snapshot only ever comes from a status.get that threw — an
// unverifiable transport failure, never a host-answered "gone". Publishing it would fire
// the disconnect toast and retire the host's session-tabs mirror while its established
// flows are still delivering. Keep the live verdict; the connection's status owner (#20003)
// holds the last verdict and retries until a real answer. A first-contact failure (no prior
// live status) still records null so host coverage completes. #19647
if (entry.status === null && get().runtimeStatusByEnvironmentId.get(environmentId)?.status) {
return
}
// Why: setRuntimeEnvironmentStatus drops any stale compat failure on a non-null
// (reachable) status, so a recovered host's reuse-flagged refetches re-probe.
get().setRuntimeEnvironmentStatus(environmentId, entry)
Expand Down
126 changes: 126 additions & 0 deletions tests/e2e/helpers/runtime-endpoint-link-fault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import net from 'node:net'
import { decodePairingOffer, encodePairingOffer } from '../../../src/shared/pairing'

/**
* A TCP hop in front of a paired runtime's WebSocket endpoint whose fault mode can
* change mid-test.
*
* `stall-new` is the Tailscale-shaped fault this exists for: already-established
* flows keep delivering while a freshly dialed connection hangs unanswered. That
* asymmetry is what separates "the control plane could not ask" from "the runtime
* is gone", and neither killing the runtime nor `runtimeEnvironments.disconnect`
* can produce it — both take the two halves down together.
*/
export type RuntimeLinkFaultMode = 'pass' | 'stall-new'

export type RuntimeEndpointLinkFault = {
/** ws:// endpoint that routes through this hop. */
endpoint: string
setMode: (mode: RuntimeLinkFaultMode) => void
/**
* Severs every established flow once, leaving `mode` in force for what dials next.
* With `stall-new` this is the link flap that puts the shared-control socket into
* `reconnecting` while its replacement dial hangs.
*/
dropEstablished: () => number
/** Connections accepted since the last reset — proves a dial actually reached the hop. */
acceptedConnectionCount: () => number
stalledConnectionCount: () => number
resetCounters: () => void
close: () => Promise<void>
}

function parseWsEndpoint(endpoint: string): { host: string; port: number } {
const url = new URL(endpoint)
return { host: url.hostname, port: Number(url.port) }
}

export async function startRuntimeEndpointLinkFault(
upstreamEndpoint: string
): Promise<RuntimeEndpointLinkFault> {
const upstream = parseWsEndpoint(upstreamEndpoint)
let mode: RuntimeLinkFaultMode = 'pass'
let accepted = 0
let stalledTotal = 0
const stalled = new Set<net.Socket>()
const live = new Set<net.Socket>()

const server = net.createServer((client) => {
accepted += 1
live.add(client)
client.on('close', () => live.delete(client))
client.on('error', () => client.destroy())
if (mode === 'stall-new') {
// Accept the TCP handshake and answer nothing: the dialer waits out its own
// timeout, as it does on a half-open path, instead of failing fast on reset.
stalledTotal += 1
stalled.add(client)
client.on('close', () => stalled.delete(client))
return
}
const upstreamSocket = net.connect(upstream.port, upstream.host)
live.add(upstreamSocket)
upstreamSocket.on('close', () => live.delete(upstreamSocket))
upstreamSocket.on('error', () => client.destroy())
client.pipe(upstreamSocket)
upstreamSocket.pipe(client)
})

await new Promise<void>((resolve, reject) => {
server.once('error', reject)
server.listen(0, '127.0.0.1', () => {
server.removeListener('error', reject)
resolve()
})
})
const address = server.address()
if (address === null || typeof address === 'string') {
throw new Error('runtime endpoint link fault did not bind a TCP port')
}

return {
endpoint: `ws://127.0.0.1:${address.port}`,
setMode: (next) => {
mode = next
if (next === 'pass') {
for (const socket of stalled) {
socket.destroy()
}
stalled.clear()
}
},
dropEstablished: () => {
let dropped = 0
for (const socket of live) {
if (!stalled.has(socket)) {
socket.destroy()
dropped += 1
}
}
return dropped
},
acceptedConnectionCount: () => accepted,
stalledConnectionCount: () => stalledTotal,
resetCounters: () => {
accepted = 0
stalledTotal = 0
},
close: async () => {
for (const socket of live) {
socket.destroy()
}
live.clear()
stalled.clear()
await new Promise<void>((resolve) => server.close(() => resolve()))
}
}
}

/** Re-points a pairing offer at `endpoint` without touching its keys or device token. */
export function repointPairingUrl(pairingUrl: string, endpoint: string): string {
return encodePairingOffer({ ...decodePairingOffer(pairingUrl), endpoint })
}

export function readPairingEndpoint(pairingUrl: string): string {
return decodePairingOffer(pairingUrl).endpoint
}
Loading
Loading