Skip to content

Commit fd86b35

Browse files
committed
bug logging: do not ignore SIGUSR1 during boot
commit_hash:d1a54d592ce6084740c28c1b8c0289ff28594922
1 parent df13e31 commit fd86b35

2 files changed

Lines changed: 15 additions & 14 deletions

File tree

core/src/logging/component.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <userver/alerts/source.hpp>
1111
#include <userver/components/component.hpp>
12+
#include <userver/components/scope.hpp>
1213
#include <userver/components/statistics_storage.hpp>
1314
#include <userver/engine/async.hpp>
1415
#include <userver/engine/sleep.hpp>
@@ -67,17 +68,25 @@ void ReportReopeningErrorAndThrow(
6768

6869
} // namespace
6970

70-
/// [Signals sample - init]
7171
Logging::Logging(const ComponentConfig& config, const ComponentContext& context)
7272
: fs_task_processor_{GetFsTaskProcessor(config, context)},
73-
metrics_storage_(context.FindComponent<components::StatisticsStorage>().GetMetricsStorage()),
74-
signal_subscriber_(context.FindComponent<os_signals::ProcessorComponent>()
75-
.Get()
76-
.AddListener(this, kName, os_signals::kSigUsr1, &Logging::OnLogRotate))
77-
/// [Signals sample - init]
73+
metrics_storage_(context.FindComponent<components::StatisticsStorage>().GetMetricsStorage())
7874
{
7975
try {
8076
Init(config, context);
77+
78+
/// [Signals sample - init]
79+
auto& signals_processor = context.FindComponent<os_signals::ProcessorComponent>().Get();
80+
context.RegisterScope(MakeScope([this, &signals_processor] {
81+
auto holder = signals_processor.AddListener(this, kName, os_signals::kSigUsr1, &Logging::OnLogRotate);
82+
83+
// Force logrotate just after signal subscription to be sure we haven't lost signals during the loading.
84+
// If there were no signals, it's OK to re-open log files one more time.
85+
OnLogRotate();
86+
87+
return holder;
88+
}));
89+
/// [Signals sample - init]
8190
} catch (const std::exception&) {
8291
Stop();
8392
throw;
@@ -157,11 +166,6 @@ void Logging::Init(const ComponentConfig& config, const ComponentContext& contex
157166
Logging::~Logging() { Stop(); }
158167

159168
void Logging::Stop() noexcept {
160-
/// [Signals sample - destr]
161-
162-
signal_subscriber_.Unsubscribe();
163-
164-
/// [Signals sample - destr]
165169
flush_task_.Stop();
166170

167171
// Loggers could be used from non coroutine environments and should be

scripts/docs/en/userver/os_signals.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ after executing [logrotate](https://github.com/logrotate/logrotate).
2828
components::Logging uses the following code:
2929
@snippet core/src/logging/component.cpp Signals sample - init
3030

31-
3. In the component's destructor, unsubscribe from the signal
32-
@snippet core/src/logging/component.cpp Signals sample - destr
33-
3431

3532
----------
3633

0 commit comments

Comments
 (0)