Flush pending events on AppDomain.ProcessExit

Open
#385 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@hpouillot is already working on this.

Since Nov 13, 2026.

  • #387 by @hpouillot — open

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp

Research direction

Start by reading the client disposal and flush paths, the terminating-exception flush added in issue #356, and the existing PostHogOptions definitions. Decide how shutdown handling should resolve the open questions about configuration, disposal, and timeout semantics, then check that process exit waits only within the configured bound and does not race with DisposeAsync; done means pending events get a best-effort flush and the handler is removed on disposal.

Written by the indexing model from the issue text.

Description

enhancement feature/error-tracking

Problem

An application that never disposes the client loses the events still queued when the process exits normally. Today the only safety nets are the periodic flush and, with CaptureUnhandledExceptions, the flush after a terminating exception (#356).

Proposal

Register an AppDomain.CurrentDomain.ProcessExit handler that performs a bounded flush or disposal of the client.

  • Add PostHogOptions.ShutdownTimeout and block for at most that long. Default 30 seconds, matching the regular shutdown default in the Node and Rust SDKs and the .NET generic host. The option was left out of #356 because there it would only have bounded the flush after a terminating exception, which stays at a fixed 2 seconds: that flush blocks a process that is already crashing.
  • Run the flush off the exiting thread, as the terminating-exception flush does, so it cannot deadlock on a SynchronizationContext.
  • Do not dispose twice or concurrently with an explicit DisposeAsync.
  • Unregister the handler when the client is disposed.

Delivery stays best-effort: an abrupt termination or a runtime failure can prevent the handler from running or completing.

Open questions

  • Whether the hook is always on or behind an option.
  • Whether it flushes only or fully disposes the client.
  • Whether DisposeAsync should also honor ShutdownTimeout, so the option has one meaning everywhere.
  • Whether the terminating-exception flush should use min(ShutdownTimeout, 2 seconds), so a shorter configured budget also applies to a crash.

Context

Raised as a non-blocking follow-up in the review of #356.

Dominant language
C#
Stars
47
Forks
23
Avg merge
2d 12h
Merged PRs (30d)
30

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PostHog/posthog-dotnet

All issues in PostHog/posthog-dotnet

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.