@cryptotaxi247 / netdata-1 / commits / c9dfe8e10

fix SSL random failures when using multithreaded web server with OpenSSL < 1.1.0 (#11089)

thiagoftsm committed May 5, 2021 at 12:04 UTC c9dfe8e102e7593820b3daf2a3115c6ddfb78680
1 file changed +37 -27
web/server/static/static-threaded.c
+37 -27
@@ -454,49 +454,59 @@ static void socket_listen_main_static_threaded_cleanup(void *ptr) {
454
455 void *socket_listen_main_static_threaded(void *ptr) {
456 netdata_thread_cleanup_push(socket_listen_main_static_threaded_cleanup, ptr);
457 - web_server_mode = WEB_SERVER_MODE_STATIC_THREADED;
457 + web_server_mode = WEB_SERVER_MODE_STATIC_THREADED;
458
459 - if(!api_sockets.opened)
460 - fatal("LISTENER: no listen sockets available.");
459 + if(!api_sockets.opened)
460 + fatal("LISTENER: no listen sockets available.");
461
462 #ifdef ENABLE_HTTPS
463 - security_start_ssl(NETDATA_SSL_CONTEXT_SERVER);
463 + security_start_ssl(NETDATA_SSL_CONTEXT_SERVER);
464 #endif
465 - // 6 threads is the optimal value
466 - // since 6 are the parallel connections browsers will do
467 - // so, if the machine has more CPUs, avoid using resources unnecessarily
468 - int def_thread_count = (processors > 6)?6:processors;
465 + // 6 threads is the optimal value
466 + // since 6 are the parallel connections browsers will do
467 + // so, if the machine has more CPUs, avoid using resources unnecessarily
468 + int def_thread_count = (processors > 6) ? 6 : processors;
469
470 - if (!strcmp(config_get(CONFIG_SECTION_WEB, "mode", ""),"single-threaded")) {
470 + if (!strcmp(config_get(CONFIG_SECTION_WEB, "mode", ""),"single-threaded")) {
471 info("Running web server with one thread, because mode is single-threaded");
472 config_set(CONFIG_SECTION_WEB, "mode", "static-threaded");
473 def_thread_count = 1;
474 - }
475 - static_threaded_workers_count = config_get_number(CONFIG_SECTION_WEB, "web server threads", def_thread_count);
474 + }
475 + static_threaded_workers_count = config_get_number(CONFIG_SECTION_WEB, "web server threads", def_thread_count);
476
477 - if(static_threaded_workers_count < 1) static_threaded_workers_count = 1;
477 + if (static_threaded_workers_count < 1) static_threaded_workers_count = 1;
478 +#ifdef ENABLE_HTTPS
479 + // See https://github.com/netdata/netdata/issues/11081#issuecomment-831998240 for more details
480 + if (OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110) {
481 + static_threaded_workers_count = 1;
482 + info("You are running an OpenSSL older than 1.1.0, web server will not enable multithreading.");
483 + }
484 +#endif
485
479 - size_t max_sockets = (size_t)config_get_number(CONFIG_SECTION_WEB, "web server max sockets", (long long int)(rlimit_nofile.rlim_cur / 4));
486 + size_t max_sockets = (size_t)config_get_number(CONFIG_SECTION_WEB, "web server max sockets",
487 + (long long int)(rlimit_nofile.rlim_cur / 4));
488
481 - static_workers_private_data = callocz((size_t)static_threaded_workers_count, sizeof(struct web_server_static_threaded_worker));
489 + static_workers_private_data = callocz((size_t)static_threaded_workers_count,
490 + sizeof(struct web_server_static_threaded_worker));
491
483 - web_server_is_multithreaded = (static_threaded_workers_count > 1);
492 + web_server_is_multithreaded = (static_threaded_workers_count > 1);
493
485 - int i;
486 - for(i = 1; i < static_threaded_workers_count; i++) {
487 - static_workers_private_data[i].id = i;
488 - static_workers_private_data[i].max_sockets = max_sockets / static_threaded_workers_count;
494 + int i;
495 + for (i = 1; i < static_threaded_workers_count; i++) {
496 + static_workers_private_data[i].id = i;
497 + static_workers_private_data[i].max_sockets = max_sockets / static_threaded_workers_count;
498
490 - char tag[50 + 1];
491 - snprintfz(tag, 50, "WEB_SERVER[static%d]", i+1);
499 + char tag[50 + 1];
500 + snprintfz(tag, 50, "WEB_SERVER[static%d]", i+1);
501
493 - info("starting worker %d", i+1);
494 - netdata_thread_create(&static_workers_private_data[i].thread, tag, NETDATA_THREAD_OPTION_DEFAULT, socket_listen_main_static_threaded_worker, (void *)&static_workers_private_data[i]);
495 - }
502 + info("starting worker %d", i+1);
503 + netdata_thread_create(&static_workers_private_data[i].thread, tag, NETDATA_THREAD_OPTION_DEFAULT,
504 + socket_listen_main_static_threaded_worker, (void *)&static_workers_private_data[i]);
505 + }
506
497 - // and the main one
498 - static_workers_private_data[0].max_sockets = max_sockets / static_threaded_workers_count;
499 - socket_listen_main_static_threaded_worker((void *)&static_workers_private_data[0]);
507 + // and the main one
508 + static_workers_private_data[0].max_sockets = max_sockets / static_threaded_workers_count;
509 + socket_listen_main_static_threaded_worker((void *)&static_workers_private_data[0]);
510
511 netdata_thread_cleanup_pop(1);
512 return NULL;