Skip to content

Commit 207b91f

Browse files
authored
Merge pull request #2627 from xrmx/dont-thread-cancel
core/uwsgi: stop using pthread_cancel()
2 parents f7856e5 + 7610f52 commit 207b91f

File tree

4 files changed

+6
-65
lines changed

4 files changed

+6
-65
lines changed

core/loop.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ void uwsgi_setup_thread_req(long core_id, struct wsgi_request *wsgi_req) {
8181
int i;
8282
sigset_t smask;
8383

84-
85-
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &i);
86-
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &i);
8784
pthread_setspecific(uwsgi.tur_key, (void *) wsgi_req);
8885

8986
if (core_id > 0) {

core/utils.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,12 +1034,6 @@ void uwsgi_destroy_request(struct wsgi_request *wsgi_req) {
10341034

10351035
close_and_free_request(wsgi_req);
10361036

1037-
int foo;
1038-
if (uwsgi.threads > 1) {
1039-
// now the thread can die...
1040-
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &foo);
1041-
}
1042-
10431037
// reset for avoiding following requests to fail on non-uwsgi protocols
10441038
// thanks Marko Tiikkaja for catching it
10451039
wsgi_req->uh->_pktsize = 0;
@@ -1131,11 +1125,6 @@ void uwsgi_close_request(struct wsgi_request *wsgi_req) {
11311125
func(wsgi_req);
11321126
}
11331127

1134-
if (uwsgi.threads > 1) {
1135-
// now the thread can die...
1136-
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &tmp_id);
1137-
}
1138-
11391128
// leave harakiri mode
11401129
if (uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].harakiri > 0) {
11411130
set_harakiri(wsgi_req, 0);
@@ -1583,18 +1572,12 @@ int wsgi_req_accept(int queue, struct wsgi_request *wsgi_req) {
15831572
}
15841573
}
15851574

1586-
// kill the thread after the request completion
1587-
if (uwsgi.threads > 1)
1588-
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &ret);
1589-
15901575
if (uwsgi.signal_socket > -1 && (interesting_fd == uwsgi.signal_socket || interesting_fd == uwsgi.my_signal_socket)) {
15911576

15921577
thunder_unlock;
15931578

15941579
uwsgi_receive_signal(wsgi_req, interesting_fd, "worker", uwsgi.mywid);
15951580

1596-
if (uwsgi.threads > 1)
1597-
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &ret);
15981581
return -1;
15991582
}
16001583

@@ -1605,8 +1588,6 @@ int wsgi_req_accept(int queue, struct wsgi_request *wsgi_req) {
16051588
wsgi_req->fd = wsgi_req->socket->proto_accept(wsgi_req, interesting_fd);
16061589
thunder_unlock;
16071590
if (wsgi_req->fd < 0) {
1608-
if (uwsgi.threads > 1)
1609-
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &ret);
16101591
return -1;
16111592
}
16121593

@@ -1621,8 +1602,6 @@ int wsgi_req_accept(int queue, struct wsgi_request *wsgi_req) {
16211602
}
16221603

16231604
thunder_unlock;
1624-
if (uwsgi.threads > 1)
1625-
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &ret);
16261605
return -1;
16271606
}
16281607

core/uwsgi.c

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,30 +1226,17 @@ void warn_pipe() {
12261226
}
12271227
}
12281228

1229-
// in threading mode we need to use the cancel pthread subsystem
1230-
void wait_for_threads() {
1229+
// This function is called from signal handler or main thread to wait worker threads.
1230+
// `uwsgi.workers[uwsgi.mywid].manage_next_request` should be set to 0 to stop worker threads.
1231+
static void wait_for_threads() {
12311232
int i, ret;
12321233

1233-
// on some platform thread cancellation is REALLY flaky
1234+
// This option was added because we used pthread_cancel().
1235+
// thread cancellation is REALLY flaky
12341236
if (uwsgi.no_threads_wait) return;
12351237

1236-
int sudden_death = 0;
1237-
1238-
pthread_mutex_lock(&uwsgi.six_feet_under_lock);
1239-
for (i = 1; i < uwsgi.threads; i++) {
1240-
if (!pthread_equal(uwsgi.workers[uwsgi.mywid].cores[i].thread_id, pthread_self())) {
1241-
if (pthread_cancel(uwsgi.workers[uwsgi.mywid].cores[i].thread_id)) {
1242-
uwsgi_error("pthread_cancel()\n");
1243-
sudden_death = 1;
1244-
}
1245-
}
1246-
}
1247-
1248-
if (sudden_death)
1249-
goto end;
1250-
12511238
// wait for thread termination
1252-
for (i = 1; i < uwsgi.threads; i++) {
1239+
for (i = 0; i < uwsgi.threads; i++) {
12531240
if (!pthread_equal(uwsgi.workers[uwsgi.mywid].cores[i].thread_id, pthread_self())) {
12541241
ret = pthread_join(uwsgi.workers[uwsgi.mywid].cores[i].thread_id, NULL);
12551242
if (ret) {
@@ -1261,26 +1248,6 @@ void wait_for_threads() {
12611248
}
12621249
}
12631250
}
1264-
1265-
// cancel initial thread last since after pthread_cancel() and
1266-
// pthread_join() is called on it, the whole process will appear to be
1267-
// a zombie. although it won't eliminate process zombie time, but it
1268-
// should minimize it.
1269-
if (!pthread_equal(uwsgi.workers[uwsgi.mywid].cores[0].thread_id, pthread_self())) {
1270-
if (pthread_cancel(uwsgi.workers[uwsgi.mywid].cores[0].thread_id)) {
1271-
uwsgi_error("pthread_cancel() on initial thread\n");
1272-
goto end;
1273-
}
1274-
1275-
ret = pthread_join(uwsgi.workers[uwsgi.mywid].cores[0].thread_id, NULL);
1276-
if (ret) {
1277-
uwsgi_log("pthread_join() = %d on initial thread\n", ret);
1278-
}
1279-
}
1280-
1281-
end:
1282-
1283-
pthread_mutex_unlock(&uwsgi.six_feet_under_lock);
12841251
}
12851252

12861253

@@ -3641,7 +3608,6 @@ void uwsgi_worker_run() {
36413608

36423609
if (uwsgi.cores > 1) {
36433610
uwsgi.workers[uwsgi.mywid].cores[0].thread_id = pthread_self();
3644-
pthread_mutex_init(&uwsgi.six_feet_under_lock, NULL);
36453611
}
36463612

36473613
uwsgi_ignition();

uwsgi.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2526,7 +2526,6 @@ struct uwsgi_server {
25262526

25272527
// avoid thundering herd in threaded modes
25282528
pthread_mutex_t thunder_mutex;
2529-
pthread_mutex_t six_feet_under_lock;
25302529
pthread_mutex_t lock_static;
25312530

25322531
int use_thunder_lock;

0 commit comments

Comments
 (0)