Skip to content

Commit 10cb5b6

Browse files
committed
acked PR comments
1 parent 271fa93 commit 10cb5b6

File tree

2 files changed

+58
-30
lines changed

2 files changed

+58
-30
lines changed

apps/sim/lib/execution/isolated-vm-worker.cjs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,13 @@ async function executeCode(request) {
143143
}
144144
}, FETCH_TIMEOUT_MS)
145145
pendingFetches.set(fetchId, { resolve, timeout })
146-
process.send({ type: 'fetch', fetchId, requestId, url, optionsJson })
146+
if (process.send && process.connected) {
147+
process.send({ type: 'fetch', fetchId, requestId, url, optionsJson })
148+
} else {
149+
clearTimeout(timeout)
150+
pendingFetches.delete(fetchId)
151+
resolve(JSON.stringify({ error: 'Parent process disconnected' }))
152+
}
147153
})
148154
})
149155
await jail.set('__fetchRef', fetchCallback)
@@ -314,17 +320,38 @@ async function executeCode(request) {
314320
}
315321

316322
process.on('message', async (msg) => {
317-
if (msg.type === 'execute') {
318-
const result = await executeCode(msg.request)
319-
process.send({ type: 'result', executionId: msg.executionId, result })
320-
} else if (msg.type === 'fetchResponse') {
321-
const pending = pendingFetches.get(msg.fetchId)
322-
if (pending) {
323-
clearTimeout(pending.timeout)
324-
pendingFetches.delete(msg.fetchId)
325-
pending.resolve(msg.response)
323+
try {
324+
if (msg.type === 'execute') {
325+
const result = await executeCode(msg.request)
326+
if (process.send && process.connected) {
327+
process.send({ type: 'result', executionId: msg.executionId, result })
328+
}
329+
} else if (msg.type === 'fetchResponse') {
330+
const pending = pendingFetches.get(msg.fetchId)
331+
if (pending) {
332+
clearTimeout(pending.timeout)
333+
pendingFetches.delete(msg.fetchId)
334+
pending.resolve(msg.response)
335+
}
336+
}
337+
} catch (err) {
338+
if (msg.type === 'execute' && process.send && process.connected) {
339+
process.send({
340+
type: 'result',
341+
executionId: msg.executionId,
342+
result: {
343+
result: null,
344+
stdout: '',
345+
error: {
346+
message: err instanceof Error ? err.message : 'Worker error',
347+
name: 'WorkerError',
348+
},
349+
},
350+
})
326351
}
327352
}
328353
})
329354

330-
process.send({ type: 'ready' })
355+
if (process.send) {
356+
process.send({ type: 'ready' })
357+
}

apps/sim/lib/execution/isolated-vm.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type ChildProcess, spawn } from 'node:child_process'
22
import fs from 'node:fs'
33
import path from 'node:path'
4+
import { fileURLToPath } from 'node:url'
45
import { validateProxyUrl } from '@/lib/core/security/input-validation'
56
import { createLogger } from '@/lib/logs/console/logger'
67

@@ -140,14 +141,24 @@ function handleWorkerMessage(message: unknown) {
140141
}
141142
secureFetch(requestId, url, options)
142143
.then((response) => {
143-
worker?.send({ type: 'fetchResponse', fetchId, response })
144+
try {
145+
worker?.send({ type: 'fetchResponse', fetchId, response })
146+
} catch (err) {
147+
logger.error('Failed to send fetch response to worker', { err, fetchId })
148+
}
144149
})
145150
.catch((err) => {
146-
worker?.send({
147-
type: 'fetchResponse',
148-
fetchId,
149-
response: JSON.stringify({ error: err instanceof Error ? err.message : 'Fetch failed' }),
150-
})
151+
try {
152+
worker?.send({
153+
type: 'fetchResponse',
154+
fetchId,
155+
response: JSON.stringify({
156+
error: err instanceof Error ? err.message : 'Fetch failed',
157+
}),
158+
})
159+
} catch (sendErr) {
160+
logger.error('Failed to send fetch error to worker', { sendErr, fetchId })
161+
}
151162
})
152163
}
153164
}
@@ -160,21 +171,11 @@ async function ensureWorker(): Promise<void> {
160171
if (workerReadyPromise) return workerReadyPromise
161172

162173
workerReadyPromise = new Promise<void>((resolve, reject) => {
163-
const workerPath = path.resolve(
164-
process.cwd(),
165-
'apps',
166-
'sim',
167-
'lib',
168-
'execution',
169-
'isolated-vm-worker.cjs'
170-
)
174+
const currentDir = path.dirname(fileURLToPath(import.meta.url))
175+
const workerPath = path.join(currentDir, 'isolated-vm-worker.cjs')
171176

172177
if (!fs.existsSync(workerPath)) {
173-
reject(
174-
new Error(
175-
`Worker file not found at ${workerPath}. Ensure the file exists and process.cwd() (${process.cwd()}) is correct.`
176-
)
177-
)
178+
reject(new Error(`Worker file not found at ${workerPath}`))
178179
return
179180
}
180181

0 commit comments

Comments
 (0)