clear_panes() docstring says "optionally awaitable" but skipping the await causes DuplicateIds
Hey, first off — thanks for Textual, it's been genuinely fun to build with. 🙂
I hit a small gotcha with TabbedContent.clear_panes() that took me a
while to track down, so figured I'd flag it in case it's an easy doc fix
(or worth changing behavior-wise).
The docstring says it returns "an optionally awaitable object", which
reads as "safe to fire-and-forget". In practice, if you skip the await
and immediately re-add panes with the same explicit ids right after (a
pretty natural "refresh" pattern), the removal is only scheduled —
so the new panes race the old ones being torn down, and you get a
DuplicateIds crash. Not obvious from the docstring that this matters!
(This is a different flavor of bug from #5215, by the way — that one was
about auto-generated ids reusing a stale counter. Here the ids are
explicit and stable; the crash only happens because clear_panes()
wasn't awaited.)
Reproduction
This crashes on the first press of r:
from textual.app import App, ComposeResult
from textual.widgets import Footer, Label, TabbedContent, TabPane
class ExampleApp(App):
BINDINGS = [("r", "refresh", "Refresh (clear + re-add same ids)")]
def compose(self) -> ComposeResult:
yield TabbedContent(id="tabs")
yield Footer()
def on_mount(self) -> None:
self._render()
def _render(self) -> None:
tabs = self.query_one("#tabs", TabbedContent)
tabs.clear_panes() # not awaited
for i in range(2):
tabs.add_pane(TabPane(f"Tab {i}", Label(f"content {i}"), id=f"pane-{i}"))
def action_refresh(self) -> None:
self._render()
if __name__ == "__main__":
ExampleApp().run()
DuplicateIds: Tried to insert a widget with ID '--content-tab-pane-0', but a widget already exists with that ID (ContentTab(id='--content-tab-pane-0')); ensure all child widgets have a unique ID.
Making _render/on_mount/action_refresh async and doing
await tabs.clear_panes() instead fixes it — tested 5 refreshes in a row
with no crash.
Maybe worth doing
No strong opinion on which, but one of:
- Tweak the docstring so it's clearer that "optionally awaitable" doesn't mean "safe to ignore" if you're about to reuse the same ids, or
- Have
clear_panes()settle the teardown before any subsequentadd_pane()call can race it, so this can't bite people regardless of whether they await it.
Ran into this building hastty, a
little TUI for Home Assistant — refreshing its TabbedContent on a
keybinding is exactly the pattern above. Happy to help test a fix if
useful!
Textual Diagnostics
| Name | Value |
|---|---|
| Textual | 8.2.8 |
| Rich | 15.0.0 |
Python
| Name | Value |
|---|---|
| Version | 3.12.3 (GCC 13.3.0) |
Operating System
| Name | Value |
|---|---|
| System | Linux |
| Release | 6.8.0-... (Ubuntu 24.04) |
We found the following entry in the FAQ which you may find helpful:
Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.
This project is developed and maintained by Will McGugan. Consider sponsoring Will's work on this project (and others).
This is an automated reply, generated by FAQtory
Proposed fix for the clear_panes() / re-add same explicit IDs race:
- Fork PR: https://github.com/Grok-Bot-Army/textual/pull/1
- Compare for upstream PR: https://github.com/Textualize/textual/compare/main...Grok-Bot-Army:textual:fix/clear-panes-refresh-race-6715?expand=1
- Tip: c268d07516fd627382fde4490c4d6b5e617f84e6
add_pane waits for any in-flight clear_panes before mounting; docstring notes await semantics. Regression test covers the fire-and-forget refresh pattern from this issue.
openjobs.bot listing e0eec255-8cfb-473a-b678-86312df30c6d https://openjobs.bot/jobs/e0eec255-8cfb-473a-b678-86312df30c6d
Revision / maintainer path (OpenJobs)
Textualize/textual sets pull_request_creation_policy: collaborators_only. Non-collaborator CreatePullRequest fails (GraphQL permission denied; REST POST /pulls → HTTP 404). Cannot open an upstream PR from outside.
Code (unchanged tip)
- Tip commit:
c268d07516fd627382fde4490c4d6b5e617f84e6 - Personal fork PR: https://github.com/Wisernage/textual/pull/1
- Org fork PR: https://github.com/Grok-Bot-Army/textual/pull/1
- Compare (personal): https://github.com/Textualize/textual/compare/main...Wisernage:textual:fix/clear-panes-refresh-race-6715?expand=1
- Compare (Grok-Bot-Army): https://github.com/Textualize/textual/compare/main...Grok-Bot-Army:textual:fix/clear-panes-refresh-race-6715?expand=1
Change
add_pane waits for any in-flight clear_panes before mounting; docstring notes await semantics. Regression: test_clear_panes_readd_same_ids_without_await. Full tests/test_tabbed_content.py: 42 passed.
openjobs.bot listing e0eec255-8cfb-473a-b678-86312df30c6d https://openjobs.bot/jobs/e0eec255-8cfb-473a-b678-86312df30c6d
Hi! I'm a beginner open-source contributor, and I'd like to help with this issue.
Would a documentation fix clarifying when clear_panes() should be awaited be welcome?
If so, could you confirm whether external contributors can submit pull requests?
Thank you!
I've prepared a documentation clarification plus a regression test for this:
https://github.com/Textualize/textual/compare/main...Aryan02090:docs/clear-panes-await-semantics-6715?expand=1
It is the docstring option (option 1 from the issue): the Note: on clear_panes() explains that removal is asynchronous and shows the awaited refresh pattern, with cross-references on add_pane() and remove_pane(). The test covers five clear + re-add cycles with the same IDs after awaiting.
I reproduced the crash on main as described, and confirmed the awaited version is clean.
One practical note: opening the PR directly fails for external accounts — GraphQL CreatePullRequest returns a permission error and REST POST /repos/Textualize/textual/pulls returns 404. So a maintainer would need to open the PR from the branch, or let me know if there is another route you prefer. Happy to adjust the change (including switching to the add_pane() ordering fix) if that is the preferred direction.