Thanks to visit codestin.com
Credit goes to github.com

Skip to content

tests/thread/thread_stacksize1: Fix crash on certain platforms #5784

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

Closed
wants to merge 1 commit into from

Conversation

dlech
Copy link
Contributor

@dlech dlech commented Mar 22, 2020

The test tests/thread/thread_stacksize1.py sometimes crashes with a segmentation fault because of an uncaught NLR jump.

$ ./ports/unix/micropython-coverage tests/thread/thread_stacksize1.py
0
0
True
0
Unhandled exception in thread started by FATAL: uncaught NLR 0x7fc316d06500

Using a debugger, this exception message was found to be:

"maximum recursion depth exceeded"

To fix this, we add some additional checks in the test to give a bigger stack to "desktop" platforms that need it to avoid this crash.

@dlech
Copy link
Contributor Author

dlech commented Mar 23, 2020

Also wondering why tests aren't formatted with black like everything else is now?

@dpgeorge
Copy link
Member

I'm not able to reproduce this problem (but I do see that it fails sometimes on Travis CI). What arch/OS can you reproduce it on?

It'd be good to try and fix this properly (if possible) rather than adjust the test.

@dlech
Copy link
Contributor Author

dlech commented Mar 23, 2020

I can reproduce the issue using the unix coverage variant on Mac. (This can also be seen in #5773 where that build is added to CI).

Based on the discussion in #2927, it sounds like having too small of a stack can lead to this error and debug builds (and therefore coverage builds) require a much bigger stack. It is the test itself that is setting a 2K stack, so I don't think the problem is elsewhere.

If there was a way to detect coverage builds at runtime, then I suppose we could make a better test condition, but I think what I currently have here seems reasonable enough.

@dpgeorge
Copy link
Member

I can reproduce the issue using the unix coverage variant on Mac.

Ok, thanks for the hint, I can now also reproduce it on Mac. And I have a different fix: the problem seems to be that on Mac the minimum stack size allowed by pthreads (the variable ``) is much less than on Linux, and so the subtraction of 8192 bytes (to give room to detect a stack overflow) takes the stack size down to 0. To fix this I did:

--- a/ports/unix/mpthreadport.c
+++ b/ports/unix/mpthreadport.c
@@ -221,7 +221,11 @@ void mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) {
 
     // adjust stack_size to provide room to recover from hitting the limit
     // this value seems to be about right for both 32-bit and 64-bit builds
-    *stack_size -= 8192;
+    if (*stack_size >= 2 * 8192) {
+        *stack_size -= 8192;
+    } else {
+        *stack_size /= 2;
+    }
 
     // add thread to linked list of all threads
     thread_t *th = malloc(sizeof(thread_t));

And this seems to get all thread tests passing on Mac.

@dlech
Copy link
Contributor Author

dlech commented Mar 24, 2020

Thanks for the insight. I will update the PR.

The test tests/thread/thread_stacksize1.py sometimes crashes with a
segmentation fault because of an uncaught NLR jump.

    $ ./ports/unix/micropython-coverage tests/thread/thread_stacksize1.py
    0
    0
    True
    0
    Unhandled exception in thread started by FATAL: uncaught NLR 0x7fc316d06500

Using a debugger, this exception message was found to be:

    "maximum recursion depth exceeded"

The stack size adjustment for detecting stack overflow in threads was
not taking into account that the requested stack size could be < 8k
in which case, the subtraction would overflow. This is fixed by ensuring
that the adjustment can't be more than the available size.

Suggested-by: @dpgeorge
@dlech dlech force-pushed the thread_stacksize1 branch from 9323e12 to d25f5d4 Compare March 27, 2020 01:22
@dlech
Copy link
Contributor Author

dlech commented Mar 27, 2020

updated

@dpgeorge
Copy link
Member

Thanks a lot! Merged in 5e6cee0

@dpgeorge dpgeorge closed this Mar 27, 2020
@dlech dlech deleted the thread_stacksize1 branch March 27, 2020 03:47
tannewt pushed a commit to tannewt/circuitpython that referenced this pull request Jan 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants