Skip to content

Commit 1b7cac0

Browse files
committed
fix(mothership): end a transport outage only after the whole poll iteration succeeds
1 parent ed7ff59 commit 1b7cac0

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

‎apps/sim/lib/mothership/transport/receiver.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ describe('outbound receiver backoff', () => {
291291
expect(logger().info).toHaveBeenCalledOnce()
292292
})
293293

294+
it('keeps backing off while polls succeed but their batches cannot be read', async () => {
295+
const malformed = () => Response.json({ requests: 'not a list' })
296+
const delays = await delaysFor(Array.from({ length: 5 }, () => malformed))
297+
expect(delays[4]).toBeGreaterThanOrEqual(3200)
298+
expect(logger().warn).toHaveBeenCalledOnce()
299+
expect(logger().info).not.toHaveBeenCalled()
300+
})
301+
294302
it('warns again, and backs off from the start, when an outage turns into a refused key', async () => {
295303
const delays = await delaysFor([lost, lost, lost, refused(401), refused(401)])
296304
expect(delays[3]).toBeLessThanOrEqual(36_000)

‎apps/sim/lib/mothership/transport/receiver.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ export async function receiveSimControls(
102102
}
103103
throw new Error(`Transport poll refused (HTTP ${response.status})`)
104104
}
105-
if (outage) {
106-
logger.info('Sim transport reconnected')
107-
outage = undefined
108-
}
109105
phase = 'parse'
110106
const batch = SimChannelBatchEnvelope.parse(await response.json())
111107
const controls = batch.requests.flatMap((raw) => receiveControl(raw) ?? [])
@@ -130,6 +126,11 @@ export async function receiveSimControls(
130126
)
131127
const failed = replies.find((reply) => reply.status === 'rejected')
132128
if (failed?.status === 'rejected') throw failed.reason
129+
// Only a whole iteration (poll, batch, replies) ends an outage.
130+
if (outage) {
131+
logger.info('Sim transport reconnected')
132+
outage = undefined
133+
}
133134
} catch (error) {
134135
if (signal.aborted) return
135136
const keyRefused = error instanceof TransportKeyRefused

0 commit comments

Comments
 (0)