Blog
EngineeringFor engineers

Signing in to a Wi-Fi controller with no browser, and the servers that logged each other out

To read a voucher's balance, our backend signs in to TP-Link's cloud the way a person at the dashboard would. The account allows one session at a time, and for a while our own servers kept signing each other out.

Lodge Internet · 14 November 2026 · 6 min read

A student sends a code to our WhatsApp bot to see how much data is left. Before we can answer, our backend has to be signed in to the cloud dashboard that runs that hostel's Wi-Fi. Nobody is sitting at that dashboard and there is no browser. In August 2026, the thing most likely to sign us out of it was our own backend.

Why we sign in at all

Every hostel we serve runs its Wi-Fi on a TP-Link Omada controller, and the codes we sell are vouchers on those controllers. To tell you what is left on a code, we ask the controller that issued it.

TP-Link has an official API for this, but for our controllers it answers with an error that TP-Link knows about and hasn't fixed. So we sign in the way the dashboard itself does, with no browser involved. It works. It also takes a few seconds (the code comments measure it at about 4.4), which is fine once and painful on every lookup.

One lesson from building it is worth passing on. A session that looks complete is not proof that it works. We once had every cookie and token in hand while every controller still rejected the session. So nothing we sign in with is shared until it has been checked against the controllers themselves.

One account, one session

Two facts set up the problem.

First, all of our controllers sit behind a single cloud login, so one session serves all of them.

Second, the cloud allows one active session per account. Sign in again and the previous session is revoked.

Our backend runs as several worker processes. Early on, each worker kept its own idea of the session, and each one handled a rejected session the obvious way: sign in again. Here is what that does. Worker A's session dies, so A signs in. That revokes the session B is using. B's next call is rejected, so B signs in, which revokes A's. The docstring on _recover_from_dead_session records the result plainly: they kept logging each other out until the process restarted and wiped every worker's local copy.

There is a second reason a loop like that is dangerous. A wrong password locks the account after a few attempts, and that lock would stop balance checks on every controller at once. So a failed sign-in never retries on its own, anywhere in the module.

What we do now

One shared session

The session lives in Redis, where every worker reads the same one. Each worker also keeps a short-lived copy in memory, so a lookup doesn't pay a Redis round-trip just to fetch it.

Each session gets an id of its own when we create it. That small detail carries the rest of the design: it lets a worker tell "a newer session someone else made" apart from "the same dead one I already had". Cached sessions from before the ids existed are ignored and replaced with one clean sign-in, because a worker can't make that distinction about them.

A lock, so only one worker signs in

get_session takes a short lock before signing in. The worker that gets it signs in, publishes the result to Redis, and releases the lock. Everyone else waits and polls for the session to appear.

The waiting worker skips the session it was trying to replace. Without that, it could find the dead session still in the cache, call it a success, and hand it back. That is exactly why the warm-up job used to report "refreshed" while lookups kept failing.

Look for a newer session before signing in again

When a call comes back logged out halfway through a lookup, the worker no longer reacts by signing in. It asks the cache first:

async def _recover_from_dead_session(dead):
    cached = read_shared_session()
    if cached and cached["sessionId"] != dead["sessionId"]:
        # Another worker already signed in. Check theirs works, then use it.
        return cached
    # The cache holds the same dead session, or nothing: sign in once,
    # under the same lock as everyone else.
    clear_shared_session()
    return await get_session(force_refresh=True)

The real function also verifies the adopted session against the controllers before trusting it, and falls through to a full sign-in if that fails. Shared state is the single source of truth, and signing in is the last resort, not the first reflex.

A sign-in nobody may abandon

This one took longer to see. A balance lookup has a time budget, because a customer is waiting. Suppose a lookup is cancelled in the middle of a sign-in. TP-Link has already revoked the old session at that point, and we haven't yet published the new one. Every worker would then discover the dead session separately, and they would race to sign in over each other, which is the loop we had just removed.

So the sign-in runs as its own task, shielded from the caller's cancellation:

task = asyncio.ensure_future(sign_in_and_publish())
session = await asyncio.shield(task)

The caller is free to give up. The sign-in finishes and lands in the cache anyway. It is also why lookups check their own deadline between round-trips instead of relying on being cancelled from outside.

A warm-up job, so customers don't pay for the sign-in

The session is cached for a few hours. Whoever checks a balance first after it lapses would pay the whole sign-in cost. backend/scripts/omada_prewarm.sh runs on a schedule, comfortably inside that lifetime, and asks the backend to sign in ahead of time. Now that person is a scheduled job, not a student.

The warm-up forces a fresh session and checks it against every configured controller before it reports success. A "refreshed" answer means a real lookup will work. Cookies coming back from the sign-in are no longer enough to claim it. If it fails, nothing breaks: the next real lookup signs in on demand, as it would have anyway, and we get an alert email so a broken sign-in is known even on a quiet afternoon.

The trade-offs

A forced sign-in revokes the session every worker is holding. That is acceptable only because recovery is cheap now. A worker with the old copy gets one rejection, finds the newer session in the cache, checks it, and carries on.

If Redis is unreachable, a worker signs in without the lock. We chose a redundant sign-in over no sign-in, knowing it can revoke a session another worker is using. Redis being down is rare, and the recovery path above handles the fallout.

The lock has a timeout. If the worker holding it stalls, the waiters give up after a while, log a warning, and sign in themselves. That is better than every customer waiting on one stuck process.

What's still open

We still depend on an undocumented dashboard API. TP-Link can change it without notice. If every lookup starts failing at once, the first step is to compare our requests with what the dashboard sends today.

All the controllers share one account. A locked account, or a sign-in that breaks on TP-Link's side, stops balance checks everywhere at once. Self-hosted controllers, which we reach through TP-Link's Open API with a token of their own, don't need the cloud session at all. That is one way out of this dependency, one controller at a time.

The other half of a balance check, working out which controller to ask, is its own story: Six characters aren't an address.

Last checked against the product on 14 November 2026.

More in Engineering