From 3858a2920eda544cb81381a5c84238f52c0c93ba Mon Sep 17 00:00:00 2001 From: Jan Grashoefer Date: Fri, 11 Apr 2025 13:06:33 +0200 Subject: [PATCH] Prevent event timestamps set to future For scheduled events, the event timestamp is the intended timestamp. If we force timer expiration, the timestamp might be in the future. Today, this happens on shutdown. This change guarantees that event timestamps are never set beyond network time. --- src/Expr.cc | 9 +++++++-- .../broker.remote_event_schedule_ts/send.send.out | 4 ++-- testing/btest/Baseline/language.event-ts-scheduled/out | 4 ++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Expr.cc b/src/Expr.cc index 88c2bbd759..9e872d846f 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -3829,8 +3829,13 @@ ScheduleTimer::ScheduleTimer(const EventHandlerPtr& arg_event, Args arg_args, do : Timer(t, TIMER_SCHEDULE), event(arg_event), args(std::move(arg_args)) {} void ScheduleTimer::Dispatch(double /* t */, bool /* is_expire */) { - if ( event ) - event_mgr.Enqueue(event, std::move(args), util::detail::SOURCE_LOCAL, 0, nullptr, this->Time()); + if ( event ) { + // An event's intended timestamp might be in the past as timer expiration is driven by + // network time. Guarantee that the intended timestamp is never in the future (e.g., + // when all timers are expired on shutdown). + auto ts = std::min(this->Time(), run_state::network_time); + event_mgr.Enqueue(event, std::move(args), util::detail::SOURCE_LOCAL, 0, nullptr, ts); + } } ScheduleExpr::ScheduleExpr(ExprPtr arg_when, EventExprPtr arg_event) diff --git a/testing/btest/Baseline/broker.remote_event_schedule_ts/send.send.out b/testing/btest/Baseline/broker.remote_event_schedule_ts/send.send.out index 68fdce6f5b..2135ed1cd8 100644 --- a/testing/btest/Baseline/broker.remote_event_schedule_ts/send.send.out +++ b/testing/btest/Baseline/broker.remote_event_schedule_ts/send.send.out @@ -39,5 +39,5 @@ sender added peer: endpoint=127.0.0.1 msg=handshake successful >> Run 10 (1989-12-13-07:00:00) >>> Publish my-message-a intended for 1989-12-13-07:00:00 (current_event_time=1989-12-13-07:00:00, network_time=1989-12-13-07:00:00) sender lost peer: endpoint=127.0.0.1 msg=lost connection to remote peer ->>> Publish my-message-b intended for 1989-12-13-07:15:00 (current_event_time=1989-12-13-07:15:00, network_time=1989-12-13-07:00:00) ->>> Publish my-message-c intended for 1989-12-13-07:30:00 (current_event_time=1989-12-13-07:30:00, network_time=1989-12-13-07:00:00) +>>> Publish my-message-b intended for 1989-12-13-07:15:00 (current_event_time=1989-12-13-07:00:00, network_time=1989-12-13-07:00:00) +>>> Publish my-message-c intended for 1989-12-13-07:30:00 (current_event_time=1989-12-13-07:00:00, network_time=1989-12-13-07:00:00) diff --git a/testing/btest/Baseline/language.event-ts-scheduled/out b/testing/btest/Baseline/language.event-ts-scheduled/out index 6890f28697..0b94b35b83 100644 --- a/testing/btest/Baseline/language.event-ts-scheduled/out +++ b/testing/btest/Baseline/language.event-ts-scheduled/out @@ -37,5 +37,5 @@ [1989-12-13-07:00:00] Test was scheduled at 1989-12-13-06:00:00 for 1989-12-13-06:30:00 >> Run 9 (1989-12-13-07:00:00) << Run 9 (1989-12-13-07:00:00) -[1989-12-13-07:00:00] Test was scheduled at 1989-12-13-07:00:00 for 1989-12-13-07:15:00 -[1989-12-13-07:00:00] Test was scheduled at 1989-12-13-07:00:00 for 1989-12-13-07:30:00 +[1989-12-13-07:00:00] Test was scheduled at 1989-12-13-07:00:00 for 1989-12-13-07:00:00 +[1989-12-13-07:00:00] Test was scheduled at 1989-12-13-07:00:00 for 1989-12-13-07:00:00