Skip to content

patch_all() can patch modules multiple times, causing RecursionErrors #99

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
nathanreynolds-kcom opened this issue Sep 19, 2018 · 3 comments
Labels

Comments

@nathanreynolds-kcom
Copy link

nathanreynolds-kcom commented Sep 19, 2018

We hit an issue where patch_all() was used in a Lambda function - after the 1000th call we saw a RecursionError when calling requests.get().

Example:

from aws_xray_sdk.core import patch_all
import requests
import sys

def lambda_handler(event, _context):
    for i in range(sys.getrecursionlimit()):
        patch_all()
    requests.get('https://google.com'# Triggers RecursionError

We should have called patch_all() outside of the function, but this still feels like a gotcha.

core.patcher._patch has this code:

def _patch(module_to_patch):
    # [...]
    if module_to_patch in _PATCHED_MODULES:
        log.debug('%s already patched', module_to_patch)

So it recognises the multiple patch, but repatches anyway.

Should _patch return early if it recognises the patch is already in place?

@nathanreynolds-kcom nathanreynolds-kcom changed the title patch_all() can patch modules multiple times patch_all() can patch modules multiple times, causing RecursionErrors Sep 19, 2018
@haotianw465
Copy link
Contributor

Thank you for reporting the issue. This is a bug and we will work on a fix soon. Just want to clarify, does calling patch_all() outside the handler workaround the problem for you? When you say the 1000th call does it mean you got this error in your 1000th lambda function execution?

@nathanreynolds-kcom
Copy link
Author

nathanreynolds-kcom commented Sep 20, 2018

Haven't tested extensively yet, hopeful having a single patch_all at initialisation should solve it - will let you know if that's not the case.

And yep, we saw it after a single instance of the Lamba function had been called 1000 times. This was under load so we were at max concurrency and all instances were active.

@haotianw465
Copy link
Contributor

The issue is fixed in 2.2.0 https://pypi.org/project/aws-xray-sdk/. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants