Skip to content

Fix resolve concurrency issues with letters status being polled#2055

Merged
NoeBerdoz merged 14 commits into14.0-MyCompassion2from
FIX-Resolve-concurrency-issues-with-letters-status-being-polled
Oct 31, 2025
Merged

Fix resolve concurrency issues with letters status being polled#2055
NoeBerdoz merged 14 commits into14.0-MyCompassion2from
FIX-Resolve-concurrency-issues-with-letters-status-being-polled

Conversation

@Shayan105
Copy link
Member

@Shayan105 Shayan105 commented Oct 22, 2025

⚠️ Important note

This PR is linked to this PR : CompassionCH/compassion-website#181

Summary

This PR fixes some issues resulting from the merging of the three following tasks :

🐛 Problem Description

After the recent merges, several regressions were identified:

  1. Page Limit Bypass: The validation check to enforce the 15-page limit on letters was no longer being triggered, allowing oversized letters to be submitted.
  2. UI Deadlock: A race condition between the backend process updating the generation_status and the frontend polling for that status was causing a deadlock. The UI would get stuck in a loading state because it was reading an inconsistent status from the database before the transaction committed.
  3. No Retry on Failure: If a letter failed generation (e.g., for being over the page limit), its status was permanently set to 'failed'. Even after the user corrected the letter, the system would not allow a new generation attempt.

✅ Proposed Solutions

To resolve these issues, the following changes have been implemented:

  1. Reinstate Page Limit Check: The preview() method, which contains the page count validation, is now called during the final "send" action. This ensures the page limit is checked just before submission.
  2. Eliminate Race Condition: To guarantee the polling mechanism sees fresh data, the database update for the generation_status is now forced to commit immediately in its own transaction. This ensures atomicity and prevents the frontend from reading a stale state.
  3. Enable Resubmission After Failure: The letter generator's state is now correctly reset when a user attempts to resubmit a letter that previously failed. The status and generation_status fields are reverted to their initial values, allowing the corrected letter to be processed as a new attempt.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Shayan105, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves critical concurrency issues that arose from the integration of letter draft functionality, progress bar updates, and page limit enforcement. By introducing atomic database transactions for updating letter preview and generation statuses, the changes ensure data integrity and reliability, preventing inconsistent states when multiple operations might be attempting to modify the same letter record simultaneously.

Highlights

  • Concurrency Fixes: Addressed concurrency issues related to the polling of letter statuses by implementing atomic database updates for letter generation and preview processes.
  • Atomic Database Operations: Modified the preview and generate_letters_job methods to perform database writes within a new cursor and explicitly commit changes, ensuring atomicity and preventing race conditions.
  • Improved Error Handling: Updated the error message extraction in the preview method to use str(error) for non-PolicyError exceptions, providing more robust error reporting.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors parts of the S2B letter generation to use separate database cursors for status updates, which is a good approach to resolve concurrency issues with UI polling. However, the refactoring has introduced a few issues. In both the preview and generate_letters_job methods, the logic to update the generation_status and clear generation_error_message on success was accidentally removed. There is also a duplicated line in the preview method. I've provided suggestions to fix these omissions and the redundant code. Additionally, there's a minor inconsistency in the return value of generate_letters_job which I've also pointed out.

Shayan105 and others added 2 commits October 22, 2025 12:01
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@NoeBerdoz NoeBerdoz requested a review from ecino October 22, 2025 12:01
@ecino
Copy link
Member

ecino commented Oct 22, 2025

I'm not sure this change is needed. The new cursor you are creating is at the end of the method. At the end of the method when it returns, the main transaction will be committed so I don't see why this will effectively change something. Both polling the status and updating it should not cause deadlocks. It's doing simultaneous writes that can cause deadlocks.

Shayan105 and others added 7 commits October 22, 2025 17:08
Fix sytax error.

Refactor and ensure atomiity of transaction

Fix concurrency problem

Refactor and add comments

Remove typos

Co-Authored-By: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…ith-letters-status-being-polled' into FIX-Resolve-concurrency-issues-with-letters-status-being-polled
@Shayan105
Copy link
Member Author

Shayan105 commented Oct 23, 2025

@ecino After discussion the following change has been applied : replace the update_generation_status() to a mode general isolated_write() do isolated trasactions to the DB.
Some refactoring were applied on this repo and also in this one : https://github.com/CompassionCH/compassion-website/pull/181/files

Comment on lines 174 to 175
"generation_status": "done",
"generation_error_message": False,
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we keep this in case the preview is not called from the frontend controller but from the backend ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Changes applied.

{
"state": "done",
"date": fields.Datetime.now(),
"generation_status": "done",
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we keep this for the backend calls?

Copy link
Member Author

Choose a reason for hiding this comment

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

Changes applied.
BPnQGGY

@Shayan105 Shayan105 requested a review from ecino October 29, 2025 13:21
@NoeBerdoz NoeBerdoz merged commit 724fe4b into 14.0-MyCompassion2 Oct 31, 2025
1 check passed
@NoeBerdoz NoeBerdoz deleted the FIX-Resolve-concurrency-issues-with-letters-status-being-polled branch October 31, 2025 08:56
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.

3 participants