Skip to content
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

[BUG] Segmentation fault with Walrus-Operator and same named variables #6094

Open
mspi92 opened this issue Mar 17, 2024 · 2 comments · May be fixed by #6120
Open

[BUG] Segmentation fault with Walrus-Operator and same named variables #6094

mspi92 opened this issue Mar 17, 2024 · 2 comments · May be fixed by #6120

Comments

@mspi92
Copy link

mspi92 commented Mar 17, 2024

Describe the bug

There is a Segmentation Fault in the attached example (simplified from actually failing production code).
Requirement for failure:

  • Usage of Walrus Operator in an if statement.
  • First If Statement does not fully evaluate
  • Second If Statement uses same variable name in assignment operator.

Segmentation fault occurs because Variable gets dereferenced in the second If-condition without having a value assigned.
The assignment doesn't happen because first If-condition doesn't get fully evaluated.

Code to reproduce the behaviour:

def problem_function():
    d = False
    a = []
    if d and (q := a):
        pass

    if q := a:
        pass

Expected behaviour

No Segmentation Fault in Cythonized code

OS

Linux

Python version

3.11.8

Cython version

latest master

Additional context

No response

@da-woods
Copy link
Contributor

Thanks - in the short term I suspect the better solution is to treat assignment expressions as "maybe assignments" rather than "definite assignments". There's quite a few variations of cases like this where it isn't clear if they're actually run and which our flow control doesn't currently cover so working it out comprehensively is likely hard.

That'll de-optimize assignment expressions slightly but I think that's OK.

da-woods added a commit to da-woods/cython that referenced this issue Apr 1, 2024
Some expressions are partly conditionally evaluated, and so
bits of them may be skipped. This has implications if they
contain an assignment expression since the variable may not
actually be assigned.

The tests for various comprehensions aren't actually needed
since they already worked, but are added for completeness.

Fixes cython#6094
@da-woods da-woods linked a pull request Apr 1, 2024 that will close this issue
@gshmu
Copy link

gshmu commented Apr 10, 2024

walrus define variable after or operate, make gunicorn crash: [ERROR] Worker (pid:9467) was sent code 139!

def func(cell: dict):
    if (flag := True) or ((text := cell.get("text")) and text.isdigit()):
        return True
    text = cell.get("text") or cell.get("value")
    return text

Expected: raise some error at lease

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

Successfully merging a pull request may close this issue.

3 participants