Skip to content

feat(mcp): make McpAnalytics.flush() send captured events - #1306

Open
lucasheriques wants to merge 6 commits into
mainfrom
lucas/mcp-analytics-flush-sends
Open

lucasheriques wants to merge 6 commits into
mainfrom
lucas/mcp-analytics-flush-sends

Conversation

@lucasheriques

@lucasheriques lucasheriques commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

McpAnalytics.flush(), on the handle instrument() returns, only waited for in-flight MCP captures. A host on a per-request runtime (serverless, edge) had to call posthog.flush() after it too, or the events were lost. A method called flush that sends nothing is easy to misuse.

It now waits for in-flight captures, then flushes the client, and it never raises: a failed client flush is logged. This is the contract agreed for posthog-js in PostHog/posthog-js#5842, so await analytics.flush() means the same thing in both SDKs.

Each client flush runs on its own short-lived thread, and overlapping flushes of one client share it, so at most one client flush runs per client. Client.flush() blocks for up to its timeout when ingest stalls. On the loop's default executor, concurrent flushes held up unrelated work such as DNS lookups. A shared pool hung forked children and made healthy flushes queue behind stalled ones.

Scope. This leaves out one case. flush() still waits only for captures on the event loop it runs on, as before. A server that handles tool calls on one loop and awaits flush() on another gets its client flushed before those captures are queued. Fixing that means changing the loop scoping in drain_pending, which is a separate change.

Blast radius. Existing code that calls await analytics.flush() and then posthog.flush() or shutdown() keeps working: the second call finds an empty queue. flush() now flushes the whole client, so events the host captured itself on it are sent too.

💚 How did you test it?

  • pytest posthog/test/mcp: 831 passed. The five new tests fail without the change: events are sent, a failed client flush is logged, the default executor stays free, a healthy flush doesn't wait behind stalled ones, and a forked child's flush completes.
  • Adversarial review against a real Client and a local capture server. Same-loop flush sends $mcp_initialize and $mcp_tool_call before it returns. 50 concurrent flushes don't deadlock. With 18 concurrent flushes against a host that never replies, an unrelated getaddrinfo dropped from 9.5s to 0.0s.
  • ruff format --check, ruff check, the mypy baseline, make public_api_check, and python -W error -c "import posthog" pass.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Written with Claude Code. Lucas chose "wait, then flush the client" while reviewing PostHog/posthog-js#5842. The alternative was wait-only, which is what Python's McpAnalytics.flush did. The pre-PR gate ran two adversarial reviewers (Opus and Sonnet). Both found the cross-loop gap above, which stays out of scope. Sonnet also found the executor starvation, which is fixed here. Later bot reviews flagged a shared pool's fork and backlog problems, cancellation and thread-start errors, and one thread per call. Flushes now run on their own threads, coalesced per client.

McpAnalytics.flush() only waited for in-flight captures, so a host on a
per-request runtime still had to call posthog.flush() itself or lose the
events. It now waits, then flushes the client in a worker thread, and logs
instead of raising when that flush fails. This matches the contract agreed
for posthog-js (PostHog/posthog-js#5842).

Tested: uv run pytest posthog/test/mcp (828 passed, 11 skipped); the two
new flush tests fail without the change. ruff format/check, mypy baseline,
public_api_check and `python -W error -c "import posthog"` pass.
A stalled ingest host holds each Client.flush() for up to its timeout. On
the loop's default executor, concurrent flushes filled it and delayed
unrelated work such as DNS lookups by ~9.5s. Flushes now use a dedicated
executor.

Tested: new test fails with asyncio.to_thread and passes now; a repro with
18 concurrent flushes against a host that never replies went from 9.5s to
0.0s for an unrelated getaddrinfo. pytest posthog/test/mcp: 829 passed.
@lucasheriques
lucasheriques requested a review from a team as a code owner November 25, 2026 15:12
@lucasheriques lucasheriques self-assigned this Nov 25, 2026
@veria-ai

veria-ai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Fixed/addressed: 1 · PR risk: 0/10

Reviews

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PostHog Review · Standard · reviewed f319f6a: 2 Should fix.

New pushes to this pull request won't start an automatic review. Comment @posthog review to review new changes.

See this review and its findings in PostHog Review. Comment @posthog review --deep for a Deep review. A Deep review takes longer and looks deeper than a Standard review.

Comment thread posthog/mcp/_sink.py Resolved
Comment thread posthog/mcp/_sink.py Resolved
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

SDK performance: posthog-python

Benchmarks completed. No changes flagged for review.

Flags require at least a 5% change in the median and all six pairs agreeing on direction. This is a noise filter, not proof of a performance change or a CI gate.

View all results
Test Measurement Before PR With PR Change Assessment
Capture and deliver events (32-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Queue an event (32-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (32-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Refresh 1,000 flag definitions
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocated memory per operation 133.67 KiB/op 133.67 KiB/op <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocations per operation 1,345.52 count/op 1,345.52 count/op <0.1% more Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Time per operation 77.47 µs/op 77.54 µs/op <0.1% more time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Idle process memory (RSS) 37.62 MiB 37.66 MiB 0.1% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 40.07 MiB 40.11 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 40.07 MiB 40.11 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process CPU time per operation 80.45 µs/op 81.12 µs/op 0.8% more time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 40.74 MiB 40.74 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocated memory per operation 185.75 KiB/op 185.75 KiB/op <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocations per operation 1,337.51 count/op 1,337.51 count/op <0.1% fewer Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Time per operation 101.64 µs/op 101.68 µs/op <0.1% more time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Idle process memory (RSS) 41.28 MiB 41.25 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 47.06 MiB 47.03 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 47.06 MiB 47.03 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process CPU time per operation 98.85 µs/op 99.25 µs/op 0.4% more time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 51.51 MiB 51.39 MiB 0.2% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocated memory per operation 138.73 KiB/op 138.73 KiB/op <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocations per operation 1,390.31 count/op 1,390.28 count/op <0.1% fewer Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Time per operation 88.54 µs/op 88.67 µs/op 0.1% more time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Idle process memory (RSS) 37.55 MiB 37.55 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 37.72 MiB 37.71 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 37.72 MiB 37.71 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process CPU time per operation 90.46 µs/op 90.7 µs/op 0.3% more time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 40.74 MiB 40.74 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocated memory per operation 190.9 KiB/op 190.9 KiB/op <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocations per operation 1,382.2 count/op 1,382.19 count/op <0.1% fewer Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Time per operation 106.52 µs/op 105.43 µs/op 1% less time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Idle process memory (RSS) 41.07 MiB 41.1 MiB <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 41.53 MiB 41.54 MiB <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 41.13 MiB 41.15 MiB <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process CPU time per operation 104.61 µs/op 99.03 µs/op 5.3% less time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 41.5 MiB 41.5 MiB <0.1% more memory Not flagged
Queue an event (32-byte payload)
Shard: capture
Caller-thread CPU time per operation 57.17 µs/op 57.31 µs/op 0.2% more time Not flagged
Queue an event (32-byte payload)
Shard: capture
Time per operation 57.17 µs/op 57.3 µs/op 0.2% more time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Caller-thread CPU time per operation 57.21 µs/op 57.43 µs/op 0.4% more time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Time per operation 57.2 µs/op 57.44 µs/op 0.4% more time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocated memory per operation 225.43 KiB/op 225.43 KiB/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocations per operation 2,279.42 count/op 2,279.42 count/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Time per operation 109.88 µs/op 109.04 µs/op 0.8% less time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Process CPU time per operation 109.88 µs/op 109.05 µs/op 0.8% less time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocated memory per operation 2.17 MiB/op 2.17 MiB/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocations per operation 22,413.69 count/op 22,413.69 count/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Time per operation 1.03 ms/op 1.03 ms/op 0.3% more time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Process CPU time per operation 1.03 ms/op 1.03 ms/op 0.3% more time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocated memory per operation 21.63 MiB/op 21.63 MiB/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocations per operation 223,655.44 count/op 223,655.44 count/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Time per operation 10.21 ms/op 10.21 ms/op <0.1% less time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Process CPU time per operation 10.21 ms/op 10.21 ms/op <0.1% less time Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Idle process memory (RSS) 43.82 MiB 43.87 MiB 0.1% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Observed peak process memory (RSS) 44.18 MiB 44.13 MiB 0.1% less memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Process memory after settling (RSS) 44.18 MiB 44.13 MiB 0.1% less memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Lifetime peak process memory (RSS) 43.94 MiB 43.87 MiB 0.1% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Idle process memory (RSS) 42.95 MiB 42.99 MiB 0.1% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Observed peak process memory (RSS) 42.96 MiB 43 MiB 0.1% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Process memory after settling (RSS) 42.96 MiB 43 MiB 0.1% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Lifetime peak process memory (RSS) 43.24 MiB 43.19 MiB 0.1% less memory Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocated memory per operation 15.74 KiB/op 15.74 KiB/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocations per operation 162.77 count/op 162.77 count/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Time per operation 10.99 µs/op 11 µs/op <0.1% more time Not flagged
Evaluate one boolean flag
Shard: flags-single
Process CPU time per operation 11 µs/op 11 µs/op <0.1% more time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocated memory per operation 24.6 KiB/op 24.6 KiB/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocations per operation 239.28 count/op 239.28 count/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Time per operation 15.58 µs/op 15.56 µs/op 0.1% less time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Process CPU time per operation 15.58 µs/op 15.56 µs/op 0.1% less time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocated memory per operation 39.81 KiB/op 39.81 KiB/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocations per operation 388.27 count/op 388.27 count/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Time per operation 22.1 µs/op 22.05 µs/op 0.2% less time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Process CPU time per operation 22.1 µs/op 22.05 µs/op 0.2% less time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocated memory per operation 60.59 KiB/op 60.59 KiB/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocations per operation 584.27 count/op 584.27 count/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Time per operation 31.76 µs/op 31.9 µs/op 0.4% more time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Process CPU time per operation 31.77 µs/op 31.9 µs/op 0.4% more time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocated memory per operation 59.22 KiB/op 59.22 KiB/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocations per operation 567.77 count/op 567.77 count/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Time per operation 29.92 µs/op 29.86 µs/op 0.2% less time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Process CPU time per operation 29.92 µs/op 29.86 µs/op 0.2% less time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocated memory per operation 19.04 KiB/op 19.04 KiB/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocations per operation 192.27 count/op 192.27 count/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Time per operation 12.5 µs/op 12.47 µs/op 0.3% less time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Process CPU time per operation 12.5 µs/op 12.47 µs/op 0.3% less time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocated memory per operation 20.66 KiB/op 20.66 KiB/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocations per operation 197.77 count/op 197.77 count/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Time per operation 13.02 µs/op 13.05 µs/op 0.2% more time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Process CPU time per operation 13.02 µs/op 13.05 µs/op 0.2% more time Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocated memory per operation 14.89 KiB/op 14.89 KiB/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocations per operation 152.77 count/op 152.77 count/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Time per operation 10.49 µs/op 10.31 µs/op 1.6% less time Not flagged
Evaluate a rollout flag
Shard: flags-single
Process CPU time per operation 10.49 µs/op 10.31 µs/op 1.7% less time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocated memory per operation 15.9 KiB/op 15.9 KiB/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocations per operation 166.77 count/op 166.77 count/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Time per operation 11.42 µs/op 11.48 µs/op 0.5% more time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Process CPU time per operation 11.42 µs/op 11.48 µs/op 0.5% more time Not flagged

Values are medians of paired measurements. Not flagged does not mean unchanged. Unavailable means a measurement could not be provided, not zero. Estimated allocation measurements retain their instrumentation uncertainty. RSS measures the whole process, not just SDK allocations; observed peaks are sampled checkpoints. Paired ranges in the diagnostics describe variability, not confidence intervals.

Each shard uses its own runner; compare Before PR against With PR within a row, not across shards.

Compared 71d0e2a4ec578df7eb2c973886fa861fa30f9c34 → 24c11845abd61987c95539095d813e98def4fd97.

Full report and diagnostics

102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in comparison.json, including per-runner environments, resource limits, measurement methods, samples and checkpoints.

@stamphog stamphog 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.

Not approved — this change needs a human reviewer.

Add the stamphog label to request another review once you have addressed this.

Both of @PostHog's substantive concerns in posthog/mcp/_sink.py remain unaddressed, and its current-head review therefore provides no concern-free assurance. Address those threads and request review from Manoel Aranda Neto or another teammate before re-requesting automated approval.

  • Author wrote 43% of the modified lines and has 9 merged PRs in these paths (familiarity MODERATE).
  • PostHog Review (Standard) reviewed the current head: 2 findings, posted on the PR.
  • posthog[bot] reviewed the current head.
  • @PostHog on posthog/mcp/_sink.py: The fork-safety concern remains unresolved: the module-level executor is retained without a child-process reset, so inherited worker bookkeeping can leave a child's flush waiting indefinitely after the parent used the pool. Recreate the executor after fork and add a regression test before re-requesting.
  • @PostHog on posthog/mcp/_sink.py: The scheduling-backlog concern remains unresolved: all clients share a finite executor, submissions have no scheduling deadline, and Client.flush() starts its timeout only after execution begins. Bound the scheduling wait and coalesce redundant per-client flushes before re-requesting; if this finding is incorrect, explain why in the review thread and resolve it.
Gate mechanics and policy version
Gate Result
prerequisites ✓ all clear
deny-list ✓ no deny categories matched
every_file_attested ✓ not a bot PR
size ✓ 28L, 2F substantive, 82L/5F incl. docs/generated/snapshots — within ceiling
tier ✓ T1-agent / T1b-small (82L, 5F, two-areas, feat)
stamphog 2.9.1 .stamphog/policy.yml @ f319f6a · reviewed head f319f6a

@stamphog stamphog Bot removed the stamphog Request stamphog review label Nov 25, 2026
PostHog Review flagged the module-level flush executor: it hangs a forked
child's first flush, and when ingest stalls, flushes for healthy clients
queue behind stalled ones with no deadline. Each flush now runs on its own
daemon thread, so no pool state outlives a fork and no flush waits for a
worker. The loop's default executor stays free.

Tested: two new tests (a healthy flush finishes while 64 flushes stall; a
forked child's flush completes) fail on the pool and pass now. pytest
posthog/test/mcp: 831 passed. The stalled-host repro still shows 0.0s for
an unrelated getaddrinfo; ruff, mypy baseline and import check pass.
@lucasheriques

Copy link
Copy Markdown
Contributor Author

@PostHog review

@posthog

posthog Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

@lucasheriques PostHog Review is reviewing this pull request.

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PostHog Review · Standard · reviewed 3fed233: 2 Should fix.

Comment thread posthog/mcp/_sink.py Resolved
Comment thread posthog/mcp/_sink.py Resolved
Comment thread posthog/mcp/_sink.py Resolved
…lure

PostHog Review found two holes in the per-flush thread. Cancelling flush()
cancelled the worker's future, so its set_result() raised in the thread. A
Thread.start() failure escaped flush(), which promises never to raise. The
future is now marked running before the await (a running future ignores
cancel()), and the thread start sits inside the guarded block.

Tested: the cancellation test and the thread-start case of the
parameterized never-raises test fail before and pass now. pytest
posthog/test/mcp: 833 passed; ruff, mypy baseline, import check pass.
@lucasheriques

Copy link
Copy Markdown
Contributor Author

@PostHog review

@posthog

posthog Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

@lucasheriques PostHog Review is reviewing this pull request.

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PostHog Review · Standard · reviewed 656a139: no findings.

@lucasheriques lucasheriques added the stamphog Request stamphog review label Nov 26, 2026
@stamphog stamphog Bot removed the stamphog Request stamphog review label Nov 26, 2026

@stamphog stamphog 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.

Not approved — this change needs a human reviewer.

Add the stamphog label to request another review once you have addressed this.

A current-head review supplies independent assurance for the risky changes, but @veria-ai's worker-exhaustion concern remains substantiated by the current code. Address that comment in posthog/mcp/_sink.py and request re-review before re-requesting approval; Manoel Aranda Neto is a suggested reviewer.

  • Author wrote 43% of the modified lines and has 9 merged PRs in these paths (familiarity MODERATE).
  • PostHog Review (Standard) reviewed the current head: no findings.
  • posthog[bot] reviewed the current head.
  • @veria-ai inline comment on posthog/mcp/_sink.py, reiterated by @veria-ai[bot] in discussion: unbounded flush worker creation remains unfixed. Lines 123–141 start a new native thread for every call without per-client coalescing or a concurrency bound; cancellation leaves the running client flush alive. Slow ingestion and per-request flush bursts can therefore exhaust thread or memory limits. Bound or coalesce overlapping flushes without using the shared executor, then request re-review; if the finding is incorrect, explain why in its thread and resolve it.
Gate mechanics and policy version
Gate Result
prerequisites ✓ all clear
deny-list ✓ no deny categories matched
every_file_attested ✓ not a bot PR
size ✓ 39L, 2F substantive, 166L/5F incl. docs/generated/snapshots — within ceiling
tier ✓ T1-agent / T1c-medium (166L, 5F, two-areas, feat)
stamphog 2.9.1 .stamphog/policy.yml @ 656a139 · reviewed head 656a139

veria-ai flagged that every flush() started its own thread, so a burst of
per-request flushes during an ingest stall started one blocked thread per
request. Overlapping flushes of the same client now share one client
flush: a caller waits for a flush that was already running (it may have
missed the caller's events), then joins or starts one that began after
its call. At most one client flush runs per client. The per-client state
is reset in a forked child.

Tested: test_overlapping_analytics_flushes_run_one_client_flush_at_a_time
(20 overlapping flushes on one stalled client) fails without coalescing
and passes now. pytest posthog/test/mcp: 834 passed; the stalled-host and
real-client repros still pass; ruff, mypy baseline, import check pass.
@lucasheriques

Copy link
Copy Markdown
Contributor Author

@PostHog review

@posthog

posthog Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

@lucasheriques PostHog Review is reviewing this pull request.

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PostHog Review · Standard · reviewed d98ec75: 1 Should fix, 1 Consider.

This is review round 3 on this pull request. When findings keep coming back in the same code, the approach is usually the problem. Before the next fix, check whether a different design would remove the whole class of findings.

Comment thread posthog/mcp/_sink.py Resolved
Comment thread posthog/mcp/_sink.py Resolved
PostHog Review found that a failed client flush stayed in the per-client
map, and its traceback kept the client alive. A caller that waited on an
earlier failed flush also left that failure unretrieved, so asyncio sent
it to the loop's exception handler instead of the MCP logger. The flush
thread now removes its own entry when it ends, and the waiting caller
suppresses the earlier failure, which the caller that started it logs.

Tested: the collection test fails without the cleanup; the loop-handler
test fails after the cleanup alone and passes with the suppression.
pytest posthog/test/mcp: 836 passed in three runs; ruff, mypy pass.
@lucasheriques

Copy link
Copy Markdown
Contributor Author

@PostHog review

@posthog

posthog Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

@lucasheriques PostHog Review is reviewing this pull request.

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PostHog Review · Standard · reviewed 24c1184: no findings.

@lucasheriques lucasheriques added the stamphog Request stamphog review label Nov 26, 2026

@stamphog stamphog 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.

Approved.

The public contract and telemetry delivery changes have independent assurance from a current-head agent review. The implementation matches the disclosed intent, with no outstanding substantive concerns or other refusal grounds found.

  • Author wrote 50% of the modified lines and has 9 merged PRs in these paths (familiarity MODERATE).
  • PostHog Review (Standard) reviewed the current head: no findings.
  • posthog[bot] reviewed the current head.
Gate mechanics and policy version
Gate Result
prerequisites ✓ all clear
deny-list ✓ no deny categories matched
every_file_attested ✓ not a bot PR
size ✓ 91L, 2F substantive, 288L/5F incl. docs/generated/snapshots — within ceiling
tier ✓ T1-agent / T1c-medium (288L, 5F, two-areas, feat)
stamphog 2.9.1 .stamphog/policy.yml @ 24c1184 · reviewed head 24c1184

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

stamphog Request stamphog review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant