Determine from the client when a session timeout has occurred

What I’m trying to do:
Redirect the application to the login page when a session times out. We need to do this for business and regulatory reasons.

What I’ve tried and what’s not working:
Added a timer to the main form and check every minute to see if there is a valid user. If no valid user, then load the login form

Code Sample:

  def autolock_timer_tick(self, **event_args):
    Purpose of this is to hide all info on the page during timeout
    This method is called Every [interval] seconds. Does not trigger if [interval] is 0.
    # does the anvil.users.get_user() call keep the session open?
    user = anvil.users.get_user()
    if user is None:
      print("session timeout")

While testing this, it seems that the anvil.users.get_user() call counts as a server call for the purposes of keeping the session alive. Is there a way to do this from within the client that does not involve a server call that keeps the session alive?

Note: If there is a way to make a special server call that does not keep the session alive, that would work just as well for me.

The Documentation Search feature is your friend! See “Recovering from an expired session” in Session expiry.

Documentation Search feature is indeed my friend! Just not for this issue. I do not want to recover from a session.

If you read what I wrote again, you’ll see I want to detect that a server expiration has happened, without a server call failing, because I need to blank out the screen/redirect to login (sort of like a screen saver). If I make a server call (including, apparently anvil.users.get_user()) then it keeps the session alive which I do not want to do.

I’m not aware of any other way to reliably detect a timeout. As noted in the documentation, even implicit database activity will extend the life of the connection, literally putting off Anvil’s timeout until later.

If you’re trying to prevent that extension from occurring, it’s starting to sound like you want your own definition of “timeout”, not Anvil’s. That’s certainly feasible. You can do that with a Timer.

1 Like

Yeah, it certainly looks that way. Even print() statements from the client (which do get sent to the server) keep the connection alive.


You’re welcome.

Sometimes, it’s less work to piggyback on an existing definition, sometimes it’s more. By making your own definition, you can make it work exactly the way you want, completely untangled from Anvil’s machinery. That should make it much simpler and easier to specify, reason about, and build. And test.