Skip to content

Conversation

@RostyslavKachan
Copy link
Collaborator

@RostyslavKachan RostyslavKachan commented Dec 1, 2025

RHINENG-22131

Secure Coding Practices Checklist GitHub Link

Secure Coding Checklist

  • Input Validation
  • Output Encoding
  • Authentication and Password Management
  • Session Management
  • Access Control
  • Cryptographic Practices
  • Error Handling and Logging
  • Data Protection
  • Communication Security
  • System Configuration
  • Database Security
  • File Management
  • Memory Management
  • General Coding Practices

Summary by Sourcery

Bug Fixes:

  • Prevent premature completion of the re_evaluation process by awaiting all Kafka send operations before finalizing.

@sourcery-ai
Copy link

sourcery-ai bot commented Dec 1, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Ensures that all asynchronous Kafka send operations in vmaas_sync.re_evaluate_systems complete gracefully before logging completion and committing, by tracking created futures and awaiting them at the end of processing.

Sequence diagram for graceful completion of Kafka send futures in re_evaluate_systems

sequenceDiagram
    participant re_evaluate_systems
    participant EVENT_LOOP as EVENT_LOOP
    participant BATCH_SEMAPHORE as BATCH_SEMAPHORE
    participant cur as cur
    participant EVALUATOR_QUEUE as EVALUATOR_QUEUE
    participant LOGGER as LOGGER

    re_evaluate_systems->>EVENT_LOOP: loop = EVENT_LOOP
    re_evaluate_systems->>re_evaluate_systems: total_scheduled = 0
    re_evaluate_systems->>re_evaluate_systems: futures = []

    loop while More database rows
        re_evaluate_systems->>EVENT_LOOP: run_until_complete(BATCH_SEMAPHORE.acquire())
        re_evaluate_systems->>cur: fetchmany(size = CFG.re_evaluation_kafka_batch_size)
        cur-->>re_evaluate_systems: rows
        re_evaluate_systems->>re_evaluate_systems: msgs = lists of system ids
        re_evaluate_systems->>re_evaluate_systems: total_scheduled += len(msgs)
        re_evaluate_systems->>EVALUATOR_QUEUE: send_list(msgs, loop = loop)
        EVALUATOR_QUEUE-->>re_evaluate_systems: future
        re_evaluate_systems->>future: add_done_callback(lambda x: BATCH_SEMAPHORE.release())
        re_evaluate_systems->>re_evaluate_systems: futures.append(future)
    end

    alt futures not empty
        re_evaluate_systems->>LOGGER: info(Waiting for N Kafka send operations to complete)
        re_evaluate_systems->>EVENT_LOOP: run_until_complete(asyncio.gather(*futures))
    end

    re_evaluate_systems->>LOGGER: info(N systems scheduled for re-evaluation)
    re_evaluate_systems->>DB as DB: conn.commit()
Loading

File-Level Changes

Change Details Files
Track and gracefully await all Kafka send futures before finishing re_evaluate_systems.
  • Introduce a list to collect futures returned by EVALUATOR_QUEUE.send_list during batch scheduling.
  • Append each newly created future to the tracking list after attaching the semaphore-release callback.
  • After the batching loop exits, if any futures were created, log how many pending Kafka send operations exist.
  • Use asyncio.gather within the existing event loop to await completion of all collected futures before logging total systems scheduled and committing the transaction.
vmaas_sync/vmaas_sync.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!

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 there - I've reviewed your changes - here's some feedback:

  • Accumulating all future objects in a single futures list for the entire while True loop can lead to unnecessary memory growth for large re-evaluation runs; consider periodically awaiting and clearing a batch-local list of futures (e.g., per DB fetch) instead of storing all of them until the very end.
  • When using asyncio.gather(*futures) without return_exceptions=True, a single exception in one send operation will raise and skip awaiting the rest; if partial completion is acceptable or you need to ensure all callbacks fire, consider return_exceptions=True and explicit error handling/logging.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Accumulating all `future` objects in a single `futures` list for the entire `while True` loop can lead to unnecessary memory growth for large re-evaluation runs; consider periodically awaiting and clearing a batch-local list of futures (e.g., per DB fetch) instead of storing all of them until the very end.
- When using `asyncio.gather(*futures)` without `return_exceptions=True`, a single exception in one send operation will raise and skip awaiting the rest; if partial completion is acceptable or you need to ensure all callbacks fire, consider `return_exceptions=True` and explicit error handling/logging.

## Individual Comments

### Comment 1
<location> `vmaas_sync/vmaas_sync.py:387` </location>
<code_context>
+
+            if futures:
+                LOGGER.info("Waiting for %s Kafka send operations to complete", len(futures))
+                loop.run_until_complete(asyncio.gather(*futures))
+
             LOGGER.info("%s systems scheduled for re-evaluation", total_scheduled)
</code_context>

<issue_to_address>
**question (bug_risk):** Async gather error handling might need to distinguish partial failures

Calling `asyncio.gather(*futures)` without `return_exceptions=True` means one failing Kafka send will raise and cancel the rest, changing behavior from “fire-and-forget” to “fail-fast and cancel peers.” If you want all sends to run and then handle/log failures individually, use `asyncio.gather(*futures, return_exceptions=True)` and handle exceptions explicitly so one bad send doesn’t prevent others from completing.
</issue_to_address>

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.


if futures:
LOGGER.info("Waiting for %s Kafka send operations to complete", len(futures))
loop.run_until_complete(asyncio.gather(*futures))
Copy link

Choose a reason for hiding this comment

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

question (bug_risk): Async gather error handling might need to distinguish partial failures

Calling asyncio.gather(*futures) without return_exceptions=True means one failing Kafka send will raise and cancel the rest, changing behavior from “fire-and-forget” to “fail-fast and cancel peers.” If you want all sends to run and then handle/log failures individually, use asyncio.gather(*futures, return_exceptions=True) and handle exceptions explicitly so one bad send doesn’t prevent others from completing.

@RostyslavKachan
Copy link
Collaborator Author

/retest

2 similar comments
@RostyslavKachan
Copy link
Collaborator Author

/retest

@RostyslavKachan
Copy link
Collaborator Author

/retest

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.

1 participant