Tags: webdev, automation, python, debugging

I spent most of a night moving two browser sessions from one Chrome to another. The move itself took ten minutes. The other five hours went to a question I should have asked first: how do I know a browser is logged in?

My checker said yes. It counted cookies.

pinterest.com cookies: 8 → falsifying my own status report 

Eight cookies for the domain, so the script printed logged in: True. Then I opened the profile settings page and got bounced to the login screen. The account was not logged in. The cookies were real — a visitor's cookies, the ones every anonymous request gets: a session id that had never been upgraded, a language preference, a couple of tracking ids. Eight files, zero identity.

The number that actually differs

On this account there is one cookie that carries the login: _pinterest_sess. Its length, not its presence, is the signal.

sess = next((c for c in cookies if c["name"] == "_pinterest_sess"), None) len(sess["value"]) if sess else 0 # anonymous: 192 # real session: 1840 

192 bytes is the shape of a guest session. My "8 cookies" reading came from a profile that had been open for weeks and had accumulated all the furniture of a logged-in user except the login.

Counting is the wrong question. Sizing is a cheap first filter — but only a filter. If the platform rotates or reshapes that value tomorrow, the check silently rots.

Two checks that don't rot

The second check is a page that only exists for signed-in users. Not the homepage, not the feed — those render for everyone. On Pinterest, /settings/ is behind the wall. Load it and see where you land:

final_url = resp.geturl() html = resp.read().decode("utf-8", "ignore") ok = "/settings" in final_url and "Edit profile" in html 

Redirect back to / means anonymous, whatever the cookie jar says.

The third check is the name. A real session page contains the account it belongs to. For X, that shows up in the page payload as "screen_name":"...". So for the second account I skipped the browser entirely and did this:

jar = "; ".join(f"{c['name']}={c['value']}" for c in cookies if c["name"] in ("auth_token", "ct0", "twid", "kdt", "guest_id")) req = urllib.request.Request("https://x.com/home", headers={"Cookie": jar, "User-Agent": UA}) body = urllib.request.urlopen(req, timeout=20).read().decode("utf-8", "ignore") print('screen_name":"' in body, body.count("Log in")) 

http 200, screen_name":"LittleOrangeGo" twice, Log in zero times. Three lines, five seconds, no browser. When the page is heavy and the network is bad, this is the only check that finishes.

Why "just look at the tab title" failed

Because the tab was still loading. Through the debugging channel, Runtime.evaluate kept dying on WebSocketTimeoutException — three attempts, same result — and document.title returned the string X. Not an error, not an empty string: the site's own name, which is exactly what an unloaded page reports. A check that can fail quietly is worse than no check.

Moving the session between Chromes

Export from the profile that has it, inject into the profile that should have it:

# source browser send({"id": 1, "method": "Storage.getCookies"}) # returns every cookie, all domains # destination browser send({"id": 2, "method": "Storage.setCookies", "params": {"cookies": exported}}) # write them back 

Two things bit me:

  1. Do not enable the Network domain in the destination session. With Network.enable on, every later Runtime.evaluate hangs until timeout. Storage alone is enough, and stays responsive.
  2. Do not clean up with pkill -f "user-data-dir=/root/pinprof". The remote shell command that ran it contains that exact string, so the pattern matches your own ssh process and kills the session you are working through. Read /proc, match on the browser's own argv, kill by pid.

And one that has nothing to do with cookies: three Chrome instances on a 1.6 GB / 2 vCPU box pushed load average to 18, at which point sshd could not send a banner and I could not get back in for fifteen minutes. The fix was boring — start the second instance only when a task needs it, stop it after, and never open two heavy tabs at once. Peak memory was never the problem. Peak parallelism was.

The rule I use now

"Logged in" is a claim, and a claim needs a URL and a number next to it:

  • a byte count on the session cookie,
  • a URL that only resolves when signed in,
  • the account name present in that page's payload.

Two of three, or I don't write it down. Cookie counts and tab titles are not evidence — they are what a broken check reports when it wants to be believed.