-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sys/posix/pthread/pthread.c: fix thread count limiting in pthread_create() #12056
sys/posix/pthread/pthread.c: fix thread count limiting in pthread_create() #12056
Conversation
@PeterKietzmann Could you review this pr please? It looks like you are a maintainer for the POSIX Layer. |
@smlng Could you look at this? I was told you could be the right person. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
regarding testing: maybe copy from tests/thread_flood
i.e. provide a pthread variant of that in the same style?
sys/posix/pthread/pthread.c
Outdated
@@ -160,7 +161,7 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr, void *(*sta | |||
pthread_start_routine, | |||
pt, | |||
"pthread"); | |||
if (pt->thread_pid == KERNEL_PID_UNDEF) { | |||
if (pt->thread_pid == -EOVERFLOW) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in error case thread_create
can return -EAGAIN
, -EOVERFLOW
or KERNEL_PID_UNDEF
, hence I would suggest to check for <= KERNEL_PID_UNDEF
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think it can return KERNEL_PID_UNDEF
because it has 3 returns
Code from core/thread.c
if (pid == KERNEL_PID_UNDEF) {
DEBUG("thread_create(): too many threads!\n");
irq_restore(state);
return -EOVERFLOW;
}
return pid;
cannot return KERNEL_PID_UNDEF
because of previous code if (pid == KERNEL_PID_UNDEF) { ... return -EOVERFLOW
and this return that only returns when the priority is wrong. But the priority is always THREAD_PRIORITY_MAIN
so this should never return -EINVAL
if (priority >= SCHED_PRIO_LEVELS) {
return -EINVAL;
}
That is why I only check for -EOVERFLOW
But the check <= KERNEL_PID_UNDEF
could be easier to read.
Would you still suggest the change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mhm, maybe check for < KERNEL_PID_UNDEF
then, bc I wouldn't rely on the priority to work either - e.g. someone might redefine THREAD_PRIORITY_MAIN
making it invalid.
However, you're right it can never return KERNEL_PID_UNDEF
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will use the function pid_is_valid
that should be the right way to do it.
@@ -87,7 +88,7 @@ static void *pthread_start_routine(void *pt_) | |||
|
|||
static int insert(pthread_thread_t *pt) | |||
{ | |||
int result = -1; | |||
int result = KERNEL_PID_UNDEF; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice catch!
Implementing a test is a good idea. Will do. |
sure, but extra commit |
I added a small test. I can add more comments if needed. |
removed unnecessary includes |
please fix CI issue and squash directly |
The function insert returns KERNEL_PID_UNDEF now because pthread_create checks for it. In pthread_create it checks now if thread_create returns a valid pid
28b604b
to
2074f12
Compare
done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tested ACK
looks like you need to put the |
Can I rebase? ( Fixup that added |
rebase and (most importantly) squash, yes please |
b0215de
to
984eb7c
Compare
done |
Contribution description
While looking at posix threads I found a bug. When creating more threads than possible
pthread_create
does not check correctly for errors.First change in
insert
because of :second change because
thread_create
returns-EOVERFLOW
notKERNEL_PID_UNDEF
when returning an error (we dont need to check the other error because the priority is main thread prority)Testing procedure
Try to create more than
MAXTHREADS
pthreads and join on every successfully created thread.Before the fix it
Should I implement a test for it?
Issues/PRs references