Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare const __turbopack_external_require__: {
} & ((id: string, thunk: () => any, esm?: boolean) => any)

import type { Channel as Ipc } from '../types'
import { dirname, resolve as pathResolve, relative } from 'path'
import { resolve as pathResolve, relative } from 'path'
import {
StackFrame,
parse as parseStackTrace,
Expand Down
121 changes: 26 additions & 95 deletions turbopack/crates/turbopack-node/js/src/web_worker/evaluate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Binding, TaskChannel } from '../worker_threads/taskChannel'
import { structuredError } from '../error'
import type { Channel } from '../types'

Expand All @@ -16,122 +17,53 @@ export declare const self: Self
// @ts-ignore
const { workerId, poolId } = self.workerData

interface Binding {
recvWorkerRequest(poolId: string): Promise<number>
recvMessageInWorker(workerId: number): Promise<string>
notifyWorkerAck(taskId: number, workerId: number): Promise<void>
sendTaskMessage(taskId: number, message: string): Promise<void>
}

let binding: Binding = self.workerData.binding

const queue: string[][] = []

export const run = async (
moduleFactory: () => Promise<{
init?: () => Promise<void>
default: (channel: Channel<any, any>, ...deserializedArgs: any[]) => any
}>
) => {
const taskId = await binding.recvWorkerRequest(poolId)
let getValue: (channel: Channel<any, any>, ...deserializedArgs: any[]) => any

await binding.notifyWorkerAck(taskId, workerId)
let isRunning = false

let nextId = 1
const requests = new Map()
const internalIpc = {
sendInfo: (message: any) =>
binding.sendTaskMessage(
const run = async (taskId: number, args: string[]) => {
try {
if (typeof getValue !== 'function') {
const module = await moduleFactory()
if (typeof module.init === 'function') {
await module.init()
}
getValue = module.default
}
const value = await getValue(new TaskChannel(binding, taskId), ...args)
await binding.sendTaskMessage(
taskId,
JSON.stringify({
type: 'info',
data: message,
type: 'end',
data: value === undefined ? undefined : JSON.stringify(value),
duration: 0,
})
),
sendRequest: async (message: any) => {
const id = nextId++
let resolve, reject
const promise = new Promise((res, rej) => {
resolve = res
reject = rej
})
requests.set(id, { resolve, reject })
return binding
.sendTaskMessage(
taskId,
JSON.stringify({ type: 'request', id, data: message })
)
.then(() => promise)
},
sendError: async (error: Error) => {
try {
await binding.sendTaskMessage(
taskId,
JSON.stringify({
type: 'error',
...structuredError(error),
})
)
} catch (err) {
// There's nothing we can do about errors that happen after this point, we can't tell anyone
// about them.
console.error('failed to send error back to rust:', err)
}
},
}

let getValue: (channel: Channel<any, any>, ...deserializedArgs: any[]) => any
try {
const module = await moduleFactory()
if (typeof module.init === 'function') {
await module.init()
}
getValue = module.default
} catch (err) {
try {
)
} catch (err) {
await binding.sendTaskMessage(
taskId,
JSON.stringify({
type: 'error',
...structuredError(err as Error),
})
)
} catch (err) {
// There's nothing we can do about errors that happen after this point, we can't tell anyone
// about them.
console.error('failed to send error back to rust:', err)
}
}

let isRunning = false

const run = async () => {
while (queue.length > 0) {
const args = queue.shift()!
try {
const value = await getValue(internalIpc, ...args)
await binding.sendTaskMessage(
taskId,
JSON.stringify({
type: 'end',
data: value === undefined ? undefined : JSON.stringify(value),
duration: 0,
})
)
} catch (err) {
await binding.sendTaskMessage(
taskId,
JSON.stringify({
type: 'error',
...structuredError(err as Error),
})
)
}
}
isRunning = false
}

while (true) {
const taskId = await binding.recvWorkerRequest(poolId)

await binding.notifyWorkerAck(taskId, workerId)

const msg_str = await binding.recvMessageInWorker(workerId)

const msg = JSON.parse(msg_str) as
Expand All @@ -148,17 +80,16 @@ export const run = async (

switch (msg.type) {
case 'evaluate': {
queue.push(msg.args)
if (!isRunning) {
isRunning = true
run()
run(taskId, msg.args)
}
break
}
case 'result': {
const request = requests.get(msg.id)
const request = TaskChannel.requests.get(msg.id)
if (request) {
requests.delete(msg.id)
TaskChannel.requests.delete(msg.id)
if (msg.error) {
// Need to reject at next macro task queue, because some rejection callbacks is not registered when executing to here,
// that will cause the error be propergated to schedule thread, then causing panic.
Expand Down
121 changes: 25 additions & 96 deletions turbopack/crates/turbopack-node/js/src/worker_threads/evaluate.ts
Original file line number Diff line number Diff line change
@@ -1,125 +1,55 @@
import { threadId as workerId, workerData } from 'worker_threads'
import { structuredError } from '../error'
import type { Channel } from '../types'

interface Binding {
recvWorkerRequest(poolId: string): Promise<number>
recvMessageInWorker(workerId: number): Promise<string>
notifyWorkerAck(taskId: number, workerId: number): Promise<void>
sendTaskMessage(taskId: number, message: string): Promise<void>
}
import { Binding, TaskChannel } from './taskChannel'

const binding: Binding = require(
/* turbopackIgnore: true */ workerData.bindingPath
)

const queue: string[][] = []

export const run = async (
moduleFactory: () => Promise<{
init?: () => Promise<void>
default: (channel: Channel<any, any>, ...deserializedArgs: any[]) => any
}>
) => {
const taskId = await binding.recvWorkerRequest(workerData.poolId)
let getValue: (channel: Channel<any, any>, ...deserializedArgs: any[]) => any

await binding.notifyWorkerAck(taskId, workerId)
let isRunning = false

let nextId = 1
const requests = new Map()
const internalIpc = {
sendInfo: (message: any) =>
binding.sendTaskMessage(
const run = async (taskId: number, args: string[]) => {
try {
if (typeof getValue !== 'function') {
const module = await moduleFactory()
if (typeof module.init === 'function') {
await module.init()
}
getValue = module.default
}
const value = await getValue(new TaskChannel(binding, taskId), ...args)
await binding.sendTaskMessage(
taskId,
JSON.stringify({
type: 'info',
data: message,
type: 'end',
data: value === undefined ? undefined : JSON.stringify(value),
duration: 0,
})
),
sendRequest: async (message: any) => {
const id = nextId++
let resolve, reject
const promise = new Promise((res, rej) => {
resolve = res
reject = rej
})
requests.set(id, { resolve, reject })
return binding
.sendTaskMessage(
taskId,
JSON.stringify({ type: 'request', id, data: message })
)
.then(() => promise)
},
sendError: async (error: Error) => {
try {
await binding.sendTaskMessage(
taskId,
JSON.stringify({
type: 'error',
...structuredError(error),
})
)
} catch (err) {
// There's nothing we can do about errors that happen after this point, we can't tell anyone
// about them.
console.error('failed to send error back to rust:', err)
}
},
}

let getValue: (channel: Channel<any, any>, ...deserializedArgs: any[]) => any
try {
const module = await moduleFactory()
if (typeof module.init === 'function') {
await module.init()
}
getValue = module.default
} catch (err) {
try {
)
} catch (err) {
await binding.sendTaskMessage(
taskId,
JSON.stringify({
type: 'error',
...structuredError(err as Error),
})
)
} catch (err) {
// There's nothing we can do about errors that happen after this point, we can't tell anyone
// about them.
console.error('failed to send error back to rust:', err)
}
}

let isRunning = false

const run = async () => {
while (queue.length > 0) {
const args = queue.shift()!
try {
const value = await getValue(internalIpc, ...args)
await binding.sendTaskMessage(
taskId,
JSON.stringify({
type: 'end',
data: value === undefined ? undefined : JSON.stringify(value),
duration: 0,
})
)
} catch (err) {
await binding.sendTaskMessage(
taskId,
JSON.stringify({
type: 'error',
...structuredError(err as Error),
})
)
}
}
isRunning = false
}

while (true) {
const taskId = await binding.recvWorkerRequest(workerData.poolId)

await binding.notifyWorkerAck(taskId, workerId)
const msg_str = await binding.recvMessageInWorker(workerId)

const msg = JSON.parse(msg_str) as
Expand All @@ -136,17 +66,16 @@ export const run = async (

switch (msg.type) {
case 'evaluate': {
queue.push(msg.args)
if (!isRunning) {
isRunning = true
run()
run(taskId, msg.args)
}
break
}
case 'result': {
const request = requests.get(msg.id)
const request = TaskChannel.requests.get(msg.id)
if (request) {
requests.delete(msg.id)
TaskChannel.requests.delete(msg.id)
if (msg.error) {
// Need to reject at next macro task queue, because some rejection callbacks is not registered when executing to here,
// that will cause the error be propergated to schedule thread, then causing panic.
Expand Down
Loading
Loading