Skip to content

Conversation

@shuoweil
Copy link
Contributor

@shuoweil shuoweil commented Dec 3, 2025

This PR addresses a flaky ZMQError: Address already in use that occasionally occurred during parallel notebook test execution.

Problem:
The bigframes.display module eagerly imported anywidget and traitlets at module load time (bigframes/display/__init__.py). This meant that when multiple Jupyter kernels were spun up simultaneously by nox for parallel testing, they would all try to initialize traitlets.HasTraits objects with sync=True properties. This led to race conditions and ZMQ port conflicts, causing notebook tests (including those that did not directly use anywidget like streaming_dataframe.ipynb) to fail. Log is here.

Solution:
The TableWidget class import in bigframes/display/__init__.py has been refactored to use Python's __getattr__ for lazy loading. This ensures that anywidget and traitlets are only imported and their associated kernel communication channels are initialized when display.TableWidget is actually accessed by the code. This prevents premature initialization and eliminates the port collision race condition during parallel test startup.

Fixes #<465768150> 🦕

@shuoweil shuoweil requested a review from tswast December 3, 2025 23:49
@shuoweil shuoweil self-assigned this Dec 3, 2025
@shuoweil shuoweil requested review from a team as code owners December 3, 2025 23:49
@product-auto-label product-auto-label bot added size: s Pull request size is small. api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. labels Dec 3, 2025
__all__ = ["TableWidget"]
except Exception:
pass
def __getattr__(name: str) -> Any:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't typical to do, so could you please add some comments explaining why we defer in this way in the docstring and/or comments as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for suggestion. I add docstring.

@shuoweil shuoweil requested a review from tswast December 4, 2025 19:01
Copy link
Collaborator

@tswast tswast left a comment

Choose a reason for hiding this comment

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

Thanks!

@shuoweil shuoweil merged commit 7e959b9 into main Dec 4, 2025
24 of 25 checks passed
@shuoweil shuoweil deleted the shuowei-fix-flaky-traitlets branch December 4, 2025 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. size: s Pull request size is small.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants