-
-
Notifications
You must be signed in to change notification settings - Fork 914
fix(task): add request timeout to devFetch so a stalled dev server rejects instead of hanging #4345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
spokodev
wants to merge
3
commits into
nitrojs:main
Choose a base branch
from
spokodev:fix/devfetch-timeout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+103
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import http from "node:http"; | ||
| import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "pathe"; | ||
| import { afterAll, describe, expect, it, vi } from "vitest"; | ||
| import { listTasks, runTask } from "../../src/task.ts"; | ||
|
|
||
| describe("task runner devFetch", () => { | ||
| const cleanups: Array<() => Promise<void> | void> = []; | ||
|
|
||
| afterAll(async () => { | ||
| for (const cleanup of cleanups) { | ||
| await cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| // A worker socket that accepts connections but never responds, like a | ||
| // stalled dev server whose pid is still alive. | ||
| async function stalledDevServer() { | ||
| const cwd = await mkdtemp(join(tmpdir(), "nitro-task-test-")); | ||
| cleanups.push(() => rm(cwd, { recursive: true, force: true })); | ||
|
|
||
| const socketPath = join(cwd, "worker.sock"); | ||
| const server = http.createServer(() => {}); | ||
| cleanups.push(() => new Promise((resolve) => server.close(() => resolve()))); | ||
| await new Promise<void>((resolve) => server.listen(socketPath, resolve)); | ||
|
|
||
| const buildDir = join(cwd, "node_modules/.nitro"); | ||
| await mkdir(buildDir, { recursive: true }); | ||
| await writeFile( | ||
| join(buildDir, "nitro.dev.json"), | ||
| JSON.stringify({ | ||
| dev: { pid: process.pid, workerAddress: { socketPath } }, | ||
| }) | ||
| ); | ||
|
|
||
| return cwd; | ||
| } | ||
|
|
||
| // https://github.com/unjs/nitro/issues/4292 | ||
| it("rejects instead of hanging when the dev server socket stalls", async () => { | ||
| const cwd = await stalledDevServer(); | ||
| await expect(listTasks({ cwd, timeout: 200 })).rejects.toThrow(/timed out/i); | ||
| }, 5000); | ||
|
|
||
| it("honours an opt-in timeout for runTask", async () => { | ||
| const cwd = await stalledDevServer(); | ||
| await expect(runTask({ name: "db:migrate" }, { cwd, timeout: 200 })).rejects.toThrow( | ||
| /timed out/i | ||
| ); | ||
| }, 5000); | ||
|
|
||
| // The socket stays idle for as long as the task runs, so only listTasks may | ||
| // carry a default. Read the request options rather than waiting 30s for it. | ||
| it("defaults the timeout for listTasks but not for runTask", async () => { | ||
| const cwd = await stalledDevServer(); | ||
| const request = vi.spyOn(http, "request"); | ||
|
|
||
| const requested = async (call: Promise<unknown>) => { | ||
| call.catch(() => {}); | ||
| await vi.waitFor(() => expect(request).toHaveBeenCalled()); | ||
| const [, options] = request.mock.calls[0] as [unknown, { timeout?: number }]; | ||
| (request.mock.results[0].value as http.ClientRequest).destroy(); | ||
| request.mockClear(); | ||
| return options; | ||
| }; | ||
|
|
||
| expect(await requested(listTasks({ cwd }))).toHaveProperty("timeout", 30_000); | ||
| expect(await requested(runTask({ name: "db:migrate" }, { cwd }))).not.toHaveProperty("timeout"); | ||
| expect(await requested(runTask({ name: "db:migrate" }, { cwd, timeout: 200 }))).toHaveProperty( | ||
| "timeout", | ||
| 200 | ||
| ); | ||
|
|
||
| request.mockRestore(); | ||
| }, 5000); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.