You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#!/usr/bin/env ruby
require 'bundler/setup'
Bundler.setup
require 'daemons'
at_exit do
begin
Process.waitpid
rescue Errno::ESRCH, Errno::ECHILD
end
end
2.times do |i|
Daemons.run_proc("test_daemon.#{i}") do
loop do
end
end
end
# 1. Forks a child (and exits the parent process, if needed)
# 2. Becomes a session leader (which detaches the program from
# the controlling terminal).
# 3. Forks another child process and exits first child. This prevents
# the potential of acquiring a controlling terminal.
# 4. Changes the current working directory to "/".
# 5. Clears the file creation mask (sets +umask+ to 0000).
# 6. Closes file descriptors (reopens +$stdout+ and +$stderr+ to point to a logfile if
# possible).
By the way, I encountered this problem when using delayed_job and debug.
Expected behavior
Child processes can be stopped.
Since the first child process should be stopped, I thought it would be a good idea to change the call to the exit! method.
Thank you for the report. I am not sure why you would want to use at_exit in such a scenario, but also I currently cannot follow on why the first child should not exit in this case? Do I understand correctly, that your proposed solution is to replace exit if pid == safefork by exit! if pid == safefork?
Yes.
I thought I should stop the first child process so that it is not affected by anything else.
I am not sure why you would want to use at_exit in such a scenario, but also I currently cannot follow on why the first child should not exit in this case?
This issue occurs with delayed_job and debug.
Increase the number of workers using delayed_job's number_of_workers option.
delayed_job tries to increase the number of workers, but the child process is not stopped, just like the repro program.
Also, in Rails development, debug is loaded by default, When starting delayed_job, unexpected block is caused by this issue.
Your environment
ruby -v
: ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]Describe the bug
Calling Process.waitpid inside the at_exit method will prevent the child process from stopping.
To Reproduce
Script:
Gemfile
test_daemon.rb
Terminal:
Child process doesn't stop.
The following fork operations are no longer possible.
daemons/lib/daemons.rb
Lines 43 to 51 in a0e84bc
By the way, I encountered this problem when using delayed_job and debug.
Expected behavior
Child processes can be stopped.
Since the first child process should be stopped, I thought it would be a good idea to change the call to the exit! method.
daemons/lib/daemons/daemonize.rb
Line 67 in a0e84bc
The text was updated successfully, but these errors were encountered: