Skip to content

Commit c25e592

Browse files
Copilotshuv1337
andauthored
Fix race condition in ConfigInvalidation.setup() with Promise-based init (#13)
* Initial plan * Fix race condition in ConfigInvalidation.setup() using Promise-based pattern Co-authored-by: kcrommett <523952+kcrommett@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: kcrommett <523952+kcrommett@users.noreply.github.com>
1 parent c3b7053 commit c25e592

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

packages/opencode/src/config/invalidation.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type ApplyInput = {
1515
refreshed?: boolean
1616
}
1717

18-
let initialized = false
18+
let setupPromise: Promise<void> | undefined
1919
async function invalidateProvider(diff: ConfigDiff): Promise<void> {
2020
await Instance.invalidate("provider")
2121
}
@@ -169,17 +169,20 @@ export namespace ConfigInvalidation {
169169
}
170170
}
171171

172-
export function setup() {
173-
if (initialized) {
174-
return
172+
export async function setup() {
173+
if (setupPromise) {
174+
return setupPromise
175175
}
176-
initialized = true
177176

178-
if (isConfigHotReloadEnabled()) {
179-
Bus.subscribe(Config.Event.Updated, async (event) => {
180-
const { diff, scope, directory, refreshed } = event.properties as any
181-
await apply({ diff, scope, directory, refreshed })
182-
})
183-
}
177+
setupPromise = (async () => {
178+
if (isConfigHotReloadEnabled()) {
179+
Bus.subscribe(Config.Event.Updated, async (event) => {
180+
const { diff, scope, directory, refreshed } = event.properties as any
181+
await apply({ diff, scope, directory, refreshed })
182+
})
183+
}
184+
})()
185+
186+
return setupPromise
184187
}
185188
}

packages/opencode/src/project/bootstrap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { ConfigInvalidation } from "../config/invalidation"
1414

1515
export async function InstanceBootstrap() {
1616
Log.Default.info("bootstrapping", { directory: Instance.directory })
17-
ConfigInvalidation.setup()
17+
await ConfigInvalidation.setup()
1818
await Plugin.init()
1919
Share.init()
2020
Format.init()

0 commit comments

Comments
 (0)