SeattleTestbed / nodemanager

Remote control server for SeattleTestbed nodes
MIT License
0 stars 10 forks source link

`nmmain` self-daemonizing is memory inefficient #117

Open aaaaalbert opened 9 years ago

aaaaalbert commented 9 years ago

Unless instructed otherwise via the --foreground command-line flag, the nodemanager automatically daemonizes after starting up. This puts it in the background and decouples it from the controlling terminal (if any). (Otherwise, running start_seattle.sh or python nmmain.py and later closing the terminal would kill the nodemanager.)

In order to daemonize, the nodemanager fork()s twice and redirects its stdstreams from / to /dev/null effectively. Only the latter of the forked children is kept alive, the parent and first child processes both exit.

On systems with small amounts of available memory such as WiFi routers running OpenWrt, fork()ing is problematic: The parent nodemanager process consumes significant amounts of RAM already, and RAM consuption increases linearly with every fork as a copy of the parent process' memory image is created. Due to the typically low CPU speed of these devices, this transient spike in RAM consumption can lock up the system for minutes, or even cause memory exhaustion which crashes the nodemanager.


An immediate solution would be to not self-daemonize, but rather use common POSIX tools to achieve the same thing:

nohup python nmmain.py </dev/null >/dev/null 2>&1 &

(nohup disables signal forwarding from the terminal to the process, the <, > redirect stdin and stdout so that the process doesn't read from or write to the terminal anymore, 2>&1 redirects stderr into the already-redirected stdout, and & puts the whole thing in the background).

This snippet would render self-daemonizing in nmmain.py unneccessary, and would replace backgrounding without detaching in the Seattle start shell script.