Properly reap child processes.
authorMahlon E. Smith <mahlon@laika.com>
Sun, 18 Feb 2018 22:18:44 -0800
changeset 9 aa9d537f7067
parent 8 1ef3f2d6d10e
child 10 252cdb26f76b
Properly reap child processes.
Makefile
netdata_tsrelay.nim
--- a/Makefile	Sun Feb 18 18:16:37 2018 -0800
+++ b/Makefile	Sun Feb 18 22:18:44 2018 -0800
@@ -4,17 +4,17 @@
 default: development
 
 debug: ${FILES}
-	nim --assertions:on --threads:on --nimcache:.cache c ${FILES}
+	nim --assertions:on --nimcache:.cache c ${FILES}
 
 development: ${FILES}
 	# can use gdb with this...
-	nim --debugInfo --threads:on --linedir:on -d:testing -d:nimTypeNames --nimcache:.cache c ${FILES}
+	nim --debugInfo --linedir:on -d:testing -d:nimTypeNames --nimcache:.cache c ${FILES}
 
 debugger: ${FILES}
-	nim --debugger:on --threads:on --nimcache:.cache c ${FILES}
+	nim --debugger:on --nimcache:.cache c ${FILES}
 
 release: ${FILES}
-	nim -d:release --opt:speed --parallelBuild:0 --threads:on --nimcache:.cache c ${FILES}
+	nim -d:release --opt:speed --parallelBuild:0 --nimcache:.cache c ${FILES}
 
 docs:
 	nim doc ${FILES}
--- a/netdata_tsrelay.nim	Sun Feb 18 18:16:37 2018 -0800
+++ b/netdata_tsrelay.nim	Sun Feb 18 22:18:44 2018 -0800
@@ -1,5 +1,4 @@
 # vim: set et nosta sw=4 ts=4 :
-# im: set et nosta sw=4 ts=4 ft=nim : 
 #
 # Copyright (c) 2018, Mahlon E. Smith <mahlon@martini.nu>
 # All rights reserved.
@@ -76,6 +75,7 @@
 # Global configuration
 var conf: Config
 
+
 proc hl( msg: string, fg: ForegroundColor, bright=false ): string =
     ## Quick wrapper for color formatting a string, since the 'terminal'
     ## module only deals with stdout directly.
@@ -171,8 +171,7 @@
 
 
 proc process( client: Socket, address: string ): void =
-    ## Do the work for a connected client within a thread.
-    ## Returns the formatted json data keyed on sample time.
+    ## Do the work for a connected client within child process.
     let t0 = cpu_time()
     var raw_data = client.fetch_data
 
@@ -205,8 +204,14 @@
     ## Open a database connection, bind to the listening socket,
     ## and start serving incoming netdata streams.
     let db = open( "", "", "", conf.dbopts )
+    db.close
     if conf.verbose: echo( "Successfully tested connection to the backend database.".hl( fgGreen ) )
-    db.close
+
+    # Ensure children are properly reaped.
+    #
+    var sa: Sigaction
+    sa.sa_handler = SIG_IGN
+    discard sigaction( SIGCHLD, sa )
 
     # Setup listening socket.
     #
@@ -230,9 +235,9 @@
         var
             client  = new Socket
             address = ""
-            status: cint = 0
 
-        server.acceptAddr( client, address ) # block
+        # Block, waiting for new connections.
+        server.acceptAddr( client, address )
 
         if fork() == 0:
             server.close
@@ -240,8 +245,6 @@
             quit( 0 )
 
         client.close
-
-        discard waitpid( P_ALL, status, WNOHANG ) # reap all previous children
         when defined( testing ): dumpNumberOfInstances()
 
 
@@ -294,9 +297,7 @@
 
 when isMainModule:
     system.addQuitProc( resetAttributes )
-
     conf = parse_cmdline()
     if conf.debug: echo hl( $conf, fgYellow )
-
     serverloop( conf )