Skip to content

Commit 1286317

Browse files
committed
fix(tui): stop superseded location syncs from retrying after backoff
Re-check that the attempt still owns the location (and the provider is alive) after the backoff wait, so a superseded sync cannot fire another round and pending retries stop on disposal. Tests now wait on conditions instead of fixed sleeps.
1 parent d86db1f commit 1286317

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

‎packages/tui/src/context/location.tsx‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export function createLocation(input: {
3333
const [ref, setRef] = createSignal<LocationRef>()
3434
const [error, setError] = createSignal<{ readonly location: LocationRef; readonly cause: unknown }>()
3535
let generation = 0
36+
let disposed = false
3637
const current = createMemo(() => input.data.location.info(ref()))
3738

3839
async function settle(location: LocationRef, attempt: number, target: LocationRef | undefined) {
@@ -41,15 +42,11 @@ export function createLocation(input: {
4142
await input.data.location.sync(target)
4243
return
4344
} catch (cause) {
44-
const at = ref()
45-
if (
46-
generation !== attempt ||
47-
at?.directory !== location.directory ||
48-
at.workspaceID !== location.workspaceID
49-
)
50-
return
45+
if (!active(location, attempt)) return
5146
if (remaining > 0) {
5247
await new Promise((resolve) => setTimeout(resolve, input.delay ?? RETRY_DELAY))
48+
// A sync that was superseded while backing off must not fire another round.
49+
if (!active(location, attempt)) return
5350
continue
5451
}
5552
setError({ location, cause })
@@ -58,6 +55,13 @@ export function createLocation(input: {
5855
}
5956
}
6057

58+
// Whether this attempt still owns the target location and the provider is alive.
59+
function active(location: LocationRef, attempt: number) {
60+
if (disposed) return false
61+
const at = ref()
62+
return generation === attempt && at?.directory === location.directory && at.workspaceID === location.workspaceID
63+
}
64+
6165
function sync(location?: LocationRef) {
6266
if (!location) return
6367
const attempt = ++generation
@@ -75,6 +79,9 @@ export function createLocation(input: {
7579
if (input.connection.status() === "connected") sync(location)
7680
}
7781

82+
onCleanup(() => {
83+
disposed = true
84+
})
7885
onCleanup(input.event.on("server.connected", () => sync(ref())))
7986

8087
return {

‎packages/tui/test/context/location.test.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import { createApi, createFetch, directory } from "../fixture/tui-client"
88

99
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
1010

11+
async function waitFor(check: () => boolean, timeout = 5000) {
12+
const start = Date.now()
13+
while (!check()) {
14+
if (Date.now() - start > timeout) throw new Error("timed out waiting for condition")
15+
await sleep(10)
16+
}
17+
}
18+
1119
function setup(options: { failures?: number; delay?: number } = {}) {
1220
return createRoot((dispose) => {
1321
const events = createGlobalEmitter<{
@@ -46,9 +54,8 @@ test("a transient sync failure is retried before the recovery prompt surfaces",
4654
const scope = setup({ failures: 1 })
4755
try {
4856
scope.location.set({ directory })
49-
await sleep(150)
57+
await waitFor(() => scope.vcsCalls() >= 2)
5058
expect(scope.location.error).toBeUndefined()
51-
expect(scope.vcsCalls()).toBeGreaterThanOrEqual(2)
5259
} finally {
5360
scope.dispose()
5461
}
@@ -58,7 +65,7 @@ test("a persistent sync failure still surfaces the location error", async () =>
5865
const scope = setup({ failures: 99 })
5966
try {
6067
scope.location.set({ directory })
61-
await sleep(250)
68+
await waitFor(() => scope.location.error !== undefined)
6269
expect(scope.location.error?.location.directory).toBe(directory)
6370
} finally {
6471
scope.dispose()

0 commit comments

Comments
 (0)