Blitz icon indicating copy to clipboard operation
Blitz copied to clipboard

[Bug]: Disconnections on android Phone

Open hayderi opened this issue 1 year ago • 6 comments

What system are you using?

Ubuntu 22.04

What version are you using?

1.11.0

What went wrong?

I’m currently using the Karing app with Singbox and really appreciate your panel—great work! However, I’m facing a connectivity issue specifically on my Android device (Samsung Fold 6). When the phone is idle for even a short time—around a minute or less—it disconnects. Upon unlocking or waking the device, it takes about 10 seconds to reconnect, during which I receive delayed notifications like WhatsApp messages and missed calls. I’ve already granted all necessary permissions and disabled memory optimization for both the Karing and Singbox apps. Interestingly, this issue doesn’t occur on my iPhone.

Any other helpful information?

What i do is i keep my iphone and android aside and keep them there for a minute and so and then ask someone to whatsapp call me. I get the call on my iphone and but on android no call. when i open my android app it gets connected after 5 to 10 seconds and then get all the calls at once. there is no battery saving mode on my android either.

hayderi avatar May 29 '25 17:05 hayderi

Hi, thank you for writing about this issue! I have the very same problem. As you said, after being idle for a while, internet connection would be disrupted and it would take 10 seconds or even more for me to have internet access again. I checked and changed many variables, couldn't solve it. Changed operator, client, and server, DNS in sub. Tested mci, mtn and rightel. Used nekobox, sing box and v2rayng. tested two servers, one in USA and the other in Germany. Used 443 single port and port hopping with 50000 ports in range, 10s to 30s hopping interval, didn't solve the issue. I wonder why i don't face this on windows.

salehMomtaz avatar May 30 '25 08:05 salehMomtaz

Same i have tried everything. Love their panel and the speed i get. But this bug is really frustrating. I hope the find an issue i have also added few things in it. I have made the following changes:

Enabled BBR TCP Congestion Control: • Set net.ipv4.tcp_congestion_control = bbr • Set net.core.default_qdisc = fq for better queueing • Adjusted QUIC Flow Control Buffers: • initStreamReceiveWindow: 8388608 • maxStreamReceiveWindow: 8388608 • initConnReceiveWindow: 20971520 • maxConnReceiveWindow: 20971520 • Optimized for mobile latency and reconnection speed

Increased QUIC Idle Timeouts: • maxIdleTimeout: 120s • udpIdleTimeout: 300s (5 minutes)

Swappiness & Cache Pressure Tweaks: • vm.swappiness = 10 • vm.vfs_cache_pressure = 50 • Enabled TCP Keep-Alive: • tcp_keepalive_time = 60 • tcp_keepalive_intvl = 15 • tcp_keepalive_probes = 5

Enabled QUIC speed test: • "speedTest": true

hayderi avatar May 30 '25 08:05 hayderi

I face it on android only not on any other and i am using it on fold 6 so it no slow phone either that it is killing the process or something.

hayderi avatar May 30 '25 08:05 hayderi

I used different devices, samsung a25 android 14 oneui 6.1, xiaomi 11t pro android 14 hyperos, xiaomi redmi 9 miui 12, the problem was persistent

salehMomtaz avatar May 30 '25 09:05 salehMomtaz

Greetings, It would be better to open this issue in the Hysteria2 core repository as well.

This issue is not exactly related to the Blitz panel, and you can easily edit any part of the configuration you want through the panel.

But according to my experience, this also happened in singbox core for Hysteria2 and the connection was disconnected if the phone was put on standby.

Good luck.

ReturnFI avatar May 31 '25 12:05 ReturnFI

Below is a concise breakdown of what’s being described in that bug report (and the accompanying discussion), written in everyday terms:

⸻

  1. The core problem • Symptom: When a Hysteria client (for example, on a phone) suddenly loses its Internet connection—say you turn off mobile data or switch from one carrier to another—the server often does not immediately realize that the client is gone. Instead, it continues showing “dead” sessions as still being “online.” • Impact: Over time, these “ghost” sessions accumulate on the server. The /online endpoint or whatever tool you use to list active sessions becomes inaccurate, because it can’t distinguish “client really still connected” from “client went offline without telling us.” That makes it impossible to know exactly how many live users there are or to enforce per-session limits properly.

