gradio: Session not found whenever the request is routed to another instance

Normally whenever I interact with a Gradio app, I see (in browser Network tab) a POST request to “/gradio_api/queue/join?” with session_hash: "u26gd43ah8" in request payload and right after that I see a GET request to /gradio_api/queue/data?session_hash=u26gd43ah8

But If I’m running the app in a stateless environment (with multiple instances) the requests are distributed across different instances and the in-memory session state on one instance won’t be accessible to another. Therefore I only see this request in Network tab: “/gradio_api/queue/join?” with session_hash: "u26gd43ah8" but nothing after that as I get Session Not Found error.

Describe the solution you’d like
Does Gradio compare the session hash from the request payload against some session_hash in the server memory for it to continue? If so, I would like Gradio to get the session hash from a database instead of from the memory so it would persist and be stateless.

Additional context
I hope that made sense. Also I don’t want to use Redis or any other expensive solutions. I just need to know how to access and update whatever it is (missing) in the memory that causes the Session not found error, so I could fix it with a middleware.

Thank you.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 15 (6 by maintainers)

Most upvoted comments

Yeah exactly, we’re working on doing exactly this with @aliabid94

Thanks for answering. Redis seems the most obvious answer here for kubernetes but if I understand correctly it’s only one state.json file per session, so maybe there’s an easier way to do that (I guess it’s not a problem when you only do multithreading? I use mount_gradio_app I don’t remember if it allowed multithreading but I could try exploring that instead)

Hi again, just posting this interesting article that lists all the scenarios where a sticky session cookie is not enough: https://pauldally.medium.com/session-affinity-and-kubernetes-proceed-with-caution-8e66fd5deb05 (related: #9136)

I’m still debugging my issue: when I have it on one browser (still don’t know what triggers it), all the following requests are randomly getting answered with a set-cookie param that rewrites the sticky cookie value to another value (2 values competing in what I saw). Maybe because the cookie has HttpOnly set and doesn’t get sent via XHR? It’s still unclear for me.

In the end it might be an Ingress issue so not really in Gradio’s scope, but giving an option for a more mature session storage mechanism seems important for Gradio, as I now discovered an important point of failure in my Gradio deployment (I’ll have to think about a migration path that includes implementing a session store myself, maybe using Gradio as API only).

I think I am facing the same issue here. I have a gradio application deployed in k8s with some horizontal pod autoscaler (HPA). At first, the " 404 session not found" errors where pretty common, as the ingress was routing the traffic indistinctly.

After that I started using sticky sessions, which helps forcing the ingress to route the same traffic trough the same pods. The only problem come when the following scenario is met:

  • Traffic is high so HPA spins up new pods
  • New user creates traffic to one of newly created pods (and crates session id in that server, managed by that pod)
  • Information exchange continues
  • Traffic decreases, so HPA kills some pods
  • The user tries to interact again with the same pod as before (already killed), and due to sticky session, the session ID cannot be found in another randomly assigned pod (one of the remaining “alive” ones after the prune)

This is the issue I’m facing at the moment.

My solution goes through storing session ID in external DB such as redis, but I don’t seem to find an straightforward way to do so with gradio/FastAPI

Same for me

Sure. Session not found happens every time with any gradio app mounted within another fastapi app and using this command to run it:

CMD [“gunicorn”, “-b”, “0.0.0.0:8080”, “-k”, “uvicorn.workers.UvicornWorker”, “–worker-tmp-dir”, “/dev/shm”, “–workers”, “2”, “–threads”, “80”, “app:app”]

Seems like it can’t handle multiple workers?

This helps a little bit but I often still get the session not found error even though I have enabled the session affinity.