refactor(worker-threads): pass functions as strings to resolve Promise cloning issues

- Updated worker execution logic to pass functions as stringified versions instead of reference types to avoid cloning issues in worker threads.
- Refactored utility functions in `workerUtils` to accept serialized function strings.
- Adjusted embedding and augmentation pipeline workflows to align with the updated worker handling mechanism.
- Enhanced parallel distance calculator to support structured arguments with stringified distance functions for better compatibility.
This commit is contained in:
David Snelling 2025-06-24 10:38:05 -07:00
parent 887a08b689
commit d18a5a6c22
5 changed files with 92 additions and 73 deletions

View file

@ -601,7 +601,7 @@ export class AugmentationPipeline {
return augFn.apply(augmentation, workerArgs); return augFn.apply(augmentation, workerArgs);
}; };
methodPromise = executeInThread<AugmentationResponse<R>>(workerFn, ...args); methodPromise = executeInThread<AugmentationResponse<R>>(workerFn.toString(), args);
} catch (threadError) { } catch (threadError) {
console.warn(`Failed to execute in thread, falling back to main thread: ${threadError}`); console.warn(`Failed to execute in thread, falling back to main thread: ${threadError}`);
// Fall back to executing in the main thread // Fall back to executing in the main thread

View file

@ -69,17 +69,21 @@ export class HNSWIndex {
// Function to be executed in a worker thread // Function to be executed in a worker thread
const distanceCalculator = ( const distanceCalculator = (
query: Vector, args: {
items: Array<{ id: string; vector: Vector }>, queryVector: Vector,
distanceFn: string vectors: Array<{ id: string; vector: Vector }>,
distanceFnString: string
}
) => { ) => {
const { queryVector, vectors, distanceFnString } = args;
// Recreate the distance function from its string representation // Recreate the distance function from its string representation
const distanceFunction = new Function('return ' + distanceFn)() as DistanceFunction const distanceFunction = new Function('return ' + distanceFnString)() as DistanceFunction
// Calculate distances for all items // Calculate distances for all items
return items.map(item => ({ return vectors.map(item => ({
id: item.id, id: item.id,
distance: distanceFunction(query, item.vector) distance: distanceFunction(queryVector, item.vector)
})) }))
} }
@ -89,10 +93,8 @@ export class HNSWIndex {
// Execute the distance calculation in a separate thread // Execute the distance calculation in a separate thread
return await executeInThread<Array<{ id: string; distance: number }>>( return await executeInThread<Array<{ id: string; distance: number }>>(
distanceCalculator, distanceCalculator.toString(),
queryVector, { queryVector, vectors, distanceFnString }
vectors,
distanceFnString
) )
} catch (error) { } catch (error) {
console.error('Error in parallel distance calculation, falling back to sequential:', error) console.error('Error in parallel distance calculation, falling back to sequential:', error)

View file

@ -418,7 +418,7 @@ export class Pipeline implements IPipeline {
return augFn.apply(augmentation, workerArgs) return augFn.apply(augmentation, workerArgs)
} }
methodPromise = executeInThread<AugmentationResponse<T>>(workerFn, ...args) methodPromise = executeInThread<AugmentationResponse<T>>(workerFn.toString(), args)
} catch (threadError) { } catch (threadError) {
console.warn(`Failed to execute in thread, falling back to main thread: ${threadError}`) console.warn(`Failed to execute in thread, falling back to main thread: ${threadError}`)
// Fall back to executing in the main thread // Fall back to executing in the main thread

View file

@ -214,7 +214,15 @@ export function createThreadedEmbeddingFunction(
try { try {
// Function to be executed in a worker thread // Function to be executed in a worker thread
const embedInWorker = async (inputData: any) => { // This must be a regular function (not async) to avoid Promise cloning issues
const embedInWorker = (inputData: any) => {
// Return a plain object with the input data
// All async operations will be performed inside the worker
return { data: inputData }
}
// Worker implementation function that will be stringified and run in the worker
const workerImplementation = async ({ data }: { data: any }) => {
// We need to dynamically import TensorFlow.js and USE in the worker // We need to dynamically import TensorFlow.js and USE in the worker
const tf = await import('@tensorflow/tfjs') const tf = await import('@tensorflow/tfjs')
const use = await import('@tensorflow-models/universal-sentence-encoder') const use = await import('@tensorflow-models/universal-sentence-encoder')
@ -224,19 +232,19 @@ export function createThreadedEmbeddingFunction(
// Handle different input types // Handle different input types
let textToEmbed: string[] let textToEmbed: string[]
if (typeof inputData === 'string') { if (typeof data === 'string') {
if (inputData.trim() === '') { if (data.trim() === '') {
return new Array(512).fill(0) return new Array(512).fill(0)
} }
textToEmbed = [inputData] textToEmbed = [data]
} else if ( } else if (
Array.isArray(inputData) && Array.isArray(data) &&
inputData.every((item) => typeof item === 'string') data.every((item) => typeof item === 'string')
) { ) {
if (inputData.length === 0 || inputData.every((item) => item.trim() === '')) { if (data.length === 0 || data.every((item) => item.trim() === '')) {
return new Array(512).fill(0) return new Array(512).fill(0)
} }
textToEmbed = inputData.filter((item) => item.trim() !== '') textToEmbed = data.filter((item) => item.trim() !== '')
if (textToEmbed.length === 0) { if (textToEmbed.length === 0) {
return new Array(512).fill(0) return new Array(512).fill(0)
} }
@ -259,7 +267,8 @@ export function createThreadedEmbeddingFunction(
} }
// Execute the embedding function in a separate thread // Execute the embedding function in a separate thread
return await executeInThread<Vector>(embedInWorker, data) // Pass the worker implementation as a string to avoid Promise cloning issues
return await executeInThread<Vector>(workerImplementation.toString(), embedInWorker(data))
} catch (error) { } catch (error) {
// If threading fails and fallback is enabled, use the standard embedding function // If threading fails and fallback is enabled, use the standard embedding function
if (options.fallbackToMain) { if (options.fallbackToMain) {

View file

@ -2,24 +2,23 @@
* Utility functions for working with Web Workers and Worker Threads * Utility functions for working with Web Workers and Worker Threads
*/ */
import { isNode, isBrowser } from './environment.js'; import { isNode, isBrowser } from './environment.js'
/** /**
* Execute a function in a Web Worker (browser environment) * Execute a function in a Web Worker (browser environment)
* *
* @param fn The function to execute * @param fnString The function to execute as a string
* @param args The arguments to pass to the function * @param args The arguments to pass to the function
* @returns A promise that resolves with the result of the function * @returns A promise that resolves with the result of the function
*/ */
export function executeInWebWorker<T>(fn: Function, ...args: any[]): Promise<T> { export function executeInWebWorker<T>(fnString: string, args: any): Promise<T> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Create a blob URL for the worker script // Create a blob URL for the worker script
const fnString = fn.toString();
const workerScript = ` const workerScript = `
self.onmessage = function(e) { self.onmessage = async function(e) {
try { try {
const fn = ${fnString}; const fn = ${fnString};
const result = fn(...e.data); const result = await fn(e.data);
self.postMessage({ success: true, data: result }); self.postMessage({ success: true, data: result });
} catch (error) { } catch (error) {
self.postMessage({ self.postMessage({
@ -28,59 +27,63 @@ export function executeInWebWorker<T>(fn: Function, ...args: any[]): Promise<T>
}); });
} }
}; };
`; `
const blob = new Blob([workerScript], { type: 'application/javascript' }); const blob = new Blob([workerScript], { type: 'application/javascript' })
const blobURL = URL.createObjectURL(blob); const blobURL = URL.createObjectURL(blob)
// Create a worker // Create a worker
const worker = new Worker(blobURL); const worker = new Worker(blobURL)
// Set up message handling // Set up message handling
worker.onmessage = function(e: MessageEvent<{ success: boolean; data: T; error?: string }>) { worker.onmessage = function (
URL.revokeObjectURL(blobURL); // Clean up e: MessageEvent<{ success: boolean; data: T; error?: string }>
worker.terminate(); // Terminate the worker ) {
URL.revokeObjectURL(blobURL) // Clean up
worker.terminate() // Terminate the worker
if (e.data.success) { if (e.data.success) {
resolve(e.data.data); resolve(e.data.data)
} else { } else {
reject(new Error(e.data.error)); reject(new Error(e.data.error))
}
} }
};
worker.onerror = function(error: ErrorEvent) { worker.onerror = function (error: ErrorEvent) {
URL.revokeObjectURL(blobURL); // Clean up URL.revokeObjectURL(blobURL) // Clean up
worker.terminate(); // Terminate the worker worker.terminate() // Terminate the worker
reject(error); reject(error)
}; }
// Start the worker // Start the worker
worker.postMessage(args); worker.postMessage(args)
}); })
} }
/** /**
* Execute a function in a Worker Thread (Node.js environment) * Execute a function in a Worker Thread (Node.js environment)
* *
* @param fn The function to execute * @param fnString The function to execute as a string
* @param args The arguments to pass to the function * @param args The arguments to pass to the function
* @returns A promise that resolves with the result of the function * @returns A promise that resolves with the result of the function
*/ */
export function executeInWorkerThread<T>(fn: Function, ...args: any[]): Promise<T> { export function executeInWorkerThread<T>(
fnString: string,
args: any
): Promise<T> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
// Dynamic import to avoid errors in browser environments // Dynamic import to avoid errors in browser environments
const { Worker } = require('worker_threads'); const { Worker } = require('worker_threads')
// Create a worker script // Create a worker script
const fnString = fn.toString();
const workerScript = ` const workerScript = `
const { parentPort } = require('worker_threads'); const { parentPort } = require('worker_threads');
parentPort.once('message', (data) => { parentPort.once('message', async (data) => {
try { try {
const fn = ${fnString}; const fn = ${fnString};
const result = fn(...data); const result = await fn(data);
parentPort.postMessage({ success: true, data: result }); parentPort.postMessage({ success: true, data: result });
} catch (error) { } catch (error) {
parentPort.postMessage({ parentPort.postMessage({
@ -89,49 +92,54 @@ export function executeInWorkerThread<T>(fn: Function, ...args: any[]): Promise<
}); });
} }
}); });
`; `
// Create a worker // Create a worker
const worker = new Worker(workerScript, { eval: true }); const worker = new Worker(workerScript, { eval: true })
// Set up message handling // Set up message handling
worker.on('message', (data: { success: boolean; data: T; error?: string }) => { worker.on(
worker.terminate(); // Terminate the worker 'message',
(data: { success: boolean; data: T; error?: string }) => {
worker.terminate() // Terminate the worker
if (data.success) { if (data.success) {
resolve(data.data); resolve(data.data)
} else { } else {
reject(new Error(data.error)); reject(new Error(data.error))
} }
}); }
)
worker.on('error', (error: Error) => { worker.on('error', (error: Error) => {
worker.terminate(); // Terminate the worker worker.terminate() // Terminate the worker
reject(error); reject(error)
}); })
// Start the worker // Start the worker
worker.postMessage(args); worker.postMessage(args)
} catch (error) { } catch (error) {
reject(error); reject(error)
} }
}); })
} }
/** /**
* Execute a function in a separate thread based on the environment * Execute a function in a separate thread based on the environment
* *
* @param fn The function to execute * @param fnString The function to execute as a string
* @param args The arguments to pass to the function * @param args The arguments to pass to the function
* @returns A promise that resolves with the result of the function * @returns A promise that resolves with the result of the function
*/ */
export function executeInThread<T>(fn: Function, ...args: any[]): Promise<T> { export function executeInThread<T>(fnString: string, args: any): Promise<T> {
if (isBrowser()) { if (isBrowser()) {
return executeInWebWorker<T>(fn, ...args); return executeInWebWorker<T>(fnString, args)
} else if (isNode()) { } else if (isNode()) {
return executeInWorkerThread<T>(fn, ...args); return executeInWorkerThread<T>(fnString, args)
} else { } else {
// Fall back to executing in the main thread // Fall back to executing in the main thread
return Promise.resolve(fn(...args) as T); // Parse the function from string and execute it
const fn = new Function('return ' + fnString)()
return Promise.resolve(fn(args) as T)
} }
} }