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

fix endless recursion introduced in #821 #832

Merged

Conversation

radoering
Copy link
Member

@radoering radoering commented Feb 2, 2025

Related to python-poetry/poetry#9956 (comment)

  • Added tests for changed code.
  • Updated documentation for changed code.

Previously, we have always tried to avoid recursions, which is difficult if you do not want to miss any simplifications. Although, #821 provides significant performance improvements for certain cases, it also introduces the risk of endless recursions due to the symmetry of intersection and union. It might still be possible to avoid this recursion with a more sophisticated fix. However, I decided to detect a recursion early instead of avoiding it.

Summary by Sourcery

Bug Fixes:

Copy link

sourcery-ai bot commented Feb 2, 2025

Reviewer's Guide by Sourcery

This pull request addresses an endless recursion issue introduced in #821. The fix involves adding a decorator to detect recursion in the intersection and union functions, and handling the RecursionError by returning a less complex marker.

Sequence diagram for marker intersection with recursion detection

sequenceDiagram
    participant Client
    participant intersection
    participant detect_recursion
    participant cnf

    Client->>intersection: intersection(*markers)
    activate intersection
    intersection->>detect_recursion: Check recursion
    activate detect_recursion
    alt No recursion detected
        detect_recursion-->>intersection: Continue
        intersection->>cnf: Convert to CNF
        alt CNF successful
            cnf-->>intersection: Return conjunction
            intersection->>intersection: Select min complexity
        else RecursionError
            cnf-->>intersection: Throw RecursionError
            intersection->>intersection: Use limited candidates
        end
    else Recursion detected
        detect_recursion-->>intersection: Throw RecursionError
    end
    intersection-->>Client: Return marker
    deactivate intersection
    deactivate detect_recursion
Loading

Class diagram for marker system with recursion detection

classDiagram
    class BaseMarker {
        +complexity
    }
    class MarkerUnion {
        +markers
    }
    class MultiMarker {
        +markers
    }
    class detect_recursion {
        +call_args: dict
        +decorated(*markers): BaseMarker
    }

    BaseMarker <|-- MarkerUnion
    BaseMarker <|-- MultiMarker

    note for detect_recursion "Decorator to detect recursions
in intersection and union"

    detect_recursion ..> BaseMarker: decorates
Loading

Flow diagram for marker processing with recursion handling

flowchart TD
    A[Start] --> B{Check recursion}
    B -->|No recursion| C[Process markers]
    B -->|Recursion detected| D[Throw RecursionError]
    C --> E{Try normalization}
    E -->|Success| F[Select min complexity]
    E -->|RecursionError| G[Use limited candidates]
    F --> H[Return result]
    G --> H
    D --> I[Handle in caller]
    I --> J[Use unnormalized form]
    J --> H
Loading

File-Level Changes

Change Details Files
Added a decorator to detect recursion in intersection and union functions.
  • Introduced a detect_recursion decorator that uses a thread-local list to track function call arguments.
  • If the same arguments are encountered again, a RecursionError is raised.
  • The intersection and union functions are decorated with detect_recursion.
src/poetry/core/version/markers.py
Modified intersection and union functions to handle RecursionError.
  • The intersection and union functions now catch RecursionError.
  • If a RecursionError is caught, a less complex marker is returned as a fallback.
src/poetry/core/version/markers.py
Added a test case to verify that the fix prevents endless recursion.
  • Added a test case test_intersection_no_endless_recursion that uses a complex marker to trigger the recursion.
  • The test asserts that the intersection of the markers does not result in an endless recursion.
tests/version/test_markers.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @radoering - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Please update the documentation to mention the recursion detection behavior in intersection/union operations, as indicated in the PR checklist.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟡 Security: 1 issue found
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

src/poetry/core/version/markers.py Show resolved Hide resolved
if markers in call_args:
raise RecursionError
call_args.append(markers)
result = func(*markers)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Call stack cleanup should be guaranteed even if an exception occurs

Wrap the function call in a try-finally block to ensure call_args.pop() is always executed, even when exceptions occur.

tests/version/test_markers.py Show resolved Hide resolved
@radoering radoering force-pushed the perf-marker-intersection-recursion branch from b44d832 to 588afc4 Compare February 2, 2025 12:04
@abn abn merged commit 7ace184 into python-poetry:main Feb 2, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants