From 19097c17a331a2d591b7cbcc41c9b60fb33aba6d Mon Sep 17 00:00:00 2001 From: Jash Date: Sun, 22 Nov 2026 03:03:33 +0530 Subject: [PATCH] fix: zmtrigger keeps each monitor's last state across a monitor reload so on/off notices aren't lost refs #7281 --- scripts/zmtrigger.pl.in | 11 ++++- .../perl/test_zmtrigger_reload_keeps_state.pl | 45 +++++++++++++++++++ tests/perl/zmtrigger_harness.pm | 2 +- 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 tests/perl/test_zmtrigger_reload_keeps_state.pl diff --git a/scripts/zmtrigger.pl.in b/scripts/zmtrigger.pl.in index e6e1852979..e8ece41331 100644 --- a/scripts/zmtrigger.pl.in +++ b/scripts/zmtrigger.pl.in @@ -302,6 +302,7 @@ exit; sub loadMonitors { $monitor_reload_time = time(); + my %old_monitors = %monitors; # Keep prior state/event so alarms that started or ended since the last poll are not swallowed %monitors = (); foreach my $monitor ( ZoneMinder::Monitor->find( Deleted=>0, @@ -309,8 +310,14 @@ sub loadMonitors { ($Config{ZM_SERVER_ID} ? (ServerId=>$Config{ZM_SERVER_ID}) : ()), )) { if ($monitor->connect()) { # This will re-init shared memory - $monitor->{LastState} = zmGetMonitorState($monitor); - $monitor->{LastEvent} = zmGetLastEvent($monitor); + my $old = $old_monitors{$monitor->{Id}}; + if ($old and defined($old->{LastState})) { + $monitor->{LastState} = $old->{LastState}; + $monitor->{LastEvent} = $old->{LastEvent}; + } else { + $monitor->{LastState} = zmGetMonitorState($monitor); + $monitor->{LastEvent} = zmGetLastEvent($monitor); + } } $monitors{$monitor->{Id}} = $monitor; } # end while fetchrow diff --git a/tests/perl/test_zmtrigger_reload_keeps_state.pl b/tests/perl/test_zmtrigger_reload_keeps_state.pl new file mode 100644 index 0000000000..61ed0caf44 --- /dev/null +++ b/tests/perl/test_zmtrigger_reload_keeps_state.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl +# +# A full monitor reload (a message for an unknown monitor id, enable/disable, +# the periodic reload) must not forget which state each monitor was last seen +# in. If it re-reads the state from shared memory, an alarm that ended since +# the last poll never sends its "off" and one that started never sends its +# "on" to the clients. +# +# Run as: perl tests/perl/test_zmtrigger_reload_keeps_state.pl +# ZMT_REPO= points it at another copy of the scripts (e.g. the original). +# +use strict; +use warnings; +use FindBin; +require "$FindBin::Bin/zmtrigger_harness.pm"; + +my $r = run_zmtrigger(<<'EOM'); +package ZmtSim; +our $max_ticks = 14; +our %rows = map { $_ => { Id => $_, Name => "m$_", Deleted => 0, Capturing => 'Always', Recording => 'None', ServerId => 0 } } 1 .. 2; +# A stray message for a monitor that does not exist makes zmtrigger reload all +# monitors, once while monitor 1's alarm has just ended, once while monitor 2's +# has just started. +our @inbox = ([6, '99|on|1|x'], [10, '99|on|1|x']); +sub t { return $ZoneMinder::tick; } +sub mem { + my ($id, $f) = @_; + my $t = t(); + my ($state, $event) = (1, 0); + if ($id == 1) { $event = $t >= 3 ? 10 : 0; $state = ($t >= 3 && $t <= 5) ? 3 : 1; } + if ($id == 2) { $event = $t >= 10 ? 20 : 0; $state = $t >= 10 ? 3 : 1; } + return $f eq 'state' ? $state : $event; +} +1; +EOM + +my %by; +foreach (grep { /^OUT / } @{$r->{lines}}) { + my $l = s/\|\d{7}\|/|T|/r; + $l =~ /^OUT (\d+)\|/ and push @{$by{$1}}, $l =~ s/^OUT //r; +} +check('daemon ran to the end', $r->{exit} . '/' . $r->{signal}, '0/0'); +check('alarm that ended just before a reload still sends off', join(',', @{$by{1} || []}), '1|on|T|10,1|off|T|10'); +check('alarm that started just before a reload still sends on', join(',', @{$by{2} || []}), '2|on|T|20'); +finish(); diff --git a/tests/perl/zmtrigger_harness.pm b/tests/perl/zmtrigger_harness.pm index 9c3741497e..089769e3f5 100644 --- a/tests/perl/zmtrigger_harness.pm +++ b/tests/perl/zmtrigger_harness.pm @@ -24,7 +24,7 @@ use File::Temp qw(tempdir); use File::Path qw(make_path); use FindBin; -my $repo = "$FindBin::Bin/../.."; +my $repo = $ENV{ZMT_REPO} // "$FindBin::Bin/../.."; sub write_file { my ($path, $content) = @_;