aboutsummaryrefslogtreecommitdiff
path: root/sbin/hastd/secondary.c
diff options
context:
space:
mode:
authorPawel Jakub Dawidek <pjd@FreeBSD.org>2010-09-22 19:03:11 +0000
committerPawel Jakub Dawidek <pjd@FreeBSD.org>2010-09-22 19:03:11 +0000
commit8b70e6ae9c216673b1db0f9605419ac90d6882a0 (patch)
treeb5341732e7574446c8cde8df47eae5f3f667961b /sbin/hastd/secondary.c
parent0c24d8e2a1054acfe7739df344c1802787baf62b (diff)
downloadsrc-8b70e6ae9c216673b1db0f9605419ac90d6882a0.tar.gz
src-8b70e6ae9c216673b1db0f9605419ac90d6882a0.zip
Fix possible deadlock where worker process sends an event to the main process
while the main process sends control message to the worker process, but worker process hasn't started control thread yet, because it waits for reply from the main process. The fix is to start the control thread before sending any events. Reported and fix suggested by: Mikolaj Golub <to.my.trociny@gmail.com> MFC after: 3 days
Notes
Notes: svn path=/head/; revision=213007
Diffstat (limited to 'sbin/hastd/secondary.c')
-rw-r--r--sbin/hastd/secondary.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/sbin/hastd/secondary.c b/sbin/hastd/secondary.c
index 6f56239be1ce..9e1e53760852 100644
--- a/sbin/hastd/secondary.c
+++ b/sbin/hastd/secondary.c
@@ -393,17 +393,27 @@ hastd_secondary(struct hast_resource *res, struct nv *nvin)
pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
init_local(res);
- init_remote(res, nvin);
init_environment();
+
+ /*
+ * Create the control thread before sending any event to the parent,
+ * as we can deadlock when parent sends control request to worker,
+ * but worker has no control thread started yet, so parent waits.
+ * In the meantime worker sends an event to the parent, but parent
+ * is unable to handle the event, because it waits for control
+ * request response.
+ */
+ error = pthread_create(&td, NULL, ctrl_thread, res);
+ assert(error == 0);
+
+ init_remote(res, nvin);
event_send(res, EVENT_CONNECT);
error = pthread_create(&td, NULL, recv_thread, res);
assert(error == 0);
error = pthread_create(&td, NULL, disk_thread, res);
assert(error == 0);
- error = pthread_create(&td, NULL, send_thread, res);
- assert(error == 0);
- (void)ctrl_thread(res);
+ (void)send_thread(res);
}
static void