Skip to content

Commit 2153fc3

Browse files
anandgupta42claude
andcommitted
fix: close the reload and reset windows for in-flight warehouse connects
- `reload()` detaches and reloads before awaiting connector closes - `reset()` invalidates pending connects before dropping states Refs #1566 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent edd1acf commit 2153fc3

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

‎packages/opencode/src/altimate/native/connections/registry.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,16 +933,18 @@ export async function remove(name: string): Promise<{ success: boolean; error?:
933933
// altimate_change start — a way to release native handles without reloading
934934
/** Close one project's cached connectors and their SSH tunnels, and drop its in-flight connects. */
935935
async function closeState(s: State): Promise<void> {
936+
// Detach synchronously, so nothing started during the awaits below is dropped or reuses a closing connector.
937+
const cached = [...s.connectors]
938+
s.connectors.clear()
936939
s.pending.clear()
937-
for (const [name, connector] of s.connectors) {
940+
for (const [name, connector] of cached) {
938941
try {
939942
await connector.close()
940943
} catch {
941944
// ignore
942945
}
943946
closeTunnel(tunnelKey(s, name))
944947
}
945-
s.connectors.clear()
946948
}
947949

948950
/**
@@ -962,12 +964,14 @@ export async function closeAll(): Promise<void> {
962964

963965
/** Reload the current project's configs and clear its cached connectors; other projects are untouched. */
964966
export async function reload(): Promise<void> {
965-
// altimate_change start — only this project's connectors; states captured before the await
967+
// altimate_change start — only this project's connectors. The new configs are in place before the first await,
968+
// so no connect can start against the old ones while the old connectors close.
966969
const s = state()
967970
const target = projectState()
968-
await closeState(s)
971+
const closing = closeState(s)
969972
injectionActive = false
970973
loadInto(target)
974+
await closing
971975
// altimate_change end
972976
}
973977

@@ -982,7 +986,8 @@ export function getConfig(name: string): ConnectionConfig | undefined {
982986

983987
/** Reset the registry state of every project (for testing). */
984988
export function reset(): void {
985-
// altimate_change start
989+
// altimate_change start — invalidate in-flight connects first, so none is cached into a dropped state
990+
for (const s of allStates()) s.pending.clear()
986991
states.clear()
987992
injectedState = undefined
988993
injectionActive = false

‎packages/opencode/test/altimate/registry-per-project.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ describe("test helpers", () => {
257257
expect(inProject(projectA, () => Registry.names())).toEqual([])
258258
})
259259

260+
test("a connect in flight across reset() is not cached into the dropped state", async () => {
261+
writeLocal(projectA, { store: { type: "sqlite", path: "store.sqlite", create: true } })
262+
const inflight = inProject(projectA, () => Registry.get("store"))
263+
Registry.reset()
264+
await expect(inflight).rejects.toThrow("reloaded or removed while it was connecting")
265+
})
266+
260267
test("closeAll releases connectors cached under setConfigs after load()", async () => {
261268
Registry.setConfigs({ mem: { type: "sqlite", path: ":memory:" } })
262269
const injected = await Registry.get("mem")

0 commit comments

Comments
 (0)