@cryptotaxi247 / netdata-1 / commits / b94e93520

Improve agent shutdown on windows (#20672)

* Join thread created * Sleep in smaller steps to detect shutdown faster

Stelios Fragkakis committed Jul 14, 2025 at 17:58 UTC b94e93520e8e9aefcb6cfbb04e46ae49718c2904
1 file changed +26 -9
src/collectors/windows.plugin/windows_plugin.c
+26 -9
@@ -11,6 +11,7 @@ static struct proc_module {
11 int update_every;
12 int (*func)(int update_every, usec_t dt);
13 RRDDIM *rd;
14 + ND_THREAD *thread;
15 } win_modules[] = {
16
17 // system metrics
@@ -170,15 +171,22 @@ static void *windows_plugin_thread_worker(void *ptr __maybe_unused)
171 struct proc_module *mod = ptr;
172 heartbeat_t hb;
173 int update_every = mod->update_every;
173 - heartbeat_init(&hb, update_every * USEC_PER_SEC);
174
175 - while (service_running(SERVICE_COLLECTORS)) {
176 - usec_t hb_dt = heartbeat_next(&hb);
177 -
178 - if (unlikely(!service_running(SERVICE_COLLECTORS)))
179 - break;
175 + heartbeat_init(&hb, USEC_PER_SEC);
176 + usec_t step = USEC_PER_SEC * update_every;
177 + usec_t real_step = USEC_PER_SEC;
178
181 - mod->func(update_every, hb_dt);
179 + usec_t last = now_realtime_usec();
180 + while (service_running(SERVICE_COLLECTORS)) {
181 + heartbeat_next(&hb);
182 + if (real_step < step) {
183 + real_step += USEC_PER_SEC;
184 + continue;
185 + }
186 + real_step = USEC_PER_SEC;
187 + usec_t now = now_realtime_usec();
188 + mod->func(update_every, now - last);
189 + last = now;
190 }
191
192 return NULL;
@@ -199,6 +207,7 @@ void *win_plugin_main(void *ptr)
207 int update_every = localhost->rrd_update_every;
208 for (i = 0; win_modules[i].name; i++) {
209 struct proc_module *pm = &win_modules[i];
210 + pm->thread = NULL;
211
212 snprintfz(buf, CONFIG_MAX_NAME, "plugin:windows:%s", pm->name);
213
@@ -210,7 +219,7 @@ void *win_plugin_main(void *ptr)
219 if (pm->enabled && unlikely(update_every != pm->update_every)) {
220 char tag_name[ND_THREAD_TAG_MAX];
221 snprintfz(tag_name, ND_THREAD_TAG_MAX - 1, "WIN_PLUGIN[%d]", i);
213 - nd_thread_create(tag_name, NETDATA_THREAD_OPTION_DEFAULT, windows_plugin_thread_worker, pm);
222 + pm->thread = nd_thread_create(tag_name, NETDATA_THREAD_OPTION_DEFAULT, windows_plugin_thread_worker, pm);
223 }
224
225 worker_register_job_name(i, win_modules[i].dim);
@@ -241,7 +250,8 @@ void *win_plugin_main(void *ptr)
250 break;
251
252 struct proc_module *pm = &win_modules[i];
244 - if (unlikely(update_every != win_modules[i].update_every))
253 + // if we have a thread, we are already running it
254 + if (pm->thread)
255 continue;
256
257 if (unlikely(!pm->enabled))
@@ -253,5 +263,12 @@ void *win_plugin_main(void *ptr)
263 lgs[LGS_MODULE_ID] = ND_LOG_FIELD_TXT(NDF_MODULE, PLUGIN_WINDOWS_NAME);
264 }
265 }
266 +
267 + // Join threads
268 + for (i = 0; win_modules[i].name; i++) {
269 + struct proc_module *pm = &win_modules[i];
270 + if (pm->thread)
271 + nd_thread_join(pm->thread);
272 + }
273 return NULL;
274 }