Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

add boot yield on route creation to avoid memory stack exceed

adilson6d40488f963c
diff --git a/backend/src/server/lib/boot-yield.ts b/backend/src/server/lib/boot-yield.ts
new file mode 100644
index 0000000000..82011fab98
--- /dev/null
+++ b/backend/src/server/lib/boot-yield.ts
@@ -0,0 +1,14 @@
+// Fastify drains every encapsulated context's onReady phase synchronously through avvio's
+// ready queue. With hundreds of per-destination route contexts that synchronous fastq drain
+// recurses once per context and can blow V8's call stack at boot. A no-op async onReady hook
+// makes this context's ready task resolve on a microtask, letting the stack unwind between
+// sibling contexts and keeping boot recursion bounded.
+export const addBootYieldHook = (server: FastifyZodProvider) => {
+ server.addHook("onReady", async () => {});
+};
+
+export const withBootYield =
+ (plugin: (server: FastifyZodProvider) => Promise<void>) => async (server: FastifyZodProvider) => {
+ addBootYieldHook(server);
+ await plugin(server);
+ };
diff --git a/backend/src/server/routes/v1/index.ts b/backend/src/server/routes/v1/index.ts
index 6a3b24ae29..0c4ee1fdd7 100644
--- a/backend/src/server/routes/v1/index.ts
+++ b/backend/src/server/routes/v1/index.ts
@@ -1,4 +1,5 @@
import { BadRequestError } from "@app/lib/errors";
+import { withBootYield } from "@app/server/lib/boot-yield";
import { injectCertManagerProjectId } from "@app/server/plugins/inject-cert-manager-project-id";
import {
APP_CONNECTION_REGISTER_ROUTER_MAP,
@@ -323,7 +324,7 @@ export const registerV1Routes = async (server: FastifyZodProvider) => {
// register service specific endpoints (app-connections/aws, app-connections/github, etc.)
for await (const [app, router] of Object.entries(APP_CONNECTION_REGISTER_ROUTER_MAP)) {
- await appConnectionRouter.register(router, { prefix: `/${app}` });
+ await appConnectionRouter.register(withBootYield(router), { prefix: `/${app}` });
}
},
{ prefix: "/app-connections" }
@@ -336,7 +337,7 @@ export const registerV1Routes = async (server: FastifyZodProvider) => {
// register service specific secret sync endpoints (secret-syncs/aws-parameter-store, secret-syncs/github, etc.)
for await (const [destination, router] of Object.entries(SECRET_SYNC_REGISTER_ROUTER_MAP)) {
- await secretSyncRouter.register(router, { prefix: `/${destination}` });
+ await secretSyncRouter.register(withBootYield(router), { prefix: `/${destination}` });
}
},
{ prefix: "/secret-syncs" }