Defer Jupyter Comm initialization until frontend is ready #6229
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.
This resolves an issue when there is a race condition between the frontend being fully initialized (i.e. Bokeh and Panel JS and all extensions being loaded) and the backend trying to send an event to the frontend. This could occur if a user loaded the extension, rendered some component and then sent a message to it in rapid succession. Sending a message to the frontend would automatically attempt to open a Comm but if the frontend wasn't ready this Comm would never connect and we'd end up with a completely dead Comm, i.e. no messages could be sent across it.
This PR resolves this issue by waiting on the frontend to open the so called client comm, which indicates that the frontend is fully initialized. Only once the client comm is open we initialize the server comm. If a user attempts to send an event across the unopened server comm we schedule the message on the event loop until the comm has successfully been initialized.
Fixes #6178