DEV Community

Cover image for Did you ever face "stale singleton httpx connection" and "cold-start connection problem" problem, Well I did tonight.
Raqeeb
Raqeeb

Posted on

Did you ever face "stale singleton httpx connection" and "cold-start connection problem" problem, Well I did tonight.

It is been while I am learning and build around FastAPI. So there is a project where I was thinking how to add this new feature over exiting one. Like what changes I need to make in database which need to be reflected in my backend and frontend. I already lunched the web locally.

Problem started

When I when back to the web and reload it it shows this error:

ERROR: ConnectTimeout: Unauthorized 401.

I was like what? Why? I thougth there is some issue with login endpoint or refresh token function. When i did some debugging and found some new information which is:

"Either Supabase's edge/pooler (or OS, or an intermediate proxy/NAT) silently kills those idle connections server-side after some timeout but client-side pool doesn't know that."

As I was doing nothing in become idle state so to save the resources server side silently close that particular connection. So I came back and try to connect it give this error.

First thought come it my mind after this was there should be a way to automatically check this idle state and if user was in ideal state then create a new connection.

Proposed Solutions

After a while I come up with these solution:

  1. Calculate the Idle time: if it is more then server connection timeout then establish new connection.
  2. Retry logic: retry once on the specific connection errors.

I thought this will work but This again give me error then this new issue I faced.

Cold-start connection problem

There is something call dual-stack (IPv4 and IPv6) networks and Happy Eyeballs is a network mechanism which automatically move to IPv4 connection if IPv6 fails.

But supabase-py uses httpx and it doesn't support Happy Eyeballs. So in first try after the connection time out it try to establish IPv6 connection which is not routeable in most Pakistani ISPs and ultimately it fails and wait for timeout. There is no way to try it again for IPv4. So we have to do it manually.

So this error help me to learn many thing in process.

Share your thoughts.

Top comments (0)