@cryptotaxi247 / netdata-1 / commits / c48d52eeb

Fix coverity erro (CID 349552) double lock (#6970)

* dim_template_fix: Fix lock We had a double lock before, this commit fix this * dim_template_fix: Fix order This commit fix the order process * dim_template_fix: Return I am returning for the first solution, because the others are generating: * dim_template_fix: Try to lock This solution try to lock the host before to move in front * dim_template_fix: Move chart lock To avoid the chart to be deleted while we are linking the alarm I am moving bak the chart lock * dim_template_fix: Fix grammar This commit fixes the grammar of an error message * dim_template_fix: bring pattern Bring the defined pattern to the code and use netdata_rwlock_trywrlock * dim_template_fix Fix format This commit fixes a format missing

thiagoftsm committed Oct 1, 2019 at 15:40 UTC c48d52eeb5b2387ead1e1fa50311750f4ccd328a
1 file changed +20 -5
database/rrddim.c
+20 -5
@@ -186,6 +186,7 @@ void rrdcalc_link_to_rrddim(RRDDIM *rd, RRDSET *st, RRDHOST *host) {
186 }
187
188 RRDDIM *rrddim_add_custom(RRDSET *st, const char *id, const char *name, collected_number multiplier, collected_number divisor, RRD_ALGORITHM algorithm, RRD_MEMORY_MODE memory_mode) {
189 + RRDHOST *host = st->rrdhost;
190 rrdset_wrlock(st);
191
192 rrdset_flag_set(st, RRDSET_FLAG_SYNC_CLOCK);
@@ -204,7 +205,6 @@ RRDDIM *rrddim_add_custom(RRDSET *st, const char *id, const char *name, collecte
205 return rd;
206 }
207
207 - RRDHOST *host = st->rrdhost;
208 char filename[FILENAME_MAX + 1];
209 char fullfilename[FILENAME_MAX + 1];
210
@@ -400,13 +400,28 @@ RRDDIM *rrddim_add_custom(RRDSET *st, const char *id, const char *name, collecte
400 if(unlikely(rrddim_index_add(st, rd) != rd))
401 error("RRDDIM: INTERNAL ERROR: attempt to index duplicate dimension '%s' on chart '%s'", rd->id, st->id);
402
403 - if(host->alarms_with_foreach || host->alarms_template_with_foreach) {
404 - rrdhost_wrlock(host);
405 - rrdcalc_link_to_rrddim(rd, st, host);
403 + if (host->alarms_with_foreach || host->alarms_template_with_foreach) {
404 + int count = 0;
405 + int hostlocked;
406 + for (count = 0 ; count < 5 ; count++) {
407 + hostlocked = netdata_rwlock_trywrlock(&host->rrdhost_rwlock);
408 + if (!hostlocked) {
409 + rrdcalc_link_to_rrddim(rd, st, host);
410 + rrdhost_unlock(host);
411 + break;
412 + } else if (hostlocked != EBUSY) {
413 + error("Cannot lock host to create an alarm for the dimension.");
414 + }
415 + usleep(200000);
416 + }
417
407 - rrdhost_unlock(host);
418 + if (count == 5) {
419 + error("Failed to create an alarm for dimension %s of chart %s 5 times. Skipping alarm."
420 + , rd->name, st->name);
421 + }
422 }
423 rrdset_unlock(st);
424 +
425 return(rd);
426 }
427