Skip to content

Commit d389ea8

Browse files
fix(voice-call): compose ngrok timeout lifecycle
1 parent 36d8d54 commit d389ea8

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

‎extensions/voice-call/src/tunnel.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,29 @@ describe("voice-call tunnels", () => {
195195
}
196196
});
197197

198+
it("unrefs the ngrok startup timeout", async () => {
199+
const unrefSpy = vi.fn();
200+
const setTimeoutOriginal = globalThis.setTimeout.bind(globalThis);
201+
const setTimeoutSpy = vi
202+
.spyOn(globalThis, "setTimeout")
203+
.mockImplementation((handler: TimerHandler, timeout?: number) => {
204+
const timer = Reflect.apply(setTimeoutOriginal, globalThis, [handler, timeout]);
205+
Object.defineProperty(timer, "unref", { value: unrefSpy, configurable: true });
206+
return timer;
207+
});
208+
try {
209+
const proc = nextProcess();
210+
const result = startNgrokTunnel({ port: 3334, path: "/voice/webhook" });
211+
emitNgrokUrl(proc, "https://unref.ngrok.io");
212+
213+
await result;
214+
215+
expect(unrefSpy).toHaveBeenCalledOnce();
216+
} finally {
217+
setTimeoutSpy.mockRestore();
218+
}
219+
});
220+
198221
it("force-kills ngrok before rejecting a startup timeout", async () => {
199222
vi.useFakeTimers();
200223
try {

‎extensions/voice-call/src/tunnel.ts‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,28 +129,32 @@ async function startNgrokTunnel(config: {
129129
stdio: ["ignore", "pipe", "pipe"],
130130
});
131131

132-
let resolved = false;
133-
let closed = false;
132+
// Startup settlement and OS process closure are separate: the deadline can
133+
// win before the child has been reaped.
134+
let startupSettled = false;
135+
let childClosed = false;
134136
let publicUrl: string | null = null;
135137
let outputBuffer = "";
136138
// Keep only enough UTF-16-safe suffix to recognize an error marker split
137139
// at the next stream chunk boundary; otherwise the caller loses the code.
138140
let stderrTail = "";
139141

140142
const timeout = setTimeout(() => {
141-
if (!resolved) {
142-
resolved = true;
143-
void terminateNgrokProcess(proc, () => closed).then(() => {
143+
if (!startupSettled) {
144+
startupSettled = true;
145+
void terminateNgrokProcess(proc, () => childClosed).then(() => {
144146
reject(new Error("ngrok startup timed out (30s)"));
145147
});
146148
}
147149
}, 30000);
150+
// Do not keep the host process alive solely waiting on ngrok startup.
151+
timeout.unref();
148152

149153
const rejectIfPending = (message: string, kill = false) => {
150-
if (!resolved) {
151-
resolved = true;
154+
if (!startupSettled) {
155+
startupSettled = true;
152156
clearTimeout(timeout);
153-
if (kill && !closed) {
157+
if (kill && !childClosed) {
154158
proc.kill("SIGKILL");
155159
}
156160
reject(new Error(message));
@@ -172,8 +176,8 @@ async function startNgrokTunnel(config: {
172176
}
173177

174178
// Check for ready state
175-
if (publicUrl && !resolved) {
176-
resolved = true;
179+
if (publicUrl && !startupSettled) {
180+
startupSettled = true;
177181
clearTimeout(timeout);
178182

179183
// Add path to the public URL
@@ -185,7 +189,7 @@ async function startNgrokTunnel(config: {
185189
publicUrl: fullUrl,
186190
provider: "ngrok",
187191
stop: async () => {
188-
await terminateNgrokProcess(proc, () => closed);
192+
await terminateNgrokProcess(proc, () => childClosed);
189193
},
190194
});
191195
}
@@ -234,9 +238,9 @@ async function startNgrokTunnel(config: {
234238
});
235239

236240
proc.on("close", (code) => {
237-
closed = true;
238-
if (!resolved) {
239-
resolved = true;
241+
childClosed = true;
242+
if (!startupSettled) {
243+
startupSettled = true;
240244
clearTimeout(timeout);
241245
reject(new Error(`ngrok exited unexpectedly with code ${code}`));
242246
}

0 commit comments

Comments
 (0)