Skip to content

Commit e423902

Browse files
Christopher Friedtcfriedt
authored andcommitted
tests: posix: pthread: test for pthread descriptor leaks
Add a simple test to ensure that we can create and join a single thread `CONFIG_MAX_PTHREAD_COUNT` * 2 times. If there are leaks, then `pthread_create()` should eventually return `EAGAIN`. If there are no leaks, then the test should pass. Fixes #47609 Signed-off-by: Christopher Friedt <[email protected]> (cherry picked from commit d37350b)
1 parent 018f836 commit e423902

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

tests/posix/common/src/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern void test_posix_pthread_create_negative(void);
2121
extern void test_posix_pthread_termination(void);
2222
extern void test_posix_multiple_threads_single_key(void);
2323
extern void test_posix_single_thread_multiple_keys(void);
24+
extern void test_pthread_descriptor_leak(void);
2425
extern void test_nanosleep_NULL_NULL(void);
2526
extern void test_nanosleep_NULL_notNULL(void);
2627
extern void test_nanosleep_notNULL_NULL(void);
@@ -45,6 +46,7 @@ void test_main(void)
4546
ztest_unit_test(test_posix_pthread_termination),
4647
ztest_unit_test(test_posix_multiple_threads_single_key),
4748
ztest_unit_test(test_posix_single_thread_multiple_keys),
49+
ztest_unit_test(test_pthread_descriptor_leak),
4850
ztest_unit_test(test_posix_clock),
4951
ztest_unit_test(test_posix_semaphore),
5052
ztest_unit_test(test_posix_normal_mutex),

tests/posix/common/src/pthread.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,20 @@ void test_posix_pthread_create_negative(void)
560560
ret = pthread_create(&pthread1, &attr1, create_thread1, (void *)1);
561561
zassert_equal(ret, EINVAL, "create thread with 0 size");
562562
}
563+
564+
void test_pthread_descriptor_leak(void)
565+
{
566+
void *unused;
567+
pthread_t pthread1;
568+
pthread_attr_t attr;
569+
570+
zassert_ok(pthread_attr_init(&attr), NULL);
571+
zassert_ok(pthread_attr_setstack(&attr, &stack_e[0][0], STACKS), NULL);
572+
573+
/* If we are leaking descriptors, then this loop will never complete */
574+
for (size_t i = 0; i < CONFIG_MAX_PTHREAD_COUNT * 2; ++i) {
575+
zassert_ok(pthread_create(&pthread1, &attr, create_thread1, NULL),
576+
"unable to create thread %zu", i);
577+
zassert_ok(pthread_join(pthread1, &unused), "unable to join thread %zu", i);
578+
}
579+
}

0 commit comments

Comments
 (0)