Fork process from ruby server

Viewed 84

I am working on a legacy codebase where it consists of a ruby webserver, which when certain requests are called will start various different sub processes, some long running some short. In a lot of cases we were just calling these processes with backticks or sometimes just fork (no exec). For example:

get '/start_process' do
  # some code here
  fork do
    `long_process arg1`
  end
end

This would lead to issues with deploying changes and restarting our server, where if this long process was running at the time, it would also be listening on our servers port and as a result when we go to restart the server, it would fail as the port is already in use (by long_process). So we changed all these instances to use Ruby Kernel#spawn or do a fork and exec, so the new process will not have the same memory as the server. Like so:

get '/start_process' do
  spawn('long_process arg1')
end

However, when doing a deploy recently I noticed the same issue occurred where a subprocess of the server that was started using Kernel#spawn was listening on the server port, resulting in the restart issue. How could it be possible for a sub process that is fork and execed from the server be also listening on the server port? Is it possible, or do you reckon I must be missing something in my codebase?

Notes: Unfortunately I cannot share the details of the codebase, but the logic is not more complicated than what you see in the example. I am using a puma Sinatra server

Edit: I just had a look at fdinfo for my server and can see the following info

pos:    0
flags:  02000002
mnt_id: 10

Which should indicate flag O_CLOEXEC is set AFAIK (O_CLOEXEC value 02000000)

0 Answers
Related