Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 516
fix(cli): clamp edge-runtime nofile ulimit to the host hard limit#6284
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
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
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
29 changes: 17 additions & 12 deletions
29 apps/cli/src/legacy/commands/start/services/edge-runtime.service.integration.test.ts
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
2 changes: 1 addition & 1 deletion
2 apps/cli/src/legacy/commands/start/services/edge-runtime.service.ts
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
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,59 @@ | ||
| // Raised from the daemon default so many concurrent Deno isolates can run | ||
| // (supabase/cli#5151). | ||
| const desiredNofile = 65536; | ||
| // `userLimits.open_files.hard` from a `process.report.getReport()` diagnostic | ||
| // report — a number or "unlimited". Narrowed structurally because getReport() | ||
| // is typed as a bare `object`. | ||
| export const hardNofileLimitFromReport = (report: unknown): number | undefined => { | ||
| if (typeof report !== "object" || report === null || !("userLimits" in report)) return undefined; | ||
| const userLimits = report.userLimits; | ||
| if (typeof userLimits !== "object" || userLimits === null || !("open_files" in userLimits)) { | ||
| return undefined; | ||
| } | ||
| const openFiles = userLimits.open_files; | ||
| if (typeof openFiles !== "object" || openFiles === null || !("hard" in openFiles)) { | ||
| return undefined; | ||
| } | ||
| const hard = openFiles.hard; | ||
| return typeof hard === "number" && Number.isSafeInteger(hard) && hard > 0 ? hard : undefined; | ||
| }; | ||
| const hostHardNofileLimit = (platformOs: string): number | undefined => | ||
| platformOs === "linux" ? hardNofileLimitFromReport(process.report?.getReport()) : undefined; | ||
| // Never request more than the host's own hard cap: sandboxed hosts cap it | ||
| // below 65536, their docker daemon shares the cap, and exceeding it fails the | ||
| // container start (CLI-2220). The process's limit is a proxy for the daemon's | ||
| // — exact only when they share a kernel and limits, so only Linux is clamped | ||
| // (elsewhere the daemon runs in a VM), and only downward: a client more | ||
| // constrained than its daemon yields a smaller fd budget, never a failed start. | ||
| export const clampNofileLimit = (hardLimit: number | undefined): number => | ||
| hardLimit === undefined ? desiredNofile : Math.min(desiredNofile, hardLimit); | ||
| interface EdgeRuntimeNofileUlimit { | ||
| /** The docker `--ulimit` value, `nofile=<limit>:<limit>`. */ | ||
| readonly arg: string; | ||
| readonly limit: number; | ||
| /** Present only when the host's hard cap forced the request below the 65536 raise. */ | ||
| readonly clampWarning?: string; | ||
| } | ||
| // `hostHardLimit` defaults to the real host probe and exists as a parameter so | ||
| // callers with no host dependence (tests) can pin the clamp decision. | ||
| export const edgeRuntimeNofileUlimit = ( | ||
| platformOs: string, | ||
| hostHardLimit: number | undefined = hostHardNofileLimit(platformOs), | ||
| ): EdgeRuntimeNofileUlimit => { | ||
| const limit = clampNofileLimit(hostHardLimit); | ||
| return { | ||
| arg: `nofile=${limit}:${limit}`, | ||
| limit, | ||
| ...(limit < desiredNofile && { | ||
| clampWarning: | ||
| `Edge Runtime file descriptor limit lowered to ${limit}: ` + | ||
| `the host's hard limit (ulimit -Hn) is below the default ${desiredNofile}. ` + | ||
| `Heavy Edge Function workloads may exhaust file descriptors.`, | ||
| }), | ||
| }; | ||
| }; |
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,74 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| clampNofileLimit, | ||
| edgeRuntimeNofileUlimit, | ||
| hardNofileLimitFromReport, | ||
| } from "./nofile-limit.ts"; | ||
| const reportWithHard = (hard: number | string) => ({ | ||
| header: { reportVersion: 5 }, | ||
| userLimits: { | ||
| open_files: { soft: 1024, hard }, | ||
| }, | ||
| }); | ||
| describe("hardNofileLimitFromReport", () => { | ||
| it("reads the hard limit from a diagnostic report", () => { | ||
| expect(hardNofileLimitFromReport(reportWithHard(1048576))).toBe(1048576); | ||
| expect(hardNofileLimitFromReport(reportWithHard(20000))).toBe(20000); | ||
| }); | ||
| it("returns undefined when the hard limit is unlimited", () => { | ||
| expect(hardNofileLimitFromReport(reportWithHard("unlimited"))).toBeUndefined(); | ||
| }); | ||
| it("returns undefined for missing or malformed report shapes", () => { | ||
| expect(hardNofileLimitFromReport(undefined)).toBeUndefined(); | ||
| expect(hardNofileLimitFromReport(null)).toBeUndefined(); | ||
| expect(hardNofileLimitFromReport({})).toBeUndefined(); | ||
| expect(hardNofileLimitFromReport({ userLimits: {} })).toBeUndefined(); | ||
| expect(hardNofileLimitFromReport({ userLimits: { open_files: {} } })).toBeUndefined(); | ||
| expect(hardNofileLimitFromReport(reportWithHard(-1))).toBeUndefined(); | ||
| }); | ||
| }); | ||
| describe("clampNofileLimit", () => { | ||
| it("keeps the 65536 raise when the hard limit is unknown or higher", () => { | ||
| expect(clampNofileLimit(undefined)).toBe(65536); | ||
| expect(clampNofileLimit(1048576)).toBe(65536); | ||
| expect(clampNofileLimit(65536)).toBe(65536); | ||
| }); | ||
| it("clamps down to a lower hard limit (CLI-2220's 20000-cap sandbox)", () => { | ||
| expect(clampNofileLimit(20000)).toBe(20000); | ||
| }); | ||
| }); | ||
| describe("edgeRuntimeNofileUlimit", () => { | ||
| it("keeps the full 65536 raise off Linux, where the daemon runs in a VM", () => { | ||
| expect(edgeRuntimeNofileUlimit("darwin")).toEqual({ arg: "nofile=65536:65536", limit: 65536 }); | ||
| expect(edgeRuntimeNofileUlimit("win32")).toEqual({ arg: "nofile=65536:65536", limit: 65536 }); | ||
| }); | ||
| it("produces a matched soft:hard arg within Go's 65536 raise on Linux", () => { | ||
| const { arg, limit, clampWarning } = edgeRuntimeNofileUlimit("linux"); | ||
| expect(arg).toBe(`nofile=${limit}:${limit}`); | ||
| expect(limit).toBeGreaterThan(0); | ||
| expect(limit).toBeLessThanOrEqual(65536); | ||
| // The warning exists exactly when this host's cap forced a reduction. | ||
| expect(clampWarning !== undefined).toBe(limit < 65536); | ||
| }); | ||
| it("carries a warning when a lower host hard limit forces a clamp (CLI-2220's 20000-cap sandbox)", () => { | ||
| const clamped = edgeRuntimeNofileUlimit("linux", 20000); | ||
| expect(clamped.arg).toBe("nofile=20000:20000"); | ||
| expect(clamped.limit).toBe(20000); | ||
| expect(clamped.clampWarning).toContain("lowered to 20000"); | ||
| expect(clamped.clampWarning).toContain("65536"); | ||
| }); | ||
| it("omits the warning when the host limit does not constrain the raise", () => { | ||
| expect(edgeRuntimeNofileUlimit("linux", 1048576).clampWarning).toBeUndefined(); | ||
| expect(edgeRuntimeNofileUlimit("linux", 65536).clampWarning).toBeUndefined(); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.