Skip to content

Conversation

@subCode321
Copy link
Contributor

@subCode321 subCode321 commented Mar 26, 2025

Fixes #154

Description

I've added a new raise exception block that throws an exception to be caught by the outermost try / catch block at the workflow run level to terminate the workflow run

Streamlit.Terminate.Run.mp4

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced error handling to promptly notify users of command execution failures, reducing the risk of silent errors and ensuring more robust error management.

@coderabbitai
Copy link

coderabbitai bot commented Mar 26, 2025

Walkthrough

The change introduces a new mechanism for handling exceptions in the run_multiple_commands method of the CommandExecutor class. A queue.Queue is used to capture exceptions from threads executing commands. The threading logic is updated to utilize a wrapper function that catches exceptions and places them in the queue. After execution, if any exceptions are found, the first one is raised as a RuntimeError. The run_command method is also modified to raise a RuntimeError upon command execution errors.

Changes

File Change Summary
src/workflow/CommandExecutor.py Introduced exception handling in run_multiple_commands using queue.Queue and modified run_command to raise RuntimeError on errors.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant CommandExecutor
    Caller->>CommandExecutor: run_multiple_commands(...)
    CommandExecutor->>CommandExecutor: Start threads for commands
    alt Exception Detected
        CommandExecutor->>Queue: Place exception in queue
    end
    CommandExecutor->>CommandExecutor: Wait for threads to complete
    alt Exceptions in Queue
        CommandExecutor->>Caller: Raise RuntimeError(exception)
    else No Exceptions
        CommandExecutor->>Caller: Return results
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Terminate workflow on error detection and display error logs (#154)

Poem

I’m a rabbit hopping through lines of code,
Where errors once slyly continued to abode.
Now a RuntimeError leaps straight to the top,
Halting workflows with one decisive hop!
🐰 Happy trails in every debug drop!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 30d746f and 5d49743.

📒 Files selected for processing (1)
  • src/workflow/CommandExecutor.py (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/workflow/CommandExecutor.py
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: build-openms
  • GitHub Check: build-simple-app
  • GitHub Check: build-full-app

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@subCode321 subCode321 marked this pull request as draft March 26, 2025 05:14
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/workflow/CommandExecutor.py (1)

63-108: Consider enhancing the error message for improved debugging

While the current implementation correctly raises an exception with the stderr content, consider including the command that failed in the error message for easier debugging.

- raise RuntimeError(error_message)
+ raise RuntimeError(f"Command failed: '{' '.join(command)}'\n{error_message}")
🧰 Tools
🪛 Ruff (0.8.2)

77-77: f-string without any placeholders

Remove extraneous f prefix

(F541)


98-98: f-string without any placeholders

Remove extraneous f prefix

(F541)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6a7766a and 30d746f.

📒 Files selected for processing (1)
  • src/workflow/CommandExecutor.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: build-full-app
  • GitHub Check: build-openms
  • GitHub Check: build-simple-app
🔇 Additional comments (2)
src/workflow/CommandExecutor.py (2)

108-108: Appropriate exception raising to propagate errors

This change aligns with the PR objective to terminate workflow runs on error, ensuring issues are not silently handled. The RuntimeError will propagate to the calling code, allowing proper error handling at higher levels.


28-62:

Details

❓ Verification inconclusive

Verify how exceptions impact parallel command execution

Since run_multiple_commands executes commands in separate threads, an exception in one thread won't affect others. Consider if you need additional exception handling to ensure that an error in one command correctly terminates the entire workflow.


🏁 Script executed:

#!/bin/bash
# Check if there are any exception handlers in code that calls run_multiple_commands
rg -A 3 -B 3 "run_multiple_commands.*\)" --include "*.py" --exclude "CommandExecutor.py"

# Check if there are try/except blocks that catch RuntimeError
rg -A 3 -B 3 "try:.*except\s+(RuntimeError|Exception)" --include "*.py"

Length of output: 1046


Action: Revisit Exception Handling in Parallel Command Execution

The current implementation of run_multiple_commands launches each command in its own thread without a centralized error-handling mechanism. This means that if one thread raises an exception (for example, within run_command), it will not affect or interrupt the execution of the other threads. Our initial search for external try/except blocks around calls to run_multiple_commands and for exception capturing in Python files didn’t yield conclusive results—likely due to the need to adjust search parameters.

Please consider the following:

  • Thread Exception Handling: If an exception in any thread should halt the overall workflow, you might need to wrap the thread target’s execution (i.e., the run_command method) in its own try/except block and propagate errors back to the main thread.
  • Caller Responsibility: Verify if the callers of run_multiple_commands are expected to handle such exceptions or if additional safeguards are needed.
  • Centralized Error Reporting: You may want to implement or integrate a mechanism that collects exceptions from all threads and makes a unified decision on workflow continuation.

Kindly validate whether this behavior meets your design intent and adjust the exception-handling strategy if a failure in one command must impact the entire operation.

@subCode321 subCode321 marked this pull request as ready for review March 26, 2025 06:16
Comment on lines +73 to +77
# Exceptions are logged once all threads have completed execution
# The first exception amongst all is logged and raised
if not exceptions_queue.empty():
raise RuntimeError(exceptions_queue.get())

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@t0mdavid-m what I've done here is that as the threads execute .... I catch the threads and then keep array of exceptions and as soon as all threads complete I throw/raise the first occurred exception to terminate the workflow. I've tested it out and it works. Only thing is that the termination happens after all threads complete.

Wouldn't it be safe to not kill other threads and simply let them run but in the minimal logs just show the error that was hit first?

Copy link
Member

Choose a reason for hiding this comment

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

I think we can kill all threads as soon as an exception was raised. The workflow will not be able to complete in any case after that so there is no reason to keep threads running.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So should I use Thread.Event to listen to a STOP event across all threads in a while loop within each thread run and terminate/kill the thread as soon as the event is set to true

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.

Workflows are not terminated if errors are detected

2 participants