Exporting exit fix (#20191)
* make the exporting engine join its worker threads on exit * remove obsolete function
Costa Tsaousis committed
Apr 28, 2025 at 11:17 UTC
5e87a65037d7398d34a7d9d9b00674275efebeaf
2 files changed
+26
-13
src/exporting/exporting_engine.c
+24
-13
@@ -131,33 +131,44 @@ static void exporting_main_cleanup(void *pptr)
131
132
engine->exit = 1;
133
134
- int found = 0;
135
- usec_t max = 2 * USEC_PER_SEC, step = 50000;
134
+ size_t all = 0, exited = 0;
135
136
for (struct instance *instance = engine->instance_root; instance; instance = instance->next) {
137
+ all++;
138
+
139
if (!instance->exited) {
139
- found++;
140
- netdata_log_info("stopping worker for instance %s", instance->config.name);
140
+ netdata_log_info("EXPORTING: signaling worker '%s' to stop...", instance->config.name);
141
uv_mutex_unlock(&instance->mutex);
142
instance->data_is_ready = 1;
143
uv_cond_signal(&instance->cond_var);
144
- } else
145
- netdata_log_info("found stopped worker for instance %s", instance->config.name);
144
+ }
145
+ else
146
+ netdata_log_info("EXPORTING: found worker '%s' already stopped", instance->config.name);
147
}
148
148
- while (found && max > 0) {
149
- max -= step;
150
- netdata_log_info("Waiting %d exporting connectors to finish...", found);
151
- sleep_usec(step);
152
- found = 0;
149
+ size_t iterations = 0;
150
+ while (exited < all) {
151
+ iterations++;
152
+ microsleep(10 * USEC_PER_MS);
153
154
+ exited = 0;
155
for (struct instance *instance = engine->instance_root; instance; instance = instance->next) {
155
- if (!instance->exited)
156
- found++;
156
+ if (instance->exited) {
157
+ exited++;
158
+
159
+ if(instance->thread) {
160
+ uv_thread_join(&instance->thread);
161
+ instance->thread = 0;
162
+ }
163
+ }
164
+ else if(iterations % 100 == 0)
165
+ netdata_log_info("EXPORTING: still waiting for worker '%s' to exit...", instance->config.name);
166
}
167
}
168
169
+ // this must be called once all the worker thread have exited
170
exporting_clean_engine();
171
+
172
static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
173
}
174
src/exporting/init_connectors.c
+2
@@ -93,6 +93,8 @@ int init_connectors(struct engine *engine)
93
int error = uv_thread_create(&instance->thread, instance->worker, instance);
94
if (error) {
95
netdata_log_error("EXPORTING: cannot create thread worker. uv_thread_create(): %s", uv_strerror(error));
96
+ instance->exited = 1;
97
+ instance->thread = 0;
98
return 1;
99
}
100