Skip to content

Commit 35dee0f

Browse files
committed
Merge pull request etr#84 from 4picht/patch-stop-leaking-threads
webserver: stop leaking pthreads on startup failure
2 parents 898e83f + 84ec6cc commit 35dee0f

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

src/webserver.cpp

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -551,36 +551,45 @@ bool webserver::start(bool blocking)
551551
this->running = true;
552552
if(start_method == http_utils::INTERNAL_SELECT)
553553
{
554-
for(int i = 0; i < num_threads; i++)
555-
{
556-
struct MHD_Daemon* daemon = MHD_start_daemon
557-
(
558-
start_conf, this->port, &policy_callback, this,
559-
&answer_to_connection, this, MHD_OPTION_ARRAY,
560-
&iov[0], MHD_OPTION_END
561-
);
562-
if(NULL == daemon)
554+
int i;
555+
try {
556+
for(i = 0; i < num_threads; i++)
563557
{
564-
cout << gettext("Unable to connect daemon to port: ") <<
565-
this->port << endl;
566-
throw ::httpserver::webserver_exception();
567-
}
568-
details::daemon_item* di = new details::daemon_item(this, daemon);
569-
daemons.push_back(di);
558+
struct MHD_Daemon* daemon = MHD_start_daemon
559+
(
560+
start_conf, this->port, &policy_callback, this,
561+
&answer_to_connection, this, MHD_OPTION_ARRAY,
562+
&iov[0], MHD_OPTION_END
563+
);
564+
if(NULL == daemon)
565+
{
566+
cout << gettext("Unable to connect daemon to port: ") << this->port << endl;
567+
throw ::httpserver::webserver_exception();
568+
}
569+
details::daemon_item* di = new details::daemon_item(this, daemon);
570+
daemons.push_back(di);
570571

571-
//RUN SELECT THREADS
572-
pthread_t t;
573-
threads.push_back(t);
572+
//RUN SELECT THREADS
573+
pthread_t t;
574+
threads.push_back(t);
574575

575-
if(pthread_create(
576+
if(pthread_create(
576577
&threads[i],
577578
NULL,
578579
&webserver::select,
579580
static_cast<void*>(di)
580-
))
581-
{
582-
throw ::httpserver::webserver_exception();
581+
))
582+
{
583+
throw ::httpserver::webserver_exception();
584+
}
585+
}
586+
} catch (::httpserver::webserver_exception &e) {
587+
this->running = false;
588+
for (;i >= 0; --i) {
589+
pthread_kill(threads[i], 9);
583590
}
591+
threads.clear();
592+
throw e;
584593
}
585594
}
586595
else

0 commit comments

Comments
 (0)