Skip to content

Conversation

@shubham1g5
Copy link
Contributor

@shubham1g5 shubham1g5 commented Apr 9, 2025

Product Description

Correctly cancels the entity loading task on finishing activity as we are not going to reattach to the task again.
No user visible change.

Technical Summary

Solves an issue where the entity loader task keeps running even after the entity select activity has been finished due to user getting out of that screen.

This was causing multiple entity loading tasks to get intitiated and run simultaneously when you go back and come in again on the entity list before the entity list has been loaded completely.

The solution here is 2 parts -

  1. Change the mayInterruptIfRunning to true to make sure that the onCancelled gets called on time for AsyncTask.

  2. Correctly cancels the work by intercepting the stopLoading flag which is set to true on cancellation.

Safety Assurance

Safety story

It's a bit confusing to me that we were setting the interrruption flag to false on cancellation which is hard to know if it was an oversight or is done due to a particular reason I am unaware of. Although think if we are trying to cancel it's better to interrupt otherwise there is no guarantee on when it well get cancelled.

Automated test coverage

None

QA Plan

Not planning

Labels and Review

  • Do we need to enhance the manual QA test coverage ? If yes, the "QA Note" label is set correctly
  • Does the PR introduce any major changes worth communicating ? If yes, the "Release Note" label is set and a "Release Note" is specified in PR description.
  • Risk label is set correctly
  • The set of people pinged as reviewers is appropriate for the level of risk of the change

@shubham1g5 shubham1g5 requested review from avazirna and ctsims April 9, 2025 14:18
@coderabbitai
Copy link

coderabbitai bot commented Apr 9, 2025

📝 Walkthrough

Walkthrough

The changes update the cancellation behavior for entity loading processes. In the EntitySelectActivity class, the onDestroy method is modified to cancel the loader task forcefully by calling loader.cancel(true) instead of the previous non-interruptive loader.cancel(false). In addition, the EntityLoaderHelper class now includes an early exit condition in the loadEntitiesWithReferences method. A check for the stopLoading flag is added early in the loop over references, allowing the method to exit immediately and return the partially loaded entities if cancellation is requested. No public entity declarations have been altered in either file.

Sequence Diagram(s)

sequenceDiagram
    participant A as EntitySelectActivity
    participant L as Loader Task
    A->>L: onDestroy() calls loader.cancel(true)
    L-->>A: Forcefully terminates ongoing operations
Loading
sequenceDiagram
    participant Loader as EntityLoaderHelper
    participant Ref as References
    Loader->>Ref: Begin iterating over references
    alt stopLoading is true
        Loader-->>Loader: Immediately return current entities list
    else
        Loader-->>Loader: Process reference and continue loop
    end
Loading

Possibly related PRs

Suggested reviewers

  • OrangeAndGreen
  • pm-dimagi
  • avazirna
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.

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)
app/src/org/commcare/tasks/EntityLoaderHelper.kt (1)

100-102: Consider consistent return behavior on cancellation.

In the new code, cancellation at the beginning of the loop returns partial entities, while cancellation later in the loop still returns null. This inconsistency might lead to unexpected behavior.

Consider making both cancellation paths return the same value:

if (stopLoading) {
-    return null
+    return entities
}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 07b4543 and 2677826.

📒 Files selected for processing (2)
  • app/src/org/commcare/activities/EntitySelectActivity.java (1 hunks)
  • app/src/org/commcare/tasks/EntityLoaderHelper.kt (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
app/src/org/commcare/activities/EntitySelectActivity.java (2)
app/src/org/commcare/tasks/EntityLoaderHelper.kt (1)
  • cancel (115-118)
app/src/org/commcare/tasks/PrimeEntityCacheHelper.kt (1)
  • cancel (203-206)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Lint Code Base
🔇 Additional comments (2)
app/src/org/commcare/activities/EntitySelectActivity.java (1)

547-547: Improved task cancellation by allowing interruption.

This change sets the mayInterruptIfRunning parameter to true when canceling the loader task during activity destruction. This ensures that the task can be immediately interrupted rather than allowed to complete, which fixes the issue where entity loader tasks continue running even after the activity has been destroyed.

app/src/org/commcare/tasks/EntityLoaderHelper.kt (1)

92-94: Added early cancellation check to avoid unnecessary processing.

Adding an early exit condition at the beginning of the loop provides a more responsive cancellation mechanism. The task now exits immediately upon detecting cancellation and returns the partially loaded entities rather than continuing to process the remaining references.

This change, together with the loader.cancel(true) change in EntitySelectActivity, effectively addresses the issue where entity loading tasks continued running after the activity was finished.

…ity as we are not going to reattach to the task again
@shubham1g5 shubham1g5 force-pushed the entityLoadingCancellationBug branch from 2677826 to fcf9a74 Compare April 9, 2025 14:28
@shubham1g5 shubham1g5 marked this pull request as ready for review April 10, 2025 06:54
@shubham1g5 shubham1g5 added this to the 2.57 milestone Apr 11, 2025
@avazirna
Copy link
Contributor

avazirna commented Apr 15, 2025

I believe this block is no longer needed, given that when stopLoading is true, we are now returning the entities list

@shubham1g5
Copy link
Contributor Author

you are absolutely right, thanks for catching - da553e5

@shubham1g5 shubham1g5 merged commit 27e9552 into master Jun 4, 2025
7 of 9 checks passed
@shubham1g5 shubham1g5 deleted the entityLoadingCancellationBug branch June 4, 2025 16:26
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