Skip to content

Fix DuplicateBlockError in multi-page apps with gr.render during reload#13013

Merged
freddyaboulton merged 3 commits intogradio-app:mainfrom
gambletan:fix/duplicate-block-error-multipage
Mar 13, 2026
Merged

Fix DuplicateBlockError in multi-page apps with gr.render during reload#13013
freddyaboulton merged 3 commits intogradio-app:mainfrom
gambletan:fix/duplicate-block-error-multipage

Conversation

@gambletan
Copy link
Copy Markdown
Contributor

Summary

  • Fixes DuplicateBlockError when using gr.render in multi-page apps launched via gradio app.py (reload mode)
  • After the initial Context.id = 0 reset in watchfn, ensures Context.id is advanced past all existing block IDs so that dynamically created blocks (from gr.render callbacks) don't collide with blocks from already-imported child page modules

Problem

When running a multi-page app with gradio app.py:

  1. The original execution imports child page modules (e.g., page1.py, page2.py), creating blocks with sequential IDs starting from 0
  2. watchfn resets Context.id = 0 and re-executes the main module
  3. Since child modules are already in sys.modules, they are not re-imported — their blocks retain their original IDs
  4. The re-executed root Blocks gets a new low ID, and Context.id ends up at a low value
  5. When gr.render fires on first page load, new blocks get IDs that collide with existing block IDs, causing DuplicateBlockError

Fix

After the exec() call in watchfn, set Context.id to max(existing_block_ids) + 1 to prevent ID collisions.

Test plan

  • Reproduce the issue from DuplicateBlockError in multi page app with render #12078 with the multi-page app example (gradio app.py) and verify the DuplicateBlockError no longer occurs on first page load
  • Verify that page reload and hot-reload (code changes) still work correctly
  • Verify that single-page apps with gr.render are unaffected

Closes #12078

🤖 Generated with Claude Code

Ethan T. and others added 2 commits March 13, 2026 13:38
When running multi-page apps with `gradio app.py` (reload mode), the
watchfn resets Context.id to 0 before re-executing the module. However,
child page modules already imported retain their original block IDs.
This causes dynamically created blocks (via gr.render) to receive IDs
that collide with existing blocks, raising DuplicateBlockError on the
first page load.

Fix by ensuring Context.id is set past all existing block IDs after
the initial module re-execution in watchfn.

Fixes gradio-app#12078

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gradio-pr-bot
Copy link
Copy Markdown
Collaborator

gradio-pr-bot commented Mar 13, 2026

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-pypi-previews.s3.amazonaws.com/3f25d8c43047628203070bb31a642c154e581d9b/gradio-6.9.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@3f25d8c43047628203070bb31a642c154e581d9b#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-npm-previews.s3.amazonaws.com/3f25d8c43047628203070bb31a642c154e581d9b/gradio-client-2.1.0.tgz

@gradio-pr-bot
Copy link
Copy Markdown
Collaborator

gradio-pr-bot commented Mar 13, 2026

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio patch

  • Fix DuplicateBlockError in multi-page apps with gr.render during reload

✅ Changeset approved by @freddyaboulton

  • Maintainers can remove approval by unchecking this checkbox.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

Copy link
Copy Markdown
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

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

Verified the fix thanks @gambletan

@freddyaboulton freddyaboulton enabled auto-merge (squash) March 13, 2026 15:21
@freddyaboulton freddyaboulton merged commit 356db5c into gradio-app:main Mar 13, 2026
28 of 31 checks passed
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.

DuplicateBlockError in multi page app with render

4 participants