@cryptotaxi247 / netdata-1 / commits / b09a9ad3f

apps.plugin function is not thread safe (#15978)

Costa Tsaousis committed Sep 15, 2023 at 16:01 UTC b09a9ad3f94a9ad0c14dce92cf2897ae35014961
1 file changed +12 -24
collectors/apps.plugin/apps_plugin.c
+12 -24
@@ -4575,7 +4575,7 @@ static int check_capabilities() {
4575 }
4576 #endif
4577
4578 -static netdata_mutex_t stdout_mutex = NETDATA_MUTEX_INITIALIZER;
4578 +static netdata_mutex_t apps_and_stdout_mutex = NETDATA_MUTEX_INITIALIZER;
4579
4580 #define PROCESS_FILTER_CATEGORY "category:"
4581 #define PROCESS_FILTER_USER "user:"
@@ -4661,10 +4661,7 @@ static void apps_plugin_function_processes_help(const char *transaction) {
4661 "Filters can be combined. Each filter can be given only one time.\n"
4662 );
4663
4664 - netdata_mutex_lock(&stdout_mutex);
4664 pluginsd_function_result_to_stdout(transaction, HTTP_RESP_OK, "text/plain", now_realtime_sec() + 3600, wb);
4666 - netdata_mutex_unlock(&stdout_mutex);
4667 -
4665 buffer_free(wb);
4666 }
4667
@@ -4701,31 +4698,24 @@ static void function_processes(const char *transaction, char *function __maybe_u
4698 if(!category && strncmp(keyword, PROCESS_FILTER_CATEGORY, strlen(PROCESS_FILTER_CATEGORY)) == 0) {
4699 category = find_target_by_name(apps_groups_root_target, &keyword[strlen(PROCESS_FILTER_CATEGORY)]);
4700 if(!category) {
4704 - netdata_mutex_lock(&stdout_mutex);
4701 pluginsd_function_json_error_to_stdout(transaction, HTTP_RESP_BAD_REQUEST,
4702 "No category with that name found.");
4707 - netdata_mutex_unlock(&stdout_mutex);
4708 -
4703 return;
4704 }
4705 }
4706 else if(!user && strncmp(keyword, PROCESS_FILTER_USER, strlen(PROCESS_FILTER_USER)) == 0) {
4707 user = find_target_by_name(users_root_target, &keyword[strlen(PROCESS_FILTER_USER)]);
4708 if(!user) {
4715 - netdata_mutex_lock(&stdout_mutex);
4709 pluginsd_function_json_error_to_stdout(transaction, HTTP_RESP_BAD_REQUEST,
4710 "No user with that name found.");
4718 - netdata_mutex_unlock(&stdout_mutex);
4711 return;
4712 }
4713 }
4714 else if(strncmp(keyword, PROCESS_FILTER_GROUP, strlen(PROCESS_FILTER_GROUP)) == 0) {
4715 group = find_target_by_name(groups_root_target, &keyword[strlen(PROCESS_FILTER_GROUP)]);
4716 if(!group) {
4725 - netdata_mutex_lock(&stdout_mutex);
4717 pluginsd_function_json_error_to_stdout(transaction, HTTP_RESP_BAD_REQUEST,
4718 "No group with that name found.");
4728 - netdata_mutex_unlock(&stdout_mutex);
4719 return;
4720 }
4721 }
@@ -4751,9 +4741,7 @@ static void function_processes(const char *transaction, char *function __maybe_u
4741 else {
4742 char msg[PLUGINSD_LINE_MAX];
4743 snprintfz(msg, PLUGINSD_LINE_MAX, "Invalid parameter '%s'", keyword);
4754 - netdata_mutex_lock(&stdout_mutex);
4744 pluginsd_function_json_error_to_stdout(transaction, HTTP_RESP_BAD_REQUEST, msg);
4756 - netdata_mutex_unlock(&stdout_mutex);
4745 return;
4746 }
4747 }
@@ -5536,9 +5524,7 @@ static void function_processes(const char *transaction, char *function __maybe_u
5524 buffer_json_member_add_time_t(wb, "expires", expires);
5525 buffer_json_finalize(wb);
5526
5539 - netdata_mutex_lock(&stdout_mutex);
5527 pluginsd_function_result_to_stdout(transaction, HTTP_RESP_OK, "application/json", expires, wb);
5541 - netdata_mutex_unlock(&stdout_mutex);
5528
5529 buffer_free(wb);
5530 }
@@ -5574,15 +5560,17 @@ static void *reader_main(void *arg __maybe_unused) {
5560
5561 // internal_error(true, "Received function '%s', transaction '%s', timeout %d", function, transaction, timeout);
5562
5563 + netdata_mutex_lock(&apps_and_stdout_mutex);
5564 +
5565 if(strncmp(function, "processes", strlen("processes")) == 0)
5566 function_processes(transaction, function, buffer, PLUGINSD_LINE_MAX + 1, timeout);
5567 else {
5580 - netdata_mutex_lock(&stdout_mutex);
5568 pluginsd_function_json_error_to_stdout(transaction, HTTP_RESP_NOT_FOUND,
5569 "No function with this name found in apps.plugin.");
5583 - netdata_mutex_unlock(&stdout_mutex);
5570 }
5571
5572 + netdata_mutex_unlock(&apps_and_stdout_mutex);
5573 +
5574 // internal_error(true, "Done with function '%s', transaction '%s', timeout %d", function, transaction, timeout);
5575 }
5576 }
@@ -5704,7 +5692,7 @@ int main(int argc, char **argv) {
5692
5693 netdata_thread_t reader_thread;
5694 netdata_thread_create(&reader_thread, "APPS_READER", NETDATA_THREAD_OPTION_DONT_LOG, reader_main, NULL);
5707 - netdata_mutex_lock(&stdout_mutex);
5695 + netdata_mutex_lock(&apps_and_stdout_mutex);
5696
5697 APPS_PLUGIN_GLOBAL_FUNCTIONS();
5698
@@ -5713,7 +5701,7 @@ int main(int argc, char **argv) {
5701 heartbeat_t hb;
5702 heartbeat_init(&hb);
5703 for(; !apps_plugin_exit ; global_iterations_counter++) {
5716 - netdata_mutex_unlock(&stdout_mutex);
5704 + netdata_mutex_unlock(&apps_and_stdout_mutex);
5705
5706 #ifdef NETDATA_PROFILING
5707 #warning "compiling for profiling"
@@ -5724,16 +5712,16 @@ int main(int argc, char **argv) {
5712 #else
5713 usec_t dt = heartbeat_next(&hb, step);
5714 #endif
5727 - netdata_mutex_lock(&stdout_mutex);
5715 + netdata_mutex_lock(&apps_and_stdout_mutex);
5716
5717 struct pollfd pollfd = { .fd = fileno(stdout), .events = POLLERR };
5718 if (unlikely(poll(&pollfd, 1, 0) < 0)) {
5731 - netdata_mutex_unlock(&stdout_mutex);
5719 + netdata_mutex_unlock(&apps_and_stdout_mutex);
5720 netdata_thread_cancel(reader_thread);
5721 fatal("Cannot check if a pipe is available");
5722 }
5723 if (unlikely(pollfd.revents & POLLERR)) {
5736 - netdata_mutex_unlock(&stdout_mutex);
5724 + netdata_mutex_unlock(&apps_and_stdout_mutex);
5725 netdata_thread_cancel(reader_thread);
5726 fatal("Received error on read pipe.");
5727 }
@@ -5744,7 +5732,7 @@ int main(int argc, char **argv) {
5732 if(!collect_data_for_all_processes()) {
5733 netdata_log_error("Cannot collect /proc data for running processes. Disabling apps.plugin...");
5734 printf("DISABLE\n");
5747 - netdata_mutex_unlock(&stdout_mutex);
5735 + netdata_mutex_unlock(&apps_and_stdout_mutex);
5736 netdata_thread_cancel(reader_thread);
5737 exit(1);
5738 }
@@ -5781,5 +5769,5 @@ int main(int argc, char **argv) {
5769
5770 debug_log("done Loop No %zu", global_iterations_counter);
5771 }
5784 - netdata_mutex_unlock(&stdout_mutex);
5772 + netdata_mutex_unlock(&apps_and_stdout_mutex);
5773 }