Skip to content

fix(telemetry): close PostHog's queue files before deleting its storage - #875

Open
altaywtf wants to merge 5 commits into
mainfrom
fix/723-telemetry-open-handle
Open

altaywtf wants to merge 5 commits into
mainfrom
fix/723-telemetry-open-handle

Conversation

@altaywtf

@altaywtf altaywtf commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Change

Stopping analytics now deletes PostHog's storage on Windows too. Fixes #723, part of #858.

sequenceDiagram
    participant Q as PostHogQueueThread
    participant S as stop()
    Q->>Q: open queue/<id>.event, write
    S->>S: wait until each accepted event's write started (5 s cap)
    S->>Q: close open queue streams, refuse new ones
    S->>S: delete storage, close client, delete again
Loading
  • PostHog's close() doesn't wait for its queue thread; Windows can't delete an open file.
  • A pass-through PostHogEncryption tracks the streams.
  • stop() stays best-effort, so the next start() always runs.

Validation

  • :core:telemetry tests, detekt and lint on macOS
  • Windows (altay-pc): the test fails 2/2 on main, passes 2/2 here

Unverified: the 5 s cap is never hit in tests.

altaywtf and others added 2 commits November 20, 2026 17:57
PostHog writes and reads each queued event on its own queue thread, and
close() neither waits for that thread nor stops it. stop() could delete
the storage while the thread still held an event file open. Linux
unlinks an open file, but Windows refuses to delete it, so
io.put.analytics outlived an opt-out there (#723).

The SDK routes every queue file stream through the encryption hook, so a
pass-through PostHogEncryption now tracks open streams. stop() waits for
each event the SDK accepted to open its file, then closes every open
stream and refuses new ones before deleting. The SDK already treats a
failed stream as a dropped record and deletes the file itself.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T15:30:23.904425Z 3ae232e New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4125e3863f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +160 to +161
lock.withLock { open -= outputStream }
super.close()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep stream removal synchronized with the underlying close

On Windows, stopping while the queue thread closes a stream can still leave storage undeleted: this removes the stream from open and releases the lock before super.close() closes its file handle. If closeAll() runs in that interval, it excludes the still-open stream and returns, allowing both deletion attempts in stop() to fail before the queue thread resumes. The input-stream wrapper has the same race. Synchronize the underlying close and removal with closeAll() so an untracked open handle cannot survive its return.

Useful? React with 👍 / 👎.

altaywtf and others added 3 commits November 20, 2026 18:19
The gate runs stop() and start() in one guarded step, so a throwing stop
left analytics off for the new account. Failures stay with the gate's
policy: PostHog never breaks a flow. client.close() can no longer skip
the final delete.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Waiting for pending writes let the queue read and send events captured
before an opt-out. Reads now fail from the start of stop(), so those
events drop as before.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

core/telemetry: stoppingLeavesNothingOnTheDevice fails on Windows

1 participant