Skip to content

fix(content_libraries): keep collection num_children in sync after item updates#38322

Open
kingoftech-v01 wants to merge 1 commit intoopenedx:masterfrom
kingoftech-v01:fix/bug-35776
Open

fix(content_libraries): keep collection num_children in sync after item updates#38322
kingoftech-v01 wants to merge 1 commit intoopenedx:masterfrom
kingoftech-v01:fix/bug-35776

Conversation

@kingoftech-v01
Copy link
Copy Markdown

Description

When adding or removing items from a library collection, the search index's num_children count was stale. Users saw incorrect counts on collection cards until a later unrelated edit triggered another reindex.

User impact (Library Author): Collection counts in the Studio library view reflect the actual number of items immediately after add/remove operations.

Root Cause

update_library_collection_items (previously update_library_collection_components) relied on Django's Collection.post_save signal to emit LIBRARY_COLLECTION_UPDATED. That signal fires before the M2M entities relationship commits. The synchronous reindex handler (search/handlers.py:234) then runs searchable_doc_for_collection, which computes num_children = filter_publishable_entities(collection.entities, has_draft=True).count() against the stale pre-commit M2M state. The final count is never corrected because the m2m_changed handler only emits CONTENT_OBJECT_ASSOCIATIONS_CHANGED per item, not LIBRARY_COLLECTION_UPDATED.

Git history:

Fix

Two surgical changes:

  1. openedx/core/djangoapps/content_libraries/api/collections.py — emit LIBRARY_COLLECTION_UPDATED with background=True explicitly after add_to_collection / remove_from_collection. The background=True flag routes the reindex to a Celery task that runs post-transaction, after the M2M commit, so it sees the correct count. This mirrors set_library_item_collections, delete_library_block, restore_library_block, delete_container, and restore_container.

  2. openedx/core/djangoapps/content_libraries/rest_api/collections.py — decorate LibraryCollectionsView with @method_decorator(transaction.non_atomic_requests, name="dispatch"), matching every other view in the same subpackage (blocks.py, libraries.py, containers.py). Without this, a Celery .delay() dispatch from inside the per-request atomic transaction could be consumed before the transaction commits.

Supporting Information

Testing Instructions

  1. In Studio, create a content library and a collection.
  2. Add items to the collection.
  3. Verify the collection card shows the correct item count immediately (before the fix it would show a stale count).
  4. Remove items. Verify the count decrements immediately.
  5. Confirm search/index consistency: searchable_doc_for_collection(collection_key)["num_children"] matches collection.entities.count().

Two new/updated tests in openedx/core/djangoapps/content_libraries/tests/test_api.py:

  • test_update_library_collection_components_event — updated expectation from 4 to 5 events and added a final-call assertion verifying the background=True emit.
  • test_bug_35776_regression_update_items_emits_background_updated — regression test covering both add and remove paths, asserting background=True emissions.

Deadline

None

Other Information

  • No database migrations
  • No new dependencies
  • No deprecations
  • Emits two LIBRARY_COLLECTION_UPDATED per update_items call (one from post_save, one explicit after M2M) — the second overrides the first, final state is correct. Matches the behavior already accepted for set_library_item_collections.
  • non_atomic_requests decorator matches every other view in content_libraries/rest_api/. The sub-operations in the view are already individually atomic where needed.

Closes #35776

…em updates

update_library_collection_items relied on Collection.post_save to emit
LIBRARY_COLLECTION_UPDATED, but post_save fires before the M2M entities
commit, so the synchronous reindex in searchable_doc_for_collection
computed num_children from the stale membership. Emit the signal
explicitly with background=True after the add/remove so the async
reindex runs post-commit with the correct count, matching the pattern
already used by set_library_item_collections, delete_library_block,
restore_library_block, delete_container, and restore_container.

Also mark LibraryCollectionsView with non_atomic_requests so Celery
tasks dispatched from the view are enqueued after the request
transaction commits, matching every other view in
openedx/core/djangoapps/content_libraries/rest_api/.

Closes openedx#35776
@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Apr 10, 2026
@openedx-webhooks
Copy link
Copy Markdown

Thanks for the pull request, @kingoftech-v01!

This repository is currently maintained by @openedx/wg-maintenance-openedx-platform.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Submit a signed contributor agreement (CLA)

⚠️ We ask all contributors to the Open edX project to submit a signed contributor agreement or indicate their institutional affiliation.
Please see the CONTRIBUTING file for more information.

If you've signed an agreement in the past, you may need to re-sign.
See The New Home of the Open edX Codebase for details.

Once you've signed the CLA, please allow 1 business day for it to be processed.
After this time, you can re-run the CLA check by adding a comment below that you have signed it.
If the CLA check continues to fail, you can tag the @openedx/cla-problems team in a comment for further assistance.

🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

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

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

Collection count and contents are sometimes incorrect

2 participants