-
Notifications
You must be signed in to change notification settings - Fork 17
fail steps with oversized return values instead of getting stuck #236
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
80272d7
fail steps with oversized return values instead of getting stuck
ianmacartney 4ae172a
save above 800KB
ianmacartney 953687f
type
ianmacartney 93149bd
test oversized values
ianmacartney 35d3de6
codegen
ianmacartney 52b97d5
use updated status
ianmacartney 7594a81
fix
ianmacartney 34c956a
remove unused start fns
ianmacartney 8786df8
use assert
ianmacartney bf2f490
feedback
ianmacartney 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /// <reference types="vite/client" /> | ||
|
|
||
| import { expect, describe, test, vi, beforeEach, afterEach } from "vitest"; | ||
| import { initConvexTest } from "./setup.test"; | ||
| import { components, internal } from "./_generated/api"; | ||
| import { assert } from "convex-helpers"; | ||
| import { getStatus } from "@convex-dev/workflow"; | ||
| import { workflow } from "./example"; | ||
|
|
||
| describe("oversized values", () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| }); | ||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| test("large step return value fails workflow and calls onComplete", async () => { | ||
| const t = initConvexTest(); | ||
| const workflowId = await t.mutation(async (ctx) => { | ||
| const wfId = await workflow.start( | ||
| ctx, | ||
| internal.oversized.largeReturnWorkflow, | ||
| {}, | ||
| { | ||
| onComplete: internal.oversized.onComplete, | ||
| context: {}, | ||
| startAsync: true, | ||
| }, | ||
| ); | ||
| await ctx.db.insert("flows", { | ||
| workflowId: wfId, | ||
| in: "largeReturn", | ||
| out: null, | ||
| }); | ||
| return wfId; | ||
| }); | ||
| await t.finishAllScheduledFunctions(vi.runAllTimers); | ||
|
|
||
| const status = await t.run((ctx) => | ||
| getStatus(ctx, components.workflow, workflowId), | ||
| ); | ||
| expect(status.type).toBe("failed"); | ||
| assert(status.type === "failed"); | ||
| expect(status.error).toContain("Step return value too large"); | ||
|
|
||
| const flow = await t.query(async (ctx) => { | ||
| return ctx.db | ||
| .query("flows") | ||
| .withIndex("workflowId", (q) => q.eq("workflowId", workflowId)) | ||
| .first(); | ||
| }); | ||
| expect(flow).not.toBeNull(); | ||
| assert(flow); | ||
| expect(flow.out).not.toBeNull(); | ||
| expect(flow.out.kind).toBe("failed"); | ||
| expect(flow.out.error).toContain("Step return value too large"); | ||
| }); | ||
|
|
||
| test("large event value fails workflow and calls onComplete", async () => { | ||
| const t = initConvexTest(); | ||
| const workflowId = await t.mutation(async (ctx) => { | ||
| const wfId = await workflow.start( | ||
| ctx, | ||
| internal.oversized.eventWorkflow, | ||
| {}, | ||
| { | ||
| onComplete: internal.oversized.onComplete, | ||
| context: {}, | ||
| startAsync: true, | ||
| }, | ||
| ); | ||
| await ctx.db.insert("flows", { | ||
| workflowId: wfId, | ||
| in: "eventWorkflow", | ||
| out: null, | ||
| }); | ||
| return wfId; | ||
| }); | ||
| // Let the workflow start and reach the awaitEvent point | ||
| await t.finishAllScheduledFunctions(vi.runAllTimers); | ||
|
|
||
| const status = await t.run((ctx) => | ||
| getStatus(ctx, components.workflow, workflowId), | ||
| ); | ||
| expect(status.type).toBe("inProgress"); | ||
|
|
||
| // Send the oversized event | ||
| await t.mutation(internal.oversized.sendBigEvent, { workflowId }); | ||
| await t.finishAllScheduledFunctions(vi.runAllTimers); | ||
|
|
||
| const status2 = await t.run((ctx) => | ||
| getStatus(ctx, components.workflow, workflowId), | ||
| ); | ||
| expect(status2.type).toBe("failed"); | ||
| assert(status2.type === "failed"); | ||
| expect(status2.error).toContain("Step return value too large"); | ||
| expect(status2.error).toContain("900002 bytes"); | ||
|
|
||
| const flow = await t.query(async (ctx) => { | ||
| return ctx.db | ||
| .query("flows") | ||
| .withIndex("workflowId", (q) => q.eq("workflowId", workflowId)) | ||
| .first(); | ||
| }); | ||
| expect(flow).not.toBeNull(); | ||
| expect(flow!.out).not.toBeNull(); | ||
| expect(flow!.out.kind).toBe("failed"); | ||
| expect(flow!.out.error).toContain("Step return value too large"); | ||
| }); | ||
| }); |
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 { v } from "convex/values"; | ||
| import { sendEvent, defineEvent } from "@convex-dev/workflow"; | ||
| import { components, internal } from "./_generated/api.js"; | ||
| import { internalAction, internalMutation } from "./_generated/server.js"; | ||
| import { vWorkflowId } from "@convex-dev/workflow"; | ||
| import { vResultValidator } from "@convex-dev/workpool"; | ||
| import { workflow } from "./example.js"; | ||
|
|
||
| // Action that returns a value larger than 800KB. | ||
| export const largeReturnAction = internalAction({ | ||
| args: {}, | ||
| returns: v.string(), | ||
| handler: async (): Promise<string> => { | ||
| return "x".repeat(900_000); | ||
| }, | ||
| }); | ||
|
|
||
| export const largeReturnWorkflow = workflow | ||
| .define({ | ||
| args: {}, | ||
| }) | ||
| .handler(async (step) => { | ||
| const result = await step.runAction( | ||
| internal.oversized.largeReturnAction, | ||
| {}, | ||
| ); | ||
| return result; | ||
| }); | ||
|
|
||
| export const bigEvent = defineEvent({ | ||
| name: "bigEvent", | ||
| validator: v.string(), | ||
| }); | ||
|
|
||
| export const eventWorkflow = workflow | ||
| .define({ | ||
| args: {}, | ||
| }) | ||
| .handler(async (step) => { | ||
| const result = await step.awaitEvent(bigEvent); | ||
| return result; | ||
| }); | ||
|
|
||
| export const sendBigEvent = internalMutation({ | ||
| args: { | ||
| workflowId: vWorkflowId, | ||
| }, | ||
| returns: v.null(), | ||
| handler: async (ctx, args) => { | ||
| await sendEvent(ctx, components.workflow, { | ||
| ...bigEvent, | ||
| workflowId: args.workflowId, | ||
| value: "y".repeat(900_000), | ||
| }); | ||
| }, | ||
| }); | ||
|
|
||
| export const onComplete = internalMutation({ | ||
| args: { | ||
| workflowId: vWorkflowId, | ||
| result: vResultValidator, | ||
| context: v.any(), | ||
| }, | ||
| returns: v.null(), | ||
| handler: async (ctx, args) => { | ||
| const flow = await ctx.db | ||
| .query("flows") | ||
| .withIndex("workflowId", (q) => q.eq("workflowId", args.workflowId)) | ||
| .first(); | ||
| if (!flow) return null; | ||
| await ctx.db.patch(flow._id, { out: args.result }); | ||
| return null; | ||
| }, | ||
| }); |
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,34 @@ | ||
| import { type Value, convexToJson, getConvexSize } from "convex/values"; | ||
| import type { RunResult } from "@convex-dev/workpool"; | ||
|
|
||
| export const MAX_RETURN_VALUE_SIZE = 800 << 10; // 800 KiB | ||
| const PREVIEW_SIZE = 128 << 10; // 128 KB | ||
|
|
||
| function truncatedPreview(returnValue: unknown): string { | ||
| const json = JSON.stringify(convexToJson(returnValue as Value)); | ||
| if (json.length <= PREVIEW_SIZE * 2) { | ||
| return json; | ||
| } | ||
| return json.slice(0, PREVIEW_SIZE) + "..." + json.slice(-PREVIEW_SIZE); | ||
| } | ||
|
|
||
| export function checkReturnValueSize( | ||
| returnValue: Value | undefined, | ||
| ): string | null { | ||
| const size = getConvexSize(returnValue); | ||
| if (size > MAX_RETURN_VALUE_SIZE) { | ||
| return `Step return value too large (${size} bytes). Maximum is ${MAX_RETURN_VALUE_SIZE} bytes. Preview: ${truncatedPreview(returnValue)}`; | ||
| } | ||
| return null; | ||
| } | ||
|
ianmacartney marked this conversation as resolved.
|
||
|
|
||
| export function checkForOversizedResult(result: RunResult): RunResult { | ||
| if (result.kind !== "success") { | ||
| return result; | ||
| } | ||
| const sizeError = checkReturnValueSize(result.returnValue); | ||
| if (!sizeError) { | ||
| return result; | ||
| } | ||
| return { kind: "failed", error: sizeError }; | ||
| } | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this could fail if the return value contains a
BigInt. Maybe consider wrapping the JSON.stringify call in atry/catch?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
convexToJsonwill turn bigint (any Convex Value) into a json-serializable value