Skip to content

Check _shutdown flag before executing serial operations#10742

Open
bysiber wants to merge 1 commit intopython-poetry:mainfrom
bysiber:fix/executor-shutdown-check-serial-ops
Open

Check _shutdown flag before executing serial operations#10742
bysiber wants to merge 1 commit intopython-poetry:mainfrom
bysiber:fix/executor-shutdown-check-serial-ops

Conversation

@bysiber
Copy link
Contributor

@bysiber bysiber commented Feb 20, 2026

Problem

In Executor.execute(), when a parallel task fails and sets self._shutdown = True, the serial operations in the same priority group still execute unconditionally.

concurrent.futures.wait() does not re-raise exceptions from worker threads — it waits for all futures to complete and returns. After it returns, the loop over serial_operations runs every serial operation without checking _shutdown. The shutdown check only happens after the try/except block.

try:
    wait(tasks)                          # parallel task set _shutdown = True
    for operation in serial_operations:
        self._execute_operation(operation)  # runs anyway!
except KeyboardInterrupt:
    self._shutdown = True

if self._shutdown:  # too late — serial ops already ran
    ...

This matters when parallel installs and serial uninstalls are in the same priority group. A failed compilation in a parallel install should stop the whole process, but instead all uninstalls still proceed, potentially leaving the environment in a worse state than before.

Fix

Check self._shutdown before each serial operation:

for operation in serial_operations:
    if self._shutdown:
        break
    self._execute_operation(operation)

Summary by Sourcery

Guard serial installation operations with the executor shutdown flag and document a separate sdist archive filename handling issue and its fix proposal.

Bug Fixes:

  • Prevent serial operations from running after a parallel task failure by checking the executor shutdown flag before each serial operation.

Documentation:

  • Add a markdown document describing a bug in sdist archive directory name derivation caused by misuse of str.rstrip and outlining a suffix-matching based fix.

@sourcery-ai
Copy link

sourcery-ai bot commented Feb 20, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Ensure executor stops executing serial operations once shutdown is signaled, and add a markdown file documenting a separate sdist suffix-handling bug and its intended fix.

Sequence diagram for executor shutdown affecting serial operations

sequenceDiagram
    participant Executor
    participant ParallelTasks
    participant SerialOperation

    Executor->>ParallelTasks: submit parallel operations
    ParallelTasks-->>Executor: futures
    Executor->>ParallelTasks: wait(tasks)
    ParallelTasks-->>Executor: one task fails, sets Executor._shutdown = True

    loop serial_operations
        Executor->>Executor: check _shutdown
        alt _shutdown is True
            Executor-->>Executor: break out of loop
        else _shutdown is False
            Executor->>SerialOperation: _execute_operation(operation)
            SerialOperation-->>Executor: result
        end
    end

    alt shutdown after error or KeyboardInterrupt
        Executor-->>Executor: skip remaining work and exit
    end
Loading

Class diagram for Executor shutdown and serialization logic

classDiagram
    class Executor {
        bool _shutdown
        _serialize(operations, priority_group)
        _execute_operation(operation)
        execute(operations)
    }

    class Operation {
        int priority
        bool is_parallel
        bool is_serial
    }

    Executor "1" o-- "*" Operation : executes

    class ParallelExecution {
        wait(tasks)
    }

    Executor ..> ParallelExecution : uses for parallel operations

    note for Executor "In _serialize, Executor now checks _shutdown before each serial operation and breaks the loop when _shutdown is True"
Loading

File-Level Changes

Change Details Files
Stop executing serial installation operations after a shutdown is requested during parallel tasks.
  • After waiting on parallel tasks, iterate serial operations with a per-operation check of the executor shutdown flag
  • Break out of the serial-operations loop immediately when the shutdown flag is set instead of checking only after all serial operations complete
src/poetry/installation/executor.py
Add markdown documentation describing a bug in sdist archive suffix handling and the proposed fix.
  • Introduce a new markdown file outlining how incorrect use of str.rstrip for archive suffixes mis-derives directory names for extracted sdists
  • Describe a fix based on explicit matching of known archive extensions rather than character-set stripping
pr_body_2.md

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!

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 - I've left some high level feedback:

  • The new pr_body_2.md file looks like PR description/supporting notes rather than source content; consider removing it from the repo or relocating it to a more appropriate place (e.g. the PR description or an issue) so it doesn’t ship with the package.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `pr_body_2.md` file looks like PR description/supporting notes rather than source content; consider removing it from the repo or relocating it to a more appropriate place (e.g. the PR description or an issue) so it doesn’t ship with the package.

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.

When a parallel task fails and sets _shutdown = True, the executor
still unconditionally runs all serial operations (uninstalls and
develop-mode installs) in the same priority group.  wait() doesn't
re-raise thread exceptions, so control falls through to the serial
loop.  The _shutdown check only happens after the try-except block.

This can leave the environment in a worse state — packages get
uninstalled after the installation was supposed to have stopped,
creating a partial environment that's harder to recover from than
just aborting early.
@bysiber bysiber force-pushed the fix/executor-shutdown-check-serial-ops branch from 4b0dd0c to b722ef9 Compare February 20, 2026 06:55
Copy link
Member

@radoering radoering left a comment

Choose a reason for hiding this comment

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

Wouldn't it make sense to put this check at the beginning of _execute_operation so that waiting tasks are also aborted early?

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