Skip to content

Commit 46cb836

Browse files
author
tonvoon
committed
Fixed memory leak in NRD::Client, with test to catch in future
1 parent 85e5105 commit 46cb836

File tree

11 files changed

+87
-9
lines changed

11 files changed

+87
-9
lines changed

Changes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Revision history for Perl module NSCA2::Daemon
1+
Revision history for Perl module NRD::Daemon
22

33
0.01 Fri Jul 3 18:49:18 2009
44
- original version; created by ExtUtils::ModuleMaker 0.51

Makefile.PL

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ WriteMakefile(
1919
'Crypt::CBC' => 0, # for testing. optional for operation
2020
'Crypt::Blowfish' => 0, # for testing. optional for operation
2121
'JSON::XS' => 0,
22+
'Test::Memory::Cycle' => 1.04, # for testing only
23+
'Scalar::Util' => 1.23,
2224
},
2325
);

README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pod2text NSCA2::Daemon.pm > README
1+
pod2text NRD::Daemon.pm > README
22

33
If this is still here it means the programmer was too lazy to create the readme file.
44

Todo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
TODO list for Perl module NSCA2::Daemon
1+
TODO list for Perl module NRD::Daemon
22

33
- Streaming lots of results
44

lib/NRD/Client.pm

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use IO::Socket;
77
use NRD::Packet;
88
use NRD::Serialize;
99
use Carp;
10+
use Scalar::Util qw(weaken);
1011

1112
=item new( \%options )
1213
@@ -92,11 +93,15 @@ sub connect {
9293
if ($self->{timeout}) {
9394
alarm( $self->{timeout} );
9495
}
95-
print $sock $data;
96+
my $s = $self->{sock};
97+
print $s $data;
9698
if ($self->{timeout}) {
9799
alarm(0);
98100
}
99101
};
102+
# Need to weaken $self as it is in the send_sock routine. Tested in t/005_client.t
103+
weaken($self);
104+
100105
$self->{sock} = $sock;
101106
if ($self->{serializer}->needs_helo) {
102107
$self->{send_sock}->($self->{packer}->pack( $self->{serializer}->helo ));
@@ -158,6 +163,10 @@ sub end {
158163
};
159164
my $error = $@;
160165
close $self->{sock};
166+
167+
# This undef is not strictly necessary, but it stops Memory::Cycle from erroring re: the GLOB variable
168+
$self->{sock} = undef;
169+
161170
alarm(0) if $self->{timeout};
162171
croak $error if $error;
163172

t/005_client.t

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/perl
2+
#
3+
# DESCRIPTION:
4+
# Test client for memory leaks
5+
#
6+
# LICENCE:
7+
# GNU GPLv2
8+
9+
use lib 't';
10+
11+
use strict;
12+
use NSCATest;
13+
use Test::More;
14+
use Net::Server::Fork;
15+
use Test::Memory::Cycle;
16+
17+
plan tests => 2;
18+
19+
my $host = 'localhost';
20+
my $port = 7669;
21+
22+
23+
my $nsca = NSCATest->new( config => "plain" );
24+
$nsca->start("--server_type=Single");
25+
26+
sleep 1;
27+
28+
my $data = [
29+
["hostname", "0", "Plugin output"],
30+
["long_output", 0, 'x' x 10240 ],
31+
["hostname-with-other-bits", "1", "More data to be read"],
32+
["hostname.here", "2", "Check that ; are okay to receive"],
33+
["host", "service", 0, "A good result here"],
34+
["host54", "service with spaces", 1, "Warning! My flies are undone!"],
35+
["host-robin", "service with a :)", 2, "Critical? Alert! Alert!"],
36+
["host-batman", "another service", 3, "Unknown - the only way to travel"],
37+
["long_output", "service1", 0, 'x' x 10240 ], #10K of plugin output
38+
['x' x 1000, 0, 'Host check with big hostname'],
39+
['x' x 1000, 'service OK', 0, 'Service check with big hostname'],
40+
['long_svc_name', 'x' x 1000, 0, 'Service check with big service name'],
41+
];
42+
43+
# When the NRD server cuts a connection we get a SIG_PIPE. Perls default is to die...
44+
$SIG{'PIPE'} = 'IGNORE';
45+
46+
use NRD::Client;
47+
48+
my $client;
49+
eval {
50+
$client = NRD::Client->new( { timeout => 5, serializer => "plain" } );
51+
$client->connect( PeerAddr => "127.0.0.1", PeerPort => $port, Proto => "tcp" );
52+
foreach my $d ( @$data ) {
53+
$client->send_result( {
54+
command => "result",
55+
data => {
56+
time => time(),
57+
%{ $client->{serializer}->from_line( join("\t", @$d) ) },
58+
},
59+
} );
60+
}
61+
$client->end;
62+
};
63+
is( $@, '', "No errors" );
64+
memory_cycle_ok( $client, "No memory cycles for client" );
65+
66+
$nsca->stop;
67+

t/send_digest.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
####################################################
2-
# Sample NSCA2 Client Config File
2+
# Sample NRD Client Config File
33
####################################################
44

55
host 127.0.0.1

t/send_encrypt.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
####################################################
2-
# Sample NSCA2 Client Config File
2+
# Sample NRD Client Config File
33
####################################################
44

55
host 127.0.0.1

t/send_plain.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
####################################################
2-
# Sample NSCA2 Client Config File
2+
# Sample NRD Client Config File
33
####################################################
44

55
host 127.0.0.1

t/send_resultdir.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
####################################################
2-
# Sample NSCA2 Client Config File
2+
# Sample NRD Client Config File
33
####################################################
44

55
host 127.0.0.1

0 commit comments

Comments
 (0)