tests: Fix signalling race condition in TPM tests
PATCH 1/1 tests: Fix signalling race condition in TPM tests
This patch fixes a race condition and test failure where the main process waits for the signal of a thread but the thread already sent that signal via a condition. Since these signals are non-sticky, we need to introduce a separate variable to make this signal sticky.
Signed-off-by Stefan Berger <[email protected]>
--- tests/tpm-crb-test.c | 1 + tests/tpm-emu.c | 11 ++++++++++- tests/tpm-emu.h | 1 + tests/tpm-tis-test.c | 1 + 4 files changed, 13 insertions(+), 1 deletion(-)
tests/tpm-crb-test.c +1 -0
@@ -151,6 +151,7 @@ int main(int argc, char **argv) test.addr->u.q_unix.path =g_build_filename (tmp_path,"sock" ,NULL );g_mutex_init (&test.data_mutex);g_cond_init (&test.data_cond); + test.data_cond_signal = false; thread =g_thread_new (NULL , tpm_emu_ctrl_thread, &test);tpm_emu_test_wait_cond (&test);
tests/tpm-emu.c +10 -1
@@ -23,9 +23,14 @@ void tpm_emu_test_wait_cond(TestState *s)gint64 end_time =g_get_monotonic_time () +5 *G_TIME_SPAN_SECOND ;g_mutex_lock (&s->data_mutex); -if (!g_cond_wait_until (&s->data_cond, &s->data_mutex, end_time)) { + +if (!s->data_cond_signal && + !g_cond_wait_until (&s->data_cond, &s->data_mutex, end_time)) {g_assert_not_reached (); } + + s->data_cond_signal = false; +g_mutex_unlock (&s->data_mutex); }
@@ -72,6 +77,10 @@ void *tpm_emu_ctrl_thread(void *data)QIOChannel *ioc;qio_channel_socket_listen_sync (lioc, s->addr, &error_abort); + +g_mutex_lock (&s->data_mutex); + s->data_cond_signal = true; +g_mutex_unlock (&s->data_mutex);g_cond_signal (&s->data_cond);qio_channel_wait (QIO_CHANNEL (lioc),G_IO_IN );
tests/tpm-emu.h +1 -0
@@ -26,6 +26,7 @@ struct tpm_hdr { typedef struct TestState { GMutex data_mutex; GCond data_cond; + bool data_cond_signal; SocketAddress *addr; QIOChannel *tpm_ioc; GThread *emu_tpm_thread;
tests/tpm-tis-test.c +1 -0
@@ -446,6 +446,7 @@ int main(int argc, char **argv) test.addr->u.q_unix.path =g_build_filename (tmp_path,"sock" ,NULL );g_mutex_init (&test.data_mutex);g_cond_init (&test.data_cond); + test.data_cond_signal = false; thread =g_thread_new (NULL , tpm_emu_ctrl_thread, &test);tpm_emu_test_wait_cond (&test);
Signature · 1 line
2.14.4
Quoted 7 lines · Stefan Berger
This patch fixes a race condition and test failure where the main process waits for the signal of a thread but the thread already sent that signal via a condition. Since these signals are non-sticky, we need to introduce a separate variable to make this signal sticky.
Signed-off-by Stefan Berger <[email protected]>
Reviewed-by Marc-André Lureau <[email protected]>
Quoted 78 lines
--- tests/tpm-crb-test.c | 1 + tests/tpm-emu.c | 11 ++++++++++- tests/tpm-emu.h | 1 + tests/tpm-tis-test.c | 1 + 4 files changed, 13 insertions(+), 1 deletion(-)tests/tpm-crb-test.c +1
-0@@ -151,6 +151,7 @@ int main(int argc, char **argv) test.addr->u.q_unix.path =g_build_filename (tmp_path,"sock" ,NULL );g_mutex_init (&test.data_mutex);g_cond_init (&test.data_cond); + test.data_cond_signal = false; thread =g_thread_new (NULL , tpm_emu_ctrl_thread, &test);tpm_emu_test_wait_cond (&test);tests/tpm-emu.c +10
-1@@ -23,9 +23,14 @@ void tpm_emu_test_wait_cond(TestState *s)gint64 end_time =g_get_monotonic_time () +5 *G_TIME_SPAN_SECOND ;g_mutex_lock (&s->data_mutex); -if (!g_cond_wait_until (&s->data_cond, &s->data_mutex, end_time)) { + +if (!s->data_cond_signal && + !g_cond_wait_until (&s->data_cond, &s->data_mutex, end_time)) {g_assert_not_reached (); } + + s->data_cond_signal = false; +g_mutex_unlock (&s->data_mutex); }@@ -72,6 +77,10 @@ void *tpm_emu_ctrl_thread(void *data)QIOChannel *ioc;qio_channel_socket_listen_sync (lioc, s->addr, &error_abort); + +g_mutex_lock (&s->data_mutex); + s->data_cond_signal = true; +g_mutex_unlock (&s->data_mutex);g_cond_signal (&s->data_cond);qio_channel_wait (QIO_CHANNEL (lioc),G_IO_IN );tests/tpm-emu.h +1
-0@@ -26,6 +26,7 @@ struct tpm_hdr { typedef struct TestState { GMutex data_mutex; GCond data_cond; + bool data_cond_signal; SocketAddress *addr; QIOChannel *tpm_ioc; GThread *emu_tpm_thread;tests/tpm-tis-test.c +1
-1@@ -446,6 +446,7 @@ int main(int argc, char **argv) test.addr->u.q_unix.path =g_build_filename (tmp_path,"sock" ,NULL );g_mutex_init (&test.data_mutex);g_cond_init (&test.data_cond); + test.data_cond_signal = false; thread =g_thread_new (NULL , tpm_emu_ctrl_thread, &test);tpm_emu_test_wait_cond (&test); --2.14.4
Signature · 1 line
Marc-André Lureau