Fix DuplicateBlockError in multi-page apps with gr.render during reload#13013
Merged
freddyaboulton merged 3 commits intogradio-app:mainfrom Mar 13, 2026
Merged
Conversation
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>
Collaborator
🪼 branch checks and previews
Install Gradio from this PR pip install https://gradio-pypi-previews.s3.amazonaws.com/3f25d8c43047628203070bb31a642c154e581d9b/gradio-6.9.0-py3-none-any.whlInstall 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 |
Collaborator
🦄 change detectedThis Pull Request includes changes to the following packages.
✅ Changeset approved by @freddyaboulton
|
freddyaboulton
approved these changes
Mar 13, 2026
Collaborator
freddyaboulton
left a comment
There was a problem hiding this comment.
Verified the fix thanks @gambletan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DuplicateBlockErrorwhen usinggr.renderin multi-page apps launched viagradio app.py(reload mode)Context.id = 0reset inwatchfn, ensuresContext.idis advanced past all existing block IDs so that dynamically created blocks (fromgr.rendercallbacks) don't collide with blocks from already-imported child page modulesProblem
When running a multi-page app with
gradio app.py:page1.py,page2.py), creating blocks with sequential IDs starting from 0watchfnresetsContext.id = 0and re-executes the main modulesys.modules, they are not re-imported — their blocks retain their original IDsBlocksgets a new low ID, andContext.idends up at a low valuegr.renderfires on first page load, new blocks get IDs that collide with existing block IDs, causingDuplicateBlockErrorFix
After the
exec()call inwatchfn, setContext.idtomax(existing_block_ids) + 1to prevent ID collisions.Test plan
gradio app.py) and verify theDuplicateBlockErrorno longer occurs on first page loadgr.renderare unaffectedCloses #12078
🤖 Generated with Claude Code