improvement(functions): increase function block timeout to 3 min (#1641) · simstudioai/sim@fd67fd2 · GitHub
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

6 files changed

Lines changed: 27 additions & 12 deletions

File tree

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

Lines changed: 5 additions & 2 deletions

‎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

0 commit comments

Comments
 (0)