⸻

  1. How Hysteria is supposed to detect a dead QUIC session • Internally, Hysteria is built on QUIC (UDP), which was designed to notice when the other side has vanished—provided a certain period of “network silence” passes. That period is controlled by the maxIdleTimeout setting. • maxIdleTimeout (for QUIC) tells the server: “If no packets arrive from this client for X seconds, assume that connection is dead and tear it down.” Once torn down, the server should emit a log line like:

INFO client disconnected {"addr": "...", "id": "user123", "error": "timeout: no recent network activity"}

and remove that session from the “online” list.

⸻

  1. What’s happening in practice (the bug)
    1. Normal disconnects work fine. • If you explicitly “disconnect” in your Hysteria client (e.g. hit the “disconnect” button in Sing-box or Husi), the client sends a proper close message. The server sees:

INFO client disconnected {"error": "accepting stream failed: Application error 0x0 (remote)"}

and immediately removes that session.

2.	Idle timeout also usually works.
•	If you stay connected but do nothing (screen off, no network usage), QUIC’s keep-alive probes will still tickle the connection. After about maxIdleTimeout seconds of true inactivity, you’ll see in the logs:

INFO client disconnected {"error": "accepting stream failed: timeout: no recent network activity"}

which means the server correctly decided “this client is gone” and cleaned it up.

3.	The “edge case” where sessions don’t get cleaned up.
•	If the client is actively streaming (e.g. watching YouTube) and then abruptly loses connectivity—for example, switching from LTE to Wi-Fi or moving between cells—the server sometimes never receives the usual “no more keep-alive probes,” or it gets confused by rapid network‐interface changes. As a result, no “disconnected” log line appears at all, and the session remains listed indefinitely.
•	When the user later reconnects (over a new IP), the server creates a brand-new QUIC session alongside the old “dead” session. So you wind up with two—or more—entries for the same user in /online. Over time, these pile up.

⸻

  1. Observations from their testing • Login/disconnect count spikes One user (7092005) has an inflated “number of logins” (nlogin)—3,164 within 27 days—which implies thousands of ephemeral sessions. Many of those were never properly torn down, so the server kept counting them. • Four test scenarios
    1. Normal connect → normal disconnect • Server logs a clean “client connected” and then a matching “client disconnected” with Application error 0x0 (remote). • Everything is removed from /online correctly.
    2. Connect, stay idle, let screen turn off • Server eventually logs “client disconnected … timeout: no recent network activity.” • Session is removed after maxIdleTimeout. Works as intended.
    3. Connect, actively use (YouTube), then turn network OFF • Server emits a series of WARN TCP error lines (because QUIC’s TCP‐over‐UDP fallback is failing mid-stream). • Finally, after some time, the server logs “client disconnected … timeout: no recent network activity.” • In this scenario, it does eventually drop the session—so you don’t end up with a ghost entry (but it may take a little longer).
    4. Connect on one network → immediately switch to another network + turn off old network • In practice, the server sees two “client connected” lines (one for each IP). • Ideally, it should tear down the old IP’s session once it sees the new IP or times out. In many tests, it actually did log a disconnect for each of those IPs with “timeout: no recent network activity.” But occasionally, one of those disconnections never materialized, leaving a stale entry.

In short, most of the time QUIC’s built-in keep-alive+maxIdleTimeout does its job. But under heavy churn or high‐rate reconnections (hundreds of logins per day), occasionally a session slips through the cracks and the server never logs a proper “disconnected” event.

⸻

  1. Why is this a problem? • /onlinesession (or any “who’s connected” API) becomes unreliable. Dead sessions remain listed, so you can’t trust an accurate count of active users. • Quota/usage tracking & billing If you bill per active session or monitor per-session throughput, those ghost sessions skew your metrics. • Resource leaks Although a forgotten QUIC session isn’t consuming CPU or bandwidth, it does consume a bit of memory (connection state). Over weeks, this can add up—especially on small servers.

⸻

  1. Suggested “workarounds” or mitigations

