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

Initial fork() support for Node.js #2635

Merged
merged 1 commit into from Feb 8, 2024
Merged

Conversation

janbiedermann
Copy link
Member

@janbiedermann janbiedermann commented Feb 2, 2024

Working, it may happen though, that the parent process exits before the child was able to read its javascript file.
Happens with SystemRunner and its tempfile, which gets deleted when the parent exits. It will fork and then print:
Error: Cannot find module '/tmp/opal-system-runner20240202-905467-v7fj7d.js'

In ruby, such situation will still work, because fork relies on the image in memory. In node, the original javascript file is still required when forking.

That of course is not a issue at all, if the parent process keeps running anyway.

This updates the node requires in nodejs/kernel.rb to use the modern form.

@janbiedermann
Copy link
Member Author

if RUBY_ENGINE == 'opal'
require 'nodejs'
end

def cpu_intensive_process
  puts "Pid: #{Process.pid}"
  x = 0
  1000000000.times do |i|
    x = i + x
  end
  puts x
end

# either this way, may fail with the error mentioned above, if there wouldn't be the other forks below
# keeping this process busy
fork do
  cpu_intensive_process
end

# or that way
fork
cpu_intensive_process

Example for testing

Copy link
Member

@elia elia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍴 ✅

@elia elia changed the title Initial fork() support for Node Initial fork() support for Node.js Feb 8, 2024
@hmdne hmdne merged commit 3cd09e9 into opal:master Feb 8, 2024
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants