Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the parent frame of a streamlit app to terminate/restart its websocket #8704

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

vdonato
Copy link
Collaborator

@vdonato vdonato commented May 17, 2024

We want to be able to let the parent frame of a streamlit app manage its websocket connection
lifecycle. This PR adds this functionality to our HostCommunicationManager class by adding four
new message types for interacting with a Streamlit app's websocket connection:

  • RESTART_WEBSOCKET_CONNECTION
  • TERMINATE_WEBSOCKET_CONNECTION
  • WEBSOCKET_DISCONNECTED (along with an attemptingToReconnect flag in the message payload)
  • WEBSOCKET_CONNECTED

.iframe(() => {
cy.waitForScriptFinish()
});
before(() => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Some things to note:

  • The vast majority of the changes to this file were applied by the autoformatter. Not sure how this file
    ended up with 4-space indents instead of 2.
  • The only actual change made to this file is the test added to the very end
  • We should be migrating this test to playwright soon, but it only took ~5 minutes to add a test here
    vs probably the day+ of work it'll take to migrate this test, so I think it's still worth doing this now
    to get the PR merged sooner than later. I added a TODO for myself to go back and migrate things later.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I actually had this same issue as well. I think we should migrate this to playwright, but I was going to suggest after MPA v2.

Copy link
Contributor

Choose a reason for hiding this comment

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

@raethlein added a new playwright fixture to run an app in iframe recently. The hostframe logic could potentially be integrated into this iframe fixture.

Copy link
Collaborator

Choose a reason for hiding this comment

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

here is an example test using the new fixture: https://github.com/streamlit/streamlit/blob/develop/e2e_playwright/st_dataframe_interactions_test.py#L329 in case it helps 🙂

@vdonato vdonato force-pushed the vdonato/host-websocket-msgs branch 2 times, most recently from 72a5d25 to b43e734 Compare May 21, 2024 23:13
@vdonato vdonato changed the title [WIP] Allow the parent frame of a streamlit app to terminate/restart its websocket Allow the parent frame of a streamlit app to terminate/restart its websocket May 21, 2024
@vdonato vdonato marked this pull request as ready for review May 21, 2024 23:14
@vdonato
Copy link
Collaborator Author

vdonato commented May 21, 2024

Heads up @sfc-gh-pfinnigan @sfc-gh-shaar that these message types should be ready for e2e testing in SiS.

Note the names of the messages are currently:

  • RESTART_WEBSOCKET_CONNECTION
  • TERMINATE_WEBSOCKET_CONNECTION
  • WEBSOCKET_DISCONNECTED (along with an attemptingToReconnect flag in the message payload)
  • WEBSOCKET_CONNECTED

but I'm not very opinionated on these names and am happy to change them if any better one are suggested.

Also, I haven't started looking into websocket close codes or backoffs on ws reconnect retries just yet, but I'll be doing some investigation of those over the next few days.

@vdonato
Copy link
Collaborator Author

vdonato commented May 21, 2024

Temporarily adding the do-not-merge label to this so that we don't actually submit it before some e2e testing is complete

.iframe(() => {
cy.waitForScriptFinish()
});
before(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I actually had this same issue as well. I think we should migrate this to playwright, but I was going to suggest after MPA v2.

this.hostCommunicationMgr.sendMessageToHost({
type: "SCRIPT_RUN_STATE_CHANGED",
scriptRunState: this.state.scriptRunState,
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the reasoning for the Script Run state changed event here? Is it just for the test?

I offered an idea that might make more sense, but in any case a comment here would be helpful to relate the two concepts if they are related.


// This test isn't ideal, but since we can't inspect the state of our app
// using RTL, we have to check that some other side effect of calling
// initializeConnectionManager occurs.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know it sounds silly, but have we considered also have a WEBSOCKET_CONNECTING message? No one needs to know it, but it does handle this in between state well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants