Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker Thread using VM to run code fails #56440

Open
d3x0r opened this issue Jan 2, 2025 · 0 comments
Open

Worker Thread using VM to run code fails #56440

d3x0r opened this issue Jan 2, 2025 · 0 comments
Labels
vm Issues and PRs related to the vm subsystem. worker Issues and PRs related to Worker support.

Comments

@d3x0r
Copy link
Contributor

d3x0r commented Jan 2, 2025

Version

23.5.0

Platform

Windows 11 Pro

Subsystem

No response

What steps will reproduce the bug?

I am working on using the VM module in worker_threads, and ran into an issue... I experimented a while to find a minimal case that would show me what I was missing... https://gist.github.com/d3x0r/c88e4cbadb37a3f4797ca40941ac3438 This is all of the approaches that I tried, and in the end I found

import { Script, constants, createContext, runInContext } from 'node:vm';
import vm from "vm";
import wt from "worker_threads";


console.log( "Starting worker 4" );
const w4 = new wt.Worker( 
				'const a = await import("node:fs").then(({readFile}) => readFile instanceof Function); \
		import {parentPort} from "worker_threads";   \
		import vm from "vm"; \
		const context = vm.createContext( {data:"yes"}, { importModuleDynamically: vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER } ); \
          \
		const b = vm.runInContext(  `import("node:fs").then(({readFile}) => readFile instanceof Function);`  \
				, context, { importModuleDynamically: vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER } );        \
          \
			parentPort.postMessage( {op:"log", f:["textScript 2", a, b] });'
			, {
				eval:true,
				execArgv : ['--input-type=module'],
				stderr:true,
				stdin:true,
				stdout:true,
			})
w4.on( "message", (msg)=>{
	console.log( "message:", msg );
} );

I tested all of the components of the above, the vm.runInContext works, the wt.Worker works, but using vm.runInContext in wt.Worker fails...

what I'm getting for an error in the application I'm developing this for I'm getting

WOrker started?
Error from thread: sandboxPrerun.js:17
import util from "util";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at new Script (node:vm:117:7)
    at createScript (node:vm:269:10)
    at Object.runInContext (node:vm:300:10)
    at file:///M:/javascript/dekcore4.os/startup/[eval1]:110:5
    at ModuleJob.run (node:internal/modules/esm/module_job:272:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:223:26)
    at async ModuleLoader.eval (node:internal/modules/esm/loader:219:20)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:98:5)
uv loop at [000000EB105FF748] has open handles:
[000002C9F97725D0] async (active)
	Close callback: 0000000000000000 
	Data: 000002C9F97725C0 
	(First field): 000002C9F9710890 
[000002C9F9772D40] async (active)
	Close callback: 0000000000000000 
	Data: 000002C9F9772D30 
	(First field): 000002C9F97108D0 
uv loop at [000000EB105FF748] has 2 open handles in total

  #  go.2.bat >err 2>&1 - Far 3.0.6161.0 x64[60876]: void __cdecl node::CheckedUvLoopClose(struct uv_loop_s *) at c:\ws\src\debug_utils.cc:352
  #  Assertion failed: "Unreachable code reached" __VA_OPT__(": ") "uv_loop_close() while having open handles"

----- Native stack trace -----

 1: 00007FF7EC0064BD void __cdecl node::SetCppgcReference(class v8::Isolate * __ptr64,class v8::Local<class v8::Object>,void * __ptr64)+16957
 2: 00007FF7EBF6D061 EVP_MD_meth_get_input_blocksize+90625
 3: 00007FF7EC00607B void __cdecl node::SetCppgcReference(class v8::Isolate * __ptr64,class v8::Local<class v8::Object>,void * __ptr64)+15867
 4: 00007FF7EBE31CED RSA_meth_get_flags+210749
 5: 00007FF7EBE25ED5 RSA_meth_get_flags+162085
 6: 00007FF7EC0668B3 uv_poll_stop+291
 7: 00007FF7ED95FE4E inflateValidate+168766
 8: 00007FFBFDAA259D BaseThreadInitThunk+29
 9: 00007FFBFF44AF38 RtlUserThreadStart+40

which is a bad crash, what I get from the above example script is

message: { op: 'log', f: [ 'textScript 2', true ] }
node:internal/event_target:1101
  process.nextTick(() => { throw err; });
                           ^

Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({})
    at Worker.emit (node:events:502:17)
    at [kOnErrorMessage] (node:internal/worker:326:10)
    at [kOnMessage] (node:internal/worker:337:37)
    at MessagePort.<anonymous> (node:internal/worker:232:57)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:827:20)
    at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28) {
  code: 'ERR_UNHANDLED_ERROR',
  context: {}
}

Node.js v23.5.0

it's possible there's a syntax error in the example, but I'm not seeing it... the quotes should be nested so it's a simple translation...

How often does it reproduce? Is there a required condition?

Always with the example script. I was also testing with Script() instead of VM and it was also failing.

What is the expected behavior? Why is that the expected behavior?

vm and worker_thread can each use script code that can use import and is a module... but somehow vm IN worker_thread can't?

What do you see instead?

(described above)

Additional information

No response

@juanarbol juanarbol added vm Issues and PRs related to the vm subsystem. worker Issues and PRs related to Worker support. labels Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vm Issues and PRs related to the vm subsystem. worker Issues and PRs related to Worker support.
Projects
None yet
Development

No branches or pull requests

2 participants