fix(server): failed telemetry batches stop re-sending every second #14986
fix/ws-reconnect-backoff fromrefs/pull/14986/head·merged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubOn Oct 2, 3.2M of 12.8M PostHog events (25%) were exact copies. 4 Windows servers on 0.0.44 in web mode sent 3.22M of them: each one sent the same first 20 events after boot about once a second, all day. This PR stops that loop. It also fixes one problem in #14897, which this PR is based on.
1. Analytics re-send loop (server)#
Cause. flush runs every second. When sendBatch failed, it put the batch back at the front of the buffer, and the next tick sent it again with no limit. Events had no uuid, so PostHog stored each copy as a new event. PostHog stores every copy, so the POST reaches PostHog while the server sees a failure.
Fix.
recordgives each event a UUIDv7, and the batch sends it. A retry carries the same uuid.- After a failed send, the background flush waits with jittered exponential backoff: 2s, doubling, capped at 5 minutes. The backoff is reset only by a successful send.
- After 5 failed sends, the batch is dropped with one warning that includes the cause. Before, every failure logged an error once a second.
- Sends time out after 10s. A lock stops the shutdown flush from sending the same batch as the background flush.
- If UUID generation fails, the event is skipped.
recordnever fails its caller.
Repro. I used a fake HTTP client that reads the batch and then fails, and one batch of 20 events. These are the POSTs per minute over 10 minutes:
| min 1 | min 2-10 | |
|---|---|---|
| Before | 60 | 60 each |
| After | 5 | 0 |
Not reproduced: why the POST fails on these machines. v0.0.43..v0.0.44 has only 3 commits, and none of them touch HTTP. v0.0.43 and v0.0.44 both shipped on Sep 29. So I also checked v0.0.42..v0.0.44:
- Effect went from rc.112 to rc.115. Its
FetchHttpClientchanges only howduplexis picked.HttpBody.textnow encodes lazily withBuffer. - Nothing in that range sets a global fetch, dispatcher, proxy agent, TLS option, or HttpClient wrapper.
- The re-queue loop dates from June (#3189). Only the failure on these 4 machines is new.
I could not test on Windows. The drop warning now logs the failure cause, so the next affected user's server log shows it. The backoff and the 5-try limit fix the cost in both cases. A dropped batch means 5 copies at most, and PostHog can dedupe them by uuid.
The two fix(server) commits have no dependency on #14897. They cherry-pick cleanly onto main.
2. Slow mobile resume (client-runtime)#
#14897 changed the long mobile resume (application-active-reconnect, 10s or more in the background) to probe the session with a 3s timeout first. iOS often kills a suspended socket without a close event, so a dead socket showed up to 3s more of "Resuming".
- This wakeup now replaces the session at once and restarts the retry ladder, as it did before #14897. Short resumes, explicit retries and offline reports still probe.
- The replacement attempt runs even while the network reports offline. Under #14897 a session can stay connected after an offline report that a probe proved wrong. Without this, the replaced session would leave the client offline until an online report.
shouldResubscribeAfterWakeupis back, so the shell and thread state do not resubscribe a second time on a long resume. The new session subscribes on its own.- This does not conflict with #14098. That PR changes the same wakeup while a connection attempt is in flight, and this PR changes it for a connected session.
Stale retry reset: no change needed. #14897's second commit already clears resetRetryState when a probe takes RetryRequested (takeSignal). Its test "keeps the backoff ladder after an explicit retry finds a healthy session" covers this.
3. Server that needs a newer client: already handled#
No code. #11974 (in 0.0.43 and later) does this before the socket opens:
- The resolver fetches the server descriptor on every attempt. The descriptor reports
orchestrationProtocolVersion. - On a mismatch, the client fails with
ConnectionBlockedError("unsupported"). The supervisor goes toblockedand stops retrying, and the registry disables the environment and shows the server's message. - So a 0.0.43 to 0.0.45 client (protocol 1) that connects to a server with #2829 (protocol 2) is blocked at discovery. It never reaches the 426. Only clients from before #11974 reach the 426, and client code cannot change those.
- An HTTP check after a failed upgrade would only cover a stale descriptor. That is why I did not add it.
Tests#
AnalyticsService.test.ts: a failing batch is sent 5 times in 10 minutes and then dropped, every retry carries the same 20 uuids, andretryDelayMsstays inside its bounds.supervisor.test.ts: the three long-resume tests from before #14897 are back, and a new test covers a long resume after a wrong offline report. All four fail without the supervisor change. The test "closes during a resume probe" now uses the short-resume wakeup.
Reviewed with sol-loop: 4 rounds with gpt-6.1-sol.
Created with Claude Opus 5.5 in Claude Code.
🤖 Generated with Claude Code
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
juliusmarminge added labelsmacroscope-review
Approvability#
Verdict: Not approved
Macroscope's review found this PR not approvable — This PR changes default production behavior in both telemetry delivery and mobile connection/session recovery, including retry limits, timeouts, session replacement, and subscription handling. The changes are tested but span multiple existing runtime paths, so their user and operational impact merits human review.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
ℹ️ No successful
mainbaseline artifact is available yet. This run establishes the initial measurement.Provider Metric Main baseline This PR Impact PR ceiling Codex Total thread wire — 4.9 KiB — 6.8 KiB ✅ Codex Thread snapshot wire — 3.7 KiB — 4.9 KiB ✅ Codex Live turn WebSocket wire — 1.2 KiB — 2.0 KiB ✅ Codex Live turn WebSocket decoded — 20.4 KiB — 29.3 KiB ✅ Codex Live turn messages — 2 — 8 ✅ Claude Total thread wire — 4.9 KiB — 6.8 KiB ✅ Claude Thread snapshot wire — 3.7 KiB — 4.9 KiB ✅ Claude Live turn WebSocket wire — 1.2 KiB — 2.0 KiB ✅ Claude Live turn WebSocket decoded — 20.8 KiB — 29.3 KiB ✅ Claude Live turn messages — 2 — 8 ✅ Baseline: unavailable · PR result:
9c70cb1· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
- Codex decoded thread snapshot: 106.1 KiB
- Claude decoded thread snapshot: 106.4 KiB
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed