question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fatal Python error: Cannot recover from stack overflow

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

def loop():
    s = "ss"
    while s == "ss":
        error = False
        try:
            driver.find_element_by_xpath("/html/body/div[4]/div[1]/div[3]/div/div[2]/div/button").click()
        except:
            print(f'''{Fore.RED}[-] The captcha is unsolved!{Fore.RESET}''')
            sleep(8)
            driver.refresh()
            error = True
        if not error:
            s = "notss"
    try:
        sleep(2)
        driver.find_element_by_xpath('//*[@id="sid2"]/div/form/div/input').send_keys(vidUrl)
        
        sleep(1)
        driver.find_element_by_xpath('//*[@id="sid2"]/div/form/div/div/button').click()
        
        sleep(5)
        driver.find_element_by_xpath('//*[@id="c2VuZE9nb2xsb3dlcnNfdGlrdG9r"]/div[1]/div/form/button').click()
        
        sleep(6)
        hearts = driver.find_element_by_xpath('//*[@id="c2VuZE9nb2xsb3dlcnNfdGlrdG9r"]/span').text.split()
        
        int(hearts[0])
        print(f'''{Fore.GREEN}[+] Hearts sent!{Fore.RESET}''')
        
        sleep(5)
        driver.refresh()
        sleep(10)  
        loop()
    except:
        print(f'''{Fore.RED}[-] An error occured, Retrying..{Fore.RESET}''')
        driver.refresh()
        sleep(5)
        loop()

Description

I believe its because i have used loop a but i am unsure how to stop it from causing this error

Operating System

Windows

Operating System Details

21H2

FastAPI Version

0.71.0

Python Version

3.9.0

Additional Context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ypsahcommented, Jan 9, 2022

For the record, when raising an issue, people tend to appreciate a minimal reproducible example.

I understand you may be new to programming, so let me give you an example of how I would have gone around to troubleshooting this issue (which is by no mean the one true way, merely an example):

Step 1: put the relevant code in a separate .py file and run it

This would have highlighted that your example is missing some context about:

  • driver;
  • vidUrl;
  • Fore;
  • sleep.

From the way you use it, I understand that driver could be an instance of selenium’s WebDriver, Fore comes from colorama, sleep is time.sleep from python’s standard library, and vidUrl is probably a string containing a URL.

from time import sleep

from colorama import Fore
fom selenium.webdriver.remote.webdriver import WebDriver

driver = WebDriver()
vidUrl = "example.com"

def loop():
    s = "ss"
    while s == "ss":
        error = False
        try:
            driver.find_element_by_xpath("/html/body/div[4]/div[1]/div[3]/div/div[2]/div/button").click()
        except:
            print(f'''{Fore.RED}[-] The captcha is unsolved!{Fore.RESET}''')
            sleep(8)
            driver.refresh()
            error = True
        if not error:
            s = "notss"
    try:
        sleep(2)
        driver.find_element_by_xpath('//*[@id="sid2"]/div/form/div/input').send_keys(vidUrl)

        sleep(1)
        driver.find_element_by_xpath('//*[@id="sid2"]/div/form/div/div/button').click()

        sleep(5)
        driver.find_element_by_xpath('//*[@id="c2VuZE9nb2xsb3dlcnNfdGlrdG9r"]/div[1]/div/form/button').click()

        sleep(6)
        hearts = driver.find_element_by_xpath('//*[@id="c2VuZE9nb2xsb3dlcnNfdGlrdG9r"]/span').text.split()

        int(hearts[0])
        print(f'''{Fore.GREEN}[+] Hearts sent!{Fore.RESET}''')

        sleep(5)
        driver.refresh()
        sleep(10)
        loop()
    except:
        print(f'''{Fore.RED}[-] An error occured, Retrying..{Fore.RESET}''')
        driver.refresh()
        sleep(5)
        loop()

if __name__ == "__main__":
    loop()

Make sure that the snippet runs by itself and that it exhibits the issue you are investigating. On linux, it would mean running:

$ python reproducer.py

Where reproducer.py is the name of the file I put the code in.

If you are using an IDE, refer to its documentation on how to run standalone python scripts.

Note: the snippet I posted above does not work as-is. I suspect this is because one needs to setup a server for selenium. If that were important for the issue at hand, you would need to explain how to set that up so others can run your code and reproduce the issue. Fortunately, it’s not relevant, so let’s forget about it.

Note#2: By now, it should already be clear that the issue is not related to fastapi as fastapi does not appear anywhere in our snippet and the issue still occurs. That makes stackoverflow.com a better place to ask a question.

Step 2: remove as much code as you can

It is best to go step by step about it:

  • remove a few lines;
  • run the code;
  • make sure it still fails in the same way.

In order, I think I would have removed:

  • the calls to sleep: to make the code faster to run;
  • lines that contain driver: to eliminate the dependency on selenium;
  • lines that contain Fore: to eliminate the dependency on colorama.

That would have gotten me something like:

def loop():
    s = "ss"
    while s == "ss":
        error = False
        try:
            pass
        except:
            error = True
        if not error:
            s = "notss"
    try:
        loop()
    except:
        loop()

if __name__ == "__main__":
    loop()

Then, if it hadn’t been clear by now what the issue is, I would have removed either the while look of the try/except block.

Only the version with the try/except block would have kept showing the issue, so that is the one I would have kept. We are now down to:

$ cat reproducer.py
def loop():
    try:
        loop()
    except:
        loop()

if __name__ == "__main__":
    loop()

Which, thinking about it, is equivalent to:

def loop():
    loop()

if __name__ == "__main__":
    loop()

Somewhere along the way, you might have noticed that the error message changed and is now:

$ python reproducer.py
Traceback (most recent call last):
  File "/tmp/tmp.KhplXQz0Am/reproducer.py", line 5, in <module>
    loop()
  File "/tmp/tmp.KhplXQz0Am/reproducer.py", line 2, in loop
    loop()
  File "/tmp/tmp.KhplXQz0Am/reproducer.py", line 2, in loop
    loop()
  File "/tmp/tmp.KhplXQz0Am/reproducer.py", line 2, in loop
    loop()
  [Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded

It could mean that I removed too many lines and am now getting into a totally unrelated issue, but actually, if you read about it, you will find that the two errors are related and this new error message gives you more information about the issue’s root cause than the old one.

Step 3: Search online for the error message

Use your favorite search engine to research: “RecursionError: maximum recursion depth exceeded”.

If you don’t find any explanation about it that fits your use case or you don’t understand the answers, only then consider asking a question on stackoverflow.com (or the relevant github project, although in this case stackoverflow.com is the way I would go myself).

Read about how to ask a good question if you can, it’ll help you get the best answer.

0reactions
tiangolocommented, Nov 7, 2022

It seems from your code that you are not using FastAPI, in that case this wouldn’t be the place to ask.

Funnily enough, the right place would be on StackOverflow (the same name as your error 😅)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fatal Python error: Cannot recover from stack overflow
I have a function that for each line of data (data have around 2'000'000 rows) do something and then recall the same function...
Read more >
Fatal Python error: Cannot recover from stack overflow
The "Fatal Python error: Cannot recover from stack overflow" occurs when Python already raised a RecursionError (RuntimeError on Python < 3.5) ...
Read more >
Fatal Python error: Cannot recover from stack overflow.
Sometimes it happens immediately after trying to attach, sometimes the operation is successful, and it happens after a few seconds of doing some...
Read more >
Fatal Python error: cannot recover from stack overflow - Odoo
I have created a custom module, made some changes, added a transient model and added a qweb report and when upgrading I cannot...
Read more >
Cannot recover from stack overflow error while parsing using ...
...implies that the recursion limit in your program logic exceeds the maximum depth of the Python interpreter stack. Deep Dive. You are getting...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found