The bug author lists three possible workarounds: 1. Restarting the server periodically (e.g. a daily cron job). • Completely clears all sessions (live or dead). • Disadvantage: all real users get disconnected and must re-handshake, potentially causing a poor user experience. 2. Ping each user’s last‐known IP • If ping fails (host unreachable), assume the session is dead and drop it manually. • Disadvantage: Not reliable if the client’s IP changed, or if ping is blocked, or if the client is behind NAT/firewall. 3. Disconnect all users at fixed intervals (e.g. every hour) via a single command on the server. • That way, any dead sessions vanish. Active clients see the “session closed” and immediately reconnect. • Only active clients pay the 1–2 second handshake overhead, and any ghost sessions disappear. • This is probably the most practical: schedule a cron like:

/etc/cron.d/hysteria-cleanup

*/30 * * * * root /usr/local/bin/hysteria disconnect --all

(assuming your binary path is /usr/local/bin/hysteria). This forces every QUIC connection to drop every 30 minutes; live clients reconnect immediately, and lost sessions never linger.

⸻

  1. Clarifications about maxIdleTimeout • Is maxIdleTimeout “on by default,” or do you have to set it explicitly? Hysteria’s default maxIdleTimeout is normally 120 seconds if you don’t specify it. In your JSON, you should explicitly set it if you want a shorter or longer timeout. For example:

"quic": { "maxIdleTimeout": "60s", … }

If you omit maxIdleTimeout entirely, Hysteria still uses its built-in default (120 s). But best practice is to specify precisely the value you want.

•	Why do dead sessions still appear?

Even though maxIdleTimeout is enabled, certain network events—like very rapid IP switching, carrier handoffs, or unusual NAT behavior—can delay or prevent QUIC from seeing the final “no keep-alive” condition. In other words, QUIC doesn’t immediately detect “the client is gone” because the network stacks on both ends didn’t clean up the old UDP socket. That’s the “edge case” they’re chasing.

⸻

  1. What you can do next
    1. Verify your own maxIdleTimeout • Make sure your server JSON explicitly sets:

"quic": { "maxIdleTimeout": "60s", // or whatever value you prefer … }

•	If you rely on the default (120 s), know that stray sessions could linger for up to two minutes (plus any extra jitter).

2.	Implement a periodic “disconnect all” job
•	As mentioned above, adding a cron or systemd timer that runs:

/usr/local/bin/hysteria disconnect --all

every 15 or 30 minutes will purge any sessions that didn’t clean up automatically. Active clients only need ~1 second to reconnect, while ghost sessions vanish forever.

3.	Watch the server logs for unexpected patterns
•	Look for repeated WARN TCP error … timeout: no recent network activity messages without a matching INFO client disconnected … line. That usually signals that a session didn’t get torn down properly.
•	If you consistently see that under normal network switches (e.g. Wi-Fi ↔ LTE), you might lengthen maxIdleTimeout slightly (e.g. to 90 s) so that QUIC has a larger window to detect the missing keep-alives.
4.	Ask users to update to the latest Hysteria version
•	Sometimes these edge cases are fixed in newer releases. Check the official changelog to see if session‐cleanup logic was improved after v2.4.4.
5.	Consider a slightly more aggressive keep-alive
•	If you set speedTest: true plus a shorter udpIdleTimeout (say 30 seconds), the server sends a small “ping” packet every 30 seconds. That ensures that, if a client’s network quietly vanishes, the server will notice within 30 seconds and tear down promptly.

⸻

Bottom line • They’re describing a situation where “dead” sessions (clients who turned off their net mid-stream) sometimes never trigger the normal QUIC timeout. • maxIdleTimeout is indeed intended to handle this—but certain abrupt network changes can slip through before the server notices. • Workarounds include: 1. Explicitly setting (or shortening) maxIdleTimeout in your config. 2. Running a periodic hysteria disconnect --all so that any lingering ghosts get flushed. 3. Watching the logs for missing “client disconnected” lines to catch when a session failed to close properly.

Implementing one or more of those steps will keep your /onlinesession list accurate and prevent “dead” entries from piling up.

hayderi avatar May 31 '25 18:05 hayderi