in reply to Kill a child nicely

You could have the parent send a "GO AWAY" message to the child. That would be done in sequence and not kill the child in the middle of processing something else.

Something like this:

#!/usr/bin/perl use strict; # https://www.perlmonks.org/?node_id=11154445 use warnings; use IO::Socket; $SIG{__WARN__} = sub { die @_ }; my $port = 3333; # FIXME my $listen = IO::Socket::INET->new( LocalPort => $port, Listen => 100, ReusePort => 1,) or die $&; if( my $pid = fork ) # parent { print "Hit <ENTER> to quit\n"; <STDIN>; print "parent starting shutdown\n"; my $child = IO::Socket::INET->new("localhost:$port") or die $@; print $child "GO AWAY\n\n"; close $child; print "parent waiting for child to exit\n"; 1 while wait > 0; print "parent exiting\n"; exit; } elsif( defined $pid ) # child { my $more = 1; while( $more ) { my $live_socket = $listen->accept; if( $live_socket ) { my $input = ''; while( <$live_socket> ) { $input .= $_; /^\r?\n?\z/ and shutdown($live_socket, 1), last; } if( $input eq "GO AWAY\n\n" ) { $more = 0; } else { print "process 'do stuff' here with:\n$input"; } } else { print "child got $!\n"; } } print "child exiting\n"; exit; } else # bummer { die "fork failed $!"; }