Skip to content

Conversation

@pvillard31
Copy link
Contributor

Summary

NIFI-15449 - NAR deletion blocks indefinitely when Python processor is initializing

The issue is caused by:

  • Deadlock between locks: a deadlock occurs between StandardPythonBridge and StandardExtensionDiscoveringManager:
    • Thread A (NAR deletion): Acquires StandardExtensionDiscoveringManager lock -> waits for StandardPythonBridge lock
    • Thread B (Processor initialization): Acquires StandardPythonBridge lock -> calls getNarDirectories() which waits for StandardExtensionDiscoveringManager lock
  • Non-interruptible initialization: The Python process initialization (virtual environment creation, debugpy installation) runs in a tight loop with no mechanism to cancel or interrupt it when a shutdown is requested.

Changes:

  • Break the deadlock: Pre-compute the NAR directories before acquiring the StandardPythonBridge synchronized block, ensuring locks are always acquired in a consistent order.
  • Make initialization interruptible:
    • Add periodic isShutdown() checks during venv creation and dependency installation loops in PythonProcess
    • Add a CANCELLED state to AsyncLoadedProcessor.LoadState and a cancelLoading() method
    • Implement cancellation support in StandardPythonProcessorBridge that sets a flag checked during initialization
    • Track Python processes from the moment they're created (before start() is called) so they can be properly shut down during NAR deletion

Tracking

Please complete the following tracking steps prior to pull request creation.

Issue Tracking

Pull Request Tracking

  • Pull Request title starts with Apache NiFi Jira issue number, such as NIFI-00000
  • Pull Request commit message starts with Apache NiFi Jira issue number, as such NIFI-00000
  • Pull request contains commits signed with a registered key indicating Verified status

Pull Request Formatting

  • Pull Request based on current revision of the main branch
  • Pull Request refers to a feature branch with one commit containing changes

Verification

Please indicate the verification steps performed prior to pull request creation.

Build

  • Build completed using ./mvnw clean install -P contrib-check
    • JDK 21
    • JDK 25

Licensing

  • New dependencies are compatible with the Apache License 2.0 according to the License Policy
  • New dependencies are documented in applicable LICENSE and NOTICE files

Documentation

  • Documentation formatting appears as expected in rendered files

…s initializing

Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com>
@pvillard31 pvillard31 added the python Pull requests that update Python code label Jan 9, 2026
Copy link
Contributor

@exceptionfactory exceptionfactory left a comment

Choose a reason for hiding this comment

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

Thanks for working on this issue @pvillard31. The general strategy looks good. I noted a few initial recommendations. I also recommend reviewing most of the new log and exception messages to include a relevant detail, such as the Process ID or component ID where applicable.

private volatile String identifier;
private volatile PythonController controller;
private volatile CompletableFuture<Void> initializationFuture;
private volatile boolean cancelled = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Although either is technically acceptable English, other references in the project use canceled with one letter L, so I recommend using that form throughout these changes.

// This allows the venv creation to be interrupted when the process is being shut down
while (!venvProcess.waitFor(1, TimeUnit.SECONDS)) {
if (isShutdown()) {
logger.info("Interrupting Python Virtual Environment creation due to shutdown");
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be helpful to include the Process ID in this log.

if (isShutdown()) {
logger.info("Interrupting Python Virtual Environment creation due to shutdown");
venvProcess.destroyForcibly();
throw new IOException("Python process shutdown during virtual environment creation");
Copy link
Contributor

Choose a reason for hiding this comment

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

The Process ID would be helpful to include in this message.

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

Labels

python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants