Skip to content

Commit fd67fd2

Browse files
improvement(functions): increase function block timeout to 3 min (#1641)
* improvement(functions): increase function block timeout to 3 min * fix tests * use shared constant * remove comment
1 parent 061c1df commit fd67fd2

File tree

6 files changed

+27
-12
lines changed

6 files changed

+27
-12
lines changed

apps/sim/app/api/function/execute/route.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { createContext, Script } from 'vm'
22
import { type NextRequest, NextResponse } from 'next/server'
33
import { env, isTruthy } from '@/lib/env'
4+
import { MAX_EXECUTION_DURATION } from '@/lib/execution/constants'
45
import { executeInE2B } from '@/lib/execution/e2b'
56
import { CodeLanguage, DEFAULT_CODE_LANGUAGE, isValidCodeLanguage } from '@/lib/execution/languages'
67
import { createLogger } from '@/lib/logs/console/logger'
78
import { validateProxyUrl } from '@/lib/security/input-validation'
89
import { generateRequestId } from '@/lib/utils'
910
export const dynamic = 'force-dynamic'
1011
export const runtime = 'nodejs'
11-
export const maxDuration = 60
12+
export const maxDuration = MAX_EXECUTION_DURATION
1213

1314
const logger = createLogger('FunctionExecuteAPI')
1415

@@ -649,10 +650,12 @@ export async function POST(req: NextRequest) {
649650
try {
650651
const body = await req.json()
651652

653+
const { DEFAULT_EXECUTION_TIMEOUT_MS } = await import('@/lib/execution/constants')
654+
652655
const {
653656
code,
654657
params = {},
655-
timeout = 5000,
658+
timeout = DEFAULT_EXECUTION_TIMEOUT_MS,
656659
language = DEFAULT_CODE_LANGUAGE,
657660
useLocalVM = false,
658661
envVars = {},

apps/sim/executor/handlers/function/function-handler.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { beforeEach, describe, expect, it, type Mock, vi } from 'vitest'
2+
import { DEFAULT_EXECUTION_TIMEOUT_MS } from '@/lib/execution/constants'
23
import { BlockType } from '@/executor/consts'
34
import { FunctionBlockHandler } from '@/executor/handlers/function/function-handler'
45
import type { ExecutionContext } from '@/executor/types'
@@ -82,7 +83,7 @@ describe('FunctionBlockHandler', () => {
8283
workflowVariables: {},
8384
blockData: {},
8485
blockNameMapping: {},
85-
_context: { workflowId: mockContext.workflowId },
86+
_context: { workflowId: mockContext.workflowId, workspaceId: mockContext.workspaceId },
8687
}
8788
const expectedOutput: any = { result: 'Success' }
8889

@@ -116,7 +117,7 @@ describe('FunctionBlockHandler', () => {
116117
workflowVariables: {},
117118
blockData: {},
118119
blockNameMapping: {},
119-
_context: { workflowId: mockContext.workflowId },
120+
_context: { workflowId: mockContext.workflowId, workspaceId: mockContext.workspaceId },
120121
}
121122
const expectedOutput: any = { result: 'Success' }
122123

@@ -138,12 +139,12 @@ describe('FunctionBlockHandler', () => {
138139
code: inputs.code,
139140
language: 'javascript',
140141
useLocalVM: true,
141-
timeout: 5000, // Default timeout
142+
timeout: DEFAULT_EXECUTION_TIMEOUT_MS,
142143
envVars: {},
143144
workflowVariables: {},
144145
blockData: {},
145146
blockNameMapping: {},
146-
_context: { workflowId: mockContext.workflowId },
147+
_context: { workflowId: mockContext.workflowId, workspaceId: mockContext.workspaceId },
147148
}
148149

149150
await handler.execute(mockBlock, inputs, mockContext)

apps/sim/executor/handlers/function/function-handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DEFAULT_EXECUTION_TIMEOUT_MS } from '@/lib/execution/constants'
12
import { DEFAULT_CODE_LANGUAGE } from '@/lib/execution/languages'
23
import { createLogger } from '@/lib/logs/console/logger'
34
import { BlockType } from '@/executor/consts'
@@ -61,7 +62,7 @@ export class FunctionBlockHandler implements BlockHandler {
6162
code: codeContent,
6263
language: inputs.language || DEFAULT_CODE_LANGUAGE,
6364
useLocalVM: !inputs.remoteExecution,
64-
timeout: inputs.timeout || 5000,
65+
timeout: inputs.timeout || DEFAULT_EXECUTION_TIMEOUT_MS,
6566
envVars: context.environmentVariables || {},
6667
workflowVariables: context.workflowVariables || {},
6768
blockData: blockData, // Pass block data for variable resolution
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Execution timeout constants
3+
*
4+
* These constants define the timeout values for code execution.
5+
* - DEFAULT_EXECUTION_TIMEOUT_MS: The default timeout for executing user code (3 minutes)
6+
* - MAX_EXECUTION_DURATION: The maximum duration for the API route (adds 30s buffer for overhead)
7+
*/
8+
9+
export const DEFAULT_EXECUTION_TIMEOUT_MS = 180000 // 3 minutes (180 seconds)
10+
export const MAX_EXECUTION_DURATION = 210 // 3.5 minutes (210 seconds) - includes buffer for sandbox creation

apps/sim/tools/function/execute.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* which runs JavaScript code in a secure sandbox.
88
*/
99
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
10+
import { DEFAULT_EXECUTION_TIMEOUT_MS } from '@/lib/execution/constants'
1011
import { ToolTester } from '@/tools/__test-utils__/test-tools'
1112
import { functionExecuteTool } from '@/tools/function/execute'
1213

@@ -95,7 +96,7 @@ describe('Function Execute Tool', () => {
9596

9697
expect(body).toEqual({
9798
code: 'return 42',
98-
timeout: 10000,
99+
timeout: DEFAULT_EXECUTION_TIMEOUT_MS,
99100
envVars: {},
100101
workflowVariables: {},
101102
blockData: {},

apps/sim/tools/function/execute.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
import { DEFAULT_EXECUTION_TIMEOUT_MS } from '@/lib/execution/constants'
12
import { DEFAULT_CODE_LANGUAGE } from '@/lib/execution/languages'
23
import type { CodeExecutionInput, CodeExecutionOutput } from '@/tools/function/types'
34
import type { ToolConfig } from '@/tools/types'
45

5-
const DEFAULT_TIMEOUT = 10000 // 10 seconds
6-
76
export const functionExecuteTool: ToolConfig<CodeExecutionInput, CodeExecutionOutput> = {
87
id: 'function_execute',
98
name: 'Function Execute',
@@ -38,7 +37,7 @@ export const functionExecuteTool: ToolConfig<CodeExecutionInput, CodeExecutionOu
3837
required: false,
3938
visibility: 'user-only',
4039
description: 'Execution timeout in milliseconds',
41-
default: DEFAULT_TIMEOUT,
40+
default: DEFAULT_EXECUTION_TIMEOUT_MS,
4241
},
4342
envVars: {
4443
type: 'object',
@@ -85,7 +84,7 @@ export const functionExecuteTool: ToolConfig<CodeExecutionInput, CodeExecutionOu
8584
code: codeContent,
8685
language: params.language || DEFAULT_CODE_LANGUAGE,
8786
useLocalVM: params.useLocalVM || false,
88-
timeout: params.timeout || DEFAULT_TIMEOUT,
87+
timeout: params.timeout || DEFAULT_EXECUTION_TIMEOUT_MS,
8988
envVars: params.envVars || {},
9089
workflowVariables: params.workflowVariables || {},
9190
blockData: params.blockData || {},

0 commit comments

Comments
 (0)