jondot/sneakers

How/when does Sneakers use forking?

mltsy opened this issue · 3 comments

mltsy commented

There is very little mentioned in the documentation or website about how forking is used in Sneakers, which makes it hard to configure correctly. I'm trying to figure out issues we're having with our MongoDB connection, and at first I didn't realize Sneakers forked processes at all! Now I see that it does, but it's not clear how many or for what, or what the parent process does (if anything). Some documentation about how/when Sneakers forks, and possible considerations around forking would be very helpful!

For future readers and if it helps:

There's quite a lot of process management built into serverengine, which sneakers uses for forking and threading AFAICT.

As someone who uses systemd (or any other supervision or containerization tool) for process management, the default forking behavior is unexpected and a little annoying. Here's the diagram serverengine has in their README.

                  Heartbeat via pipe
                      & auto-restart
                 /                \               ---+
+------------+  /   +----------+   \  +--------+     |
| Supervisor |------|  Server  |------| Worker |     |
+------------+      +----------+\     +--------+     | Multi-process
                        /         \                  | or multi-thread
                       /            \ +--------+     |
      Dynamic reconfiguration         | Worker |     |
     and live restart support         +--------+     |
                                                  ---+

The setting that I've found to control the forking is workers. Any setting > 1 would be "multi-process", in which there are multiple processes being managed for the one parent that you started.

If you're using a process supervisor like upstart, systemd, containers, etc, you'll want workers: 1 in your sneakers config. daemonize: false is the other option you'll probably want so the process stays in the foreground.

With workers: 1, you'll still get one single fork. When you're using upstart, it might require some extra options to track the fork instead of the parent.

This line in sneakers provides worker_type as process to serverengine. Which produces a MultiProcessServer which inevitably forks at least 1 time.

mltsy commented

Ah! That's very helpful. I did proceed with an assumption that Sneakers was forking (since I was getting errors that looked like they were a result of forking). Maybe one of us can find some time to transfer this knowledge into a Wiki page! I feel like that would be useful.