Skip to content

Commit 3062d40

Browse files
authored
fix(edge): note when stopped so we can kill process in early init (#313)
This is to address the edge case surfaced by the (Astro integration PR)[withastro/astro#13768] wherein the edge function Deno server won't be closed if the .stop() comes in very soon after the init. The issue is that .stop() can be called before denoBridge.runInBackground has finished resolving (but before we've waited for the server to start), and in that case we will never know it needed to be stopped.
1 parent 7327ebe commit 3062d40

File tree

1 file changed

+14
-4
lines changed
  • packages/edge-functions/dev/node

1 file changed

+14
-4
lines changed

packages/edge-functions/dev/node/main.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class EdgeFunctionsHandler {
5151
private geolocation: Geolocation
5252
private initialization: ReturnType<typeof this.initialize>
5353
private initialized: boolean
54+
private stopped: boolean
5455
private logger: Logger
5556
private requestTimeout: number
5657
private siteID?: string
@@ -65,6 +66,7 @@ export class EdgeFunctionsHandler {
6566
DENO_REGION: 'dev',
6667
})
6768
this.initialized = false
69+
this.stopped = false
6870
this.logger = options.logger
6971
this.requestTimeout = options.requestTimeout ?? REQUEST_TIMEOUT
7072
this.siteID = options.siteID
@@ -282,15 +284,21 @@ export class EdgeFunctionsHandler {
282284
extendEnv: false,
283285
pipeOutput: true,
284286
})
287+
if (this.stopped) {
288+
killProcess(processRef.ps)
289+
success = false
290+
}
285291
} catch (error) {
286292
success = false
287293

288294
this.logger.error(`An error occurred while setting up the Netlify Edge Functions environment: ${String(error)}`)
289295
}
290296

291-
// The Promise above will resolve as soon as we start the command, but we
292-
// must wait for it to actually listen for requests.
293-
await this.waitForDenoServer(denoPort)
297+
if (success) {
298+
// The Promise above will resolve as soon as we start the command, but we
299+
// must wait for it to actually listen for requests.
300+
await this.waitForDenoServer(denoPort)
301+
}
294302

295303
this.initialized = true
296304

@@ -347,7 +355,9 @@ export class EdgeFunctionsHandler {
347355
}
348356

349357
async stop() {
350-
if (!this.denoServerProcess) {
358+
if (this.stopped) return
359+
this.stopped = true
360+
if (!this.denoServerProcess?.ps) {
351361
return
352362
}
353363

0 commit comments

Comments
 (0)