From 61241defa175ea9f61eb45331e0f23fd1c8e107b Mon Sep 17 00:00:00 2001 From: "Mahlon E. Smith" Date: Sun, 18 Feb 2018 22:18:44 -0800 Subject: [PATCH] Properly reap child processes. --- Makefile | 8 ++++---- netdata_tsrelay.nim | 21 +++++++++++---------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 9cbe6c2..571ad3b 100644 --- a/Makefile +++ b/Makefile @@ -4,17 +4,17 @@ FILES = netdata_tsrelay.nim 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} diff --git a/netdata_tsrelay.nim b/netdata_tsrelay.nim index 557f843..651a2e3 100644 --- a/netdata_tsrelay.nim +++ b/netdata_tsrelay.nim @@ -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 # All rights reserved. @@ -76,6 +75,7 @@ type # 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 write_to_database( samples: seq[ JsonNode ] ): void = 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 @@ proc serverloop( conf: Config ): void = ## Open a database connection, bind to the listening socket, ## and start serving incoming netdata streams. let db = open( "", "", "", conf.dbopts ) - if conf.verbose: echo( "Successfully tested connection to the backend database.".hl( fgGreen ) ) db.close + if conf.verbose: echo( "Successfully tested connection to the backend database.".hl( fgGreen ) ) + + # Ensure children are properly reaped. + # + var sa: Sigaction + sa.sa_handler = SIG_IGN + discard sigaction( SIGCHLD, sa ) # Setup listening socket. # @@ -230,9 +235,9 @@ proc serverloop( conf: Config ): void = 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 @@ proc serverloop( conf: Config ): void = quit( 0 ) client.close - - discard waitpid( P_ALL, status, WNOHANG ) # reap all previous children when defined( testing ): dumpNumberOfInstances() @@ -294,9 +297,7 @@ proc parse_cmdline: Config = when isMainModule: system.addQuitProc( resetAttributes ) - conf = parse_cmdline() if conf.debug: echo hl( $conf, fgYellow ) - serverloop( conf )