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

Shut down the session after closing the browser tab by the user #8545

Open
2 tasks done
Anna050689 opened this issue Apr 19, 2024 · 4 comments
Open
2 tasks done

Shut down the session after closing the browser tab by the user #8545

Anna050689 opened this issue Apr 19, 2024 · 4 comments
Labels
area:backend type:enhancement Requests for feature enhancements or new features

Comments

@Anna050689
Copy link

Checklist

  • I have searched the existing issues for similar feature requests.
  • I added a descriptive title and summary to this issue.

Summary

I want to implement the mechanism to detect the user exit after closing the tab and shut down the session on the backend side. I used the solution provided in the streamlit discussion - https://discuss.streamlit.io/t/detecting-user-exit-browser-tab-closed-session-end/62066/2

My code snippet is the next -

import threading

from streamlit.runtime.scriptrunner import add_script_run_ctx
from streamlit.runtime.scriptrunner.script_run_context import get_script_run_ctx
from streamlit.runtime import get_instance


def heart_beat():
    """
    Heartbeat function to track whether the session is alive
    """
    thread = threading.Timer(interval=2, function=heart_beat)

    # insert context to the current thread, needed for
    # getting session specific attributes like st.session_state

    add_script_run_ctx(thread)

    # context is required to get session_id of the calling
    # thread (which would be the script thread)
    ctx = get_script_run_ctx()

    # this is the main runtime, contains all the sessions
    runtime = get_instance()

    if runtime.is_active_session(session_id=ctx.session_id):
        thread.start()
    else:
        return


if __name__ == "__main__":
    heart_beat()

Is it possible to shut down the session on the backend side?

Why?

It's important to have more controlled behavior, as our application will potentially interact with multiple users.

How?

No response

Additional Context

No response

@Anna050689 Anna050689 added the type:enhancement Requests for feature enhancements or new features label Apr 19, 2024
Copy link

To help Streamlit prioritize this feature, react with a 👍 (thumbs up emoji) to the initial post.

Your vote helps us identify which enhancements matter most to our users.

Visits

@jrieke
Copy link
Collaborator

jrieke commented Apr 19, 2024

Hey @Anna050689! Can you tell us a bit more about why you need to detect when the session is closing? E.g. do you have some things in your code that you need to close or clean up? More detail helps us to figure out how common this use case is and prioritize it.

@sfc-gh-jcarroll this might be a good issue for your hooks idea.

@Anna050689
Copy link
Author

Hello, @jrieke! The code snippet provided in the summary section detects when the user closes the browser tab. In this case, it's possible to detect this action and clean up some artifacts after the user exits. However, the session on the backend remains open. Since we want to support multiple simultaneous users, this could be resource-consuming in terms of memory and other resources.

@Asaurus1
Copy link
Contributor

I would also like to have a bit more granular control over how long AppSessionManager keeps tabs open. A configurable parameter like "websocketSessionTimeout" that would cause a session to be cleaned up if it hasn't heard from the browser in X seconds (would require a browser-heartbeat msg every so often) would be quite useful)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:backend type:enhancement Requests for feature enhancements or new features
Projects
None yet
Development

No branches or pull requests

4 participants