Skip to content

Commit 3ebba02

Browse files
authored
refactor: replace Bun.sleep with node timers (anomalyco#15013)
1 parent cf425d1 commit 3ebba02

11 files changed

Lines changed: 29 additions & 18 deletions

File tree

github/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { Context as GitHubContext } from "@actions/github/lib/context"
88
import type { IssueCommentEvent, PullRequestReviewCommentEvent } from "@octokit/webhooks-types"
99
import { createOpencodeClient } from "@opencode-ai/sdk"
1010
import { spawn } from "node:child_process"
11+
import { setTimeout as sleep } from "node:timers/promises"
1112

1213
type GitHubAuthor = {
1314
login: string
@@ -281,7 +282,7 @@ async function assertOpencodeConnected() {
281282
connected = true
282283
break
283284
} catch (e) {}
284-
await Bun.sleep(300)
285+
await sleep(300)
285286
} while (retry++ < 30)
286287

287288
if (!connected) {

packages/opencode/src/cli/cmd/auth.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Instance } from "../../project/instance"
1313
import type { Hooks } from "@opencode-ai/plugin"
1414
import { Process } from "../../util/process"
1515
import { text } from "node:stream/consumers"
16+
import { setTimeout as sleep } from "node:timers/promises"
1617

1718
type PluginAuth = NonNullable<Hooks["auth"]>
1819

@@ -47,7 +48,7 @@ async function handlePluginAuth(plugin: { auth: PluginAuth }, provider: string,
4748
const method = plugin.auth.methods[index]
4849

4950
// Handle prompts for all auth types
50-
await Bun.sleep(10)
51+
await sleep(10)
5152
const inputs: Record<string, string> = {}
5253
if (method.prompts) {
5354
for (const prompt of method.prompts) {

packages/opencode/src/cli/cmd/debug/lsp.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { bootstrap } from "../../bootstrap"
33
import { cmd } from "../cmd"
44
import { Log } from "../../../util/log"
55
import { EOL } from "os"
6+
import { setTimeout as sleep } from "node:timers/promises"
67

78
export const LSPCommand = cmd({
89
command: "lsp",
@@ -19,7 +20,7 @@ const DiagnosticsCommand = cmd({
1920
async handler(args) {
2021
await bootstrap(process.cwd(), async () => {
2122
await LSP.touchFile(args.file, true)
22-
await Bun.sleep(1000)
23+
await sleep(1000)
2324
process.stdout.write(JSON.stringify(await LSP.diagnostics(), null, 2) + EOL)
2425
})
2526
},

packages/opencode/src/cli/cmd/github.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { Bus } from "../../bus"
2828
import { MessageV2 } from "../../session/message-v2"
2929
import { SessionPrompt } from "@/session/prompt"
3030
import { $ } from "bun"
31+
import { setTimeout as sleep } from "node:timers/promises"
3132

3233
type GitHubAuthor = {
3334
login: string
@@ -353,7 +354,7 @@ export const GithubInstallCommand = cmd({
353354
}
354355

355356
retries++
356-
await Bun.sleep(1000)
357+
await sleep(1000)
357358
} while (true)
358359

359360
s.stop("Installed GitHub app")
@@ -1372,7 +1373,7 @@ Co-authored-by: ${actor} <${actor}@users.noreply.github.com>"`
13721373
} catch (e) {
13731374
if (retries > 0) {
13741375
console.log(`Retrying after ${delayMs}ms...`)
1375-
await Bun.sleep(delayMs)
1376+
await sleep(delayMs)
13761377
return withRetry(fn, retries - 1, delayMs)
13771378
}
13781379
throw e

packages/opencode/src/cli/cmd/tui/worker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { GlobalBus } from "@/bus/global"
1010
import { createOpencodeClient, type Event } from "@opencode-ai/sdk/v2"
1111
import type { BunWebSocketData } from "hono/bun"
1212
import { Flag } from "@/flag/flag"
13+
import { setTimeout as sleep } from "node:timers/promises"
1314

1415
await Log.init({
1516
print: process.argv.includes("--print-logs"),
@@ -75,7 +76,7 @@ const startEventStream = (directory: string) => {
7576
).catch(() => undefined)
7677

7778
if (!events) {
78-
await Bun.sleep(250)
79+
await sleep(250)
7980
continue
8081
}
8182

@@ -84,7 +85,7 @@ const startEventStream = (directory: string) => {
8485
}
8586

8687
if (!signal.aborted) {
87-
await Bun.sleep(250)
88+
await sleep(250)
8889
}
8990
}
9091
})().catch((error) => {

packages/opencode/src/plugin/codex.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Installation } from "../installation"
44
import { Auth, OAUTH_DUMMY_KEY } from "../auth"
55
import os from "os"
66
import { ProviderTransform } from "@/provider/transform"
7+
import { setTimeout as sleep } from "node:timers/promises"
78

89
const log = Log.create({ service: "plugin.codex" })
910

@@ -602,7 +603,7 @@ export async function CodexAuthPlugin(input: PluginInput): Promise<Hooks> {
602603
return { type: "failed" as const }
603604
}
604605

605-
await Bun.sleep(interval + OAUTH_POLLING_SAFETY_MARGIN_MS)
606+
await sleep(interval + OAUTH_POLLING_SAFETY_MARGIN_MS)
606607
}
607608
},
608609
}

packages/opencode/src/plugin/copilot.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Hooks, PluginInput } from "@opencode-ai/plugin"
22
import { Installation } from "@/installation"
33
import { iife } from "@/util/iife"
4+
import { setTimeout as sleep } from "node:timers/promises"
45

56
const CLIENT_ID = "Ov23li8tweQw6odWQebz"
67
// Add a small safety buffer when polling to avoid hitting the server
@@ -270,7 +271,7 @@ export async function CopilotAuthPlugin(input: PluginInput): Promise<Hooks> {
270271
}
271272

272273
if (data.error === "authorization_pending") {
273-
await Bun.sleep(deviceData.interval * 1000 + OAUTH_POLLING_SAFETY_MARGIN_MS)
274+
await sleep(deviceData.interval * 1000 + OAUTH_POLLING_SAFETY_MARGIN_MS)
274275
continue
275276
}
276277

@@ -286,13 +287,13 @@ export async function CopilotAuthPlugin(input: PluginInput): Promise<Hooks> {
286287
newInterval = serverInterval * 1000
287288
}
288289

289-
await Bun.sleep(newInterval + OAUTH_POLLING_SAFETY_MARGIN_MS)
290+
await sleep(newInterval + OAUTH_POLLING_SAFETY_MARGIN_MS)
290291
continue
291292
}
292293

293294
if (data.error) return { type: "failed" as const }
294295

295-
await Bun.sleep(deviceData.interval * 1000 + OAUTH_POLLING_SAFETY_MARGIN_MS)
296+
await sleep(deviceData.interval * 1000 + OAUTH_POLLING_SAFETY_MARGIN_MS)
296297
continue
297298
}
298299
},

packages/opencode/src/shell/shell.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { lazy } from "@/util/lazy"
33
import { Filesystem } from "@/util/filesystem"
44
import path from "path"
55
import { spawn, type ChildProcess } from "child_process"
6+
import { setTimeout as sleep } from "node:timers/promises"
67

78
const SIGKILL_TIMEOUT_MS = 200
89

@@ -22,13 +23,13 @@ export namespace Shell {
2223

2324
try {
2425
process.kill(-pid, "SIGTERM")
25-
await Bun.sleep(SIGKILL_TIMEOUT_MS)
26+
await sleep(SIGKILL_TIMEOUT_MS)
2627
if (!opts?.exited?.()) {
2728
process.kill(-pid, "SIGKILL")
2829
}
2930
} catch (_e) {
3031
proc.kill("SIGTERM")
31-
await Bun.sleep(SIGKILL_TIMEOUT_MS)
32+
await sleep(SIGKILL_TIMEOUT_MS)
3233
if (!opts?.exited?.()) {
3334
proc.kill("SIGKILL")
3435
}

packages/opencode/test/preload.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os from "os"
44
import path from "path"
55
import fs from "fs/promises"
6+
import { setTimeout as sleep } from "node:timers/promises"
67
import { afterAll } from "bun:test"
78

89
// Set XDG env vars FIRST, before any src/ imports
@@ -15,7 +16,7 @@ afterAll(async () => {
1516
typeof error === "object" && error !== null && "code" in error && error.code === "EBUSY"
1617
const rm = async (left: number): Promise<void> => {
1718
Bun.gc(true)
18-
await Bun.sleep(100)
19+
await sleep(100)
1920
return fs.rm(dir, { recursive: true, force: true }).catch((error) => {
2021
if (!busy(error)) throw error
2122
if (left <= 1) throw error

packages/opencode/test/pty/pty-output-isolation.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test"
22
import { Instance } from "../../src/project/instance"
33
import { Pty } from "../../src/pty"
44
import { tmpdir } from "../fixture/fixture"
5+
import { setTimeout as sleep } from "node:timers/promises"
56

67
describe("pty", () => {
78
test("does not leak output when websocket objects are reused", async () => {
@@ -43,7 +44,7 @@ describe("pty", () => {
4344

4445
// Output from a must never show up in b.
4546
Pty.write(a.id, "AAA\n")
46-
await Bun.sleep(100)
47+
await sleep(100)
4748

4849
expect(outB.join("")).not.toContain("AAA")
4950
} finally {
@@ -88,7 +89,7 @@ describe("pty", () => {
8889
}
8990

9091
Pty.write(a.id, "AAA\n")
91-
await Bun.sleep(100)
92+
await sleep(100)
9293

9394
expect(outB.join("")).not.toContain("AAA")
9495
} finally {
@@ -128,7 +129,7 @@ describe("pty", () => {
128129
ctx.connId = 2
129130

130131
Pty.write(a.id, "AAA\n")
131-
await Bun.sleep(100)
132+
await sleep(100)
132133

133134
expect(out.join("")).toContain("AAA")
134135
} finally {

0 commit comments

Comments
 (0)