Title: [BUG] Variable references not injected into execution context, causing ReferenceError · Issue #2798 · simstudioai/sim · GitHub
Open Graph Title: [BUG] Variable references not injected into execution context, causing ReferenceError · Issue #2798 · simstudioai/sim
X Title: [BUG] Variable references not injected into execution context, causing ReferenceError · Issue #2798 · simstudioai/sim
Description: Bug Description When function/condition blocks reference undefined block variables (e.g.,
Open Graph Description: Bug Description When function/condition blocks reference undefined block variables (e.g.,
X Description: Bug Description When function/condition blocks reference undefined block variables (e.g., <webhook1.conversation.id> when webhook block hasn't executed), the variable reference is substit...
Opengraph URL: https://github.com/simstudioai/sim/issues/2798
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[BUG] Variable references not injected into execution context, causing ReferenceError","articleBody":"## Bug Description\n\nWhen function/condition blocks reference undefined block variables (e.g., `\u003cwebhook1.conversation.id\u003e` when webhook block hasn't executed), the variable reference is substituted into the code but the actual variable is never injected into the execution context, causing \"ReferenceError: variable is not defined\" at runtime.\n\n**This issue was discovered as a consequence of fixing #2794** - while the nullish coalescing fix addressed the substitution syntax, it revealed this deeper IPC serialization bug.\n\n## Root Cause\n\nThe variable substitution process has a multi-layer issue:\n\n1. **Code Substitution Layer**: `\u003cwebhook1.conversation.id\u003e` is correctly replaced with `__tag_webhook1_conversation_id` in the code\n2. **Context Variable Storage**: The variable IS added to `contextVariables` map with `undefined` value\n3. **IPC Serialization Layer**: When `contextVariables` is serialized via IPC to the worker thread, `JSON.stringify()` **strips keys with undefined values**, causing those variables to not be transmitted\n4. **Execution Layer**: The worker receives an empty `contextVariables` object and can't inject the missing variables into the VM\n\nResult: Code references `__tag_webhook1_conversation_id` but it's never defined in the execution context.\n\n## Error Manifestation\n\n```\nReferenceError: __tag_webhook1_conversation_id is not defined\n```\n\nThis occurs in:\n- E2B JavaScript execution path\n- E2B Python execution path\n- Isolated-VM execution path\n\n## Reproduction Steps\n\n1. Create a workflow with Start trigger and Webhook trigger\n2. Add Extract Context function block with: `const x = \u003cwebhook1.optional_field\u003e ?? \"default\";`\n3. Execute workflow using **Start trigger** (webhook doesn't execute)\n4. Function block fails with \"ReferenceError: __tag_webhook1_optional_field is not defined\"\n\n## Root Issue: IPC Serialization\n\nThe key problem occurs in `apps/sim/lib/execution/isolated-vm.ts`:\n\n```javascript\n// BROKEN - undefined values are stripped during JSON serialization in IPC\nworker!.send({ type: 'execute', executionId, request: req })\n```\n\nWhen `req.contextVariables` contains `{ __tag_webhook_id: undefined }`, the IPC serialization strips it, resulting in an empty object on the worker side.\n\n## Solution Applied\n\n### File 1: `apps/sim/lib/execution/isolated-vm.ts`\n\nAdd explicit sanitization BEFORE IPC transmission:\n\n```diff\n+ // Convert undefined values to null in contextVariables before IPC serialization.\n+ // JSON.stringify() strips keys with undefined values, which would cause ReferenceErrors\n+ // when user code references variables that weren't properly injected into the VM context.\n+ const sanitizedContextVariables: Record\u003cstring, unknown\u003e = {}\n+ for (const [key, value] of Object.entries(req.contextVariables)) {\n+ sanitizedContextVariables[key] = value === undefined ? null : value\n+ }\n+ const sanitizedReq = { ...req, contextVariables: sanitizedContextVariables }\n\n return new Promise((resolve) =\u003e {\n // ...\n try {\n- worker!.send({ type: 'execute', executionId, request: req })\n+ worker!.send({ type: 'execute', executionId, request: sanitizedReq })\n } catch {\n```\n\n### File 2: `apps/sim/app/api/function/execute/route.ts`\n\nFix E2B JavaScript and Python paths:\n\n```diff\n // E2B JavaScript path (around line 745)\n for (const [k, v] of Object.entries(contextVariables)) {\n- prologue += `const ${k} = JSON.parse(${JSON.stringify(JSON.stringify(v))});\\n`\n+ // Convert undefined to null so JSON.stringify produces valid output\n+ const safeValue = v === undefined ? null : v\n+ prologue += `const ${k} = JSON.parse(${JSON.stringify(JSON.stringify(safeValue))});\\n`\n prologueLineCount++\n }\n\n // E2B Python path (around line 817)\n for (const [k, v] of Object.entries(contextVariables)) {\n- prologue += `${k} = json.loads(${JSON.stringify(JSON.stringify(v))})\\n`\n+ // Convert undefined to null so JSON.stringify produces valid output\n+ const safeValue = v === undefined ? null : v\n+ prologue += `${k} = json.loads(${JSON.stringify(JSON.stringify(safeValue))})\\n`\n prologueLineCount++\n }\n```\n\n### File 3: `apps/sim/lib/execution/isolated-vm-worker.cjs`\n\nAdd defensive handling in worker:\n\n```diff\n for (const [key, value] of Object.entries(contextVariables)) {\n- await jail.set(key, new ivm.ExternalCopy(value).copyInto())\n+ // Convert undefined to null to ensure the variable is properly set in the VM\n+ const safeValue = value === undefined ? null : value\n+ await jail.set(key, new ivm.ExternalCopy(safeValue).copyInto())\n }\n```\n\n## Why This Fix Works\n\nThis ensures:\n- All variables are transmitted via IPC (null is serializable, undefined is not)\n- Worker receives all variables and can inject them into the VM context\n- User code can use nullish checks (`??`) on the received null values\n\n## Related Issues\n\n- #2794: Variable substitution breaks nullish coalescing operator - **this bug was discovered as a follow-up to that fix**\n\n## Testing\n\nVerify with:\n1. Start trigger + Extract Context referencing non-existent webhook fields\n2. Confirm `??` operator fallback works correctly\n3. Test all three execution paths (E2B JS, E2B Python, Isolated-VM)","author":{"url":"https://github.com/PlaneInABottle","@type":"Person","name":"PlaneInABottle"},"datePublished":"2026-01-13T21:31:31.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/2798/sim/issues/2798"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0fe68b31-486b-f6a6-c6ef-05b8246f4887 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C860:27025E:291C27:34BEEA:696B8037 |
| html-safe-nonce | 2a5cf5aea3361f3986a07a8350b472b09dee0d4defce45ef6df86e5700268325 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDODYwOjI3MDI1RToyOTFDMjc6MzRCRUVBOjY5NkI4MDM3IiwidmlzaXRvcl9pZCI6IjU3MzQ5MzcyMTk0NDUyMjgwNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 834620ff11a612558d2a31c1593efe4460b2b80c98868b31a550ed9bbfa6f0b9 |
| hovercard-subject-tag | issue:3810673515 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/simstudioai/sim/2798/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3c53d55a47447570451e2ab838b182180b242e932cbf88721ce69ff484098c6c/simstudioai/sim/issues/2798 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3c53d55a47447570451e2ab838b182180b242e932cbf88721ce69ff484098c6c/simstudioai/sim/issues/2798 |
| og:image:alt | Bug Description When function/condition blocks reference undefined block variables (e.g., |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | PlaneInABottle |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| turbo-cache-control | no-preview |
| go-import | github.com/simstudioai/sim git https://github.com/simstudioai/sim.git |
| octolytics-dimension-user_id | 199344406 |
| octolytics-dimension-user_login | simstudioai |
| octolytics-dimension-repository_id | 912559512 |
| octolytics-dimension-repository_nwo | simstudioai/sim |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 912559512 |
| octolytics-dimension-repository_network_root_nwo | simstudioai/sim |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width