huajun07 / codesketcher

Visualise your code in action!
https://main.d1fr5et3wgts3j.amplifyapp.com/
MIT License
0 stars 0 forks source link

AWS Lambda's Python runtime behaves differently from local #39

Closed limanjun99 closed 1 year ago

limanjun99 commented 1 year ago

This issue was first discovered in this PR. The executor lambda was found to behave differently than in local testing, causing errors.

Consider the following testcase (which is already one of the unit tests):

{
    "code": dedent(
        """\
        a = 1
        b = int(input())
        c = int(input())
        d = a + b + c
        print(f'Value of d is {d}')"""
    ),
    "input": dedent(
        """\
        2
        3
        Extra input is ignored"""
    ),
}

It runs fine locally, but when deployed to AWS lambda and we manually run an invocation, we get this response instead:

{
    "executed": true,
    "data": [
        {
            "line_number": 1,
            "local_variable_changes": {
                "a": {
                    "type": "int",
                    "value": 1
                }
            },
            "global_variable_changes": {},
            "function_scope": []
        }
    ],
    "output": "Value of d is 6\n"
}

Looking at the logs, we see that at some point in code execution, we enter a frame with the name awslambdaric.bootstrap, which highly suggests that Lambda is doing something that causes our code to error out.

TODO

limanjun99 commented 1 year ago

Not entirely sure why we did not encounter this issue before. The fix for now is to ignore frames where the __name__ in frame globals is awslambdaric.bootstrap, by passing it to the skip parameter when initing a bdb instance. This fix should be sufficient if this is a one-time issue. If this occurs again with multiple other module names, it might be worth the effort to look into sandboxing/docker.

Another issue will be opened to look into adding integration tests in the future.