Skip to content

Legacy thread watches leave large heap allocations until GC #544

Closed as not planned

Description

@nixuuu

Legacy/backpressured thread watches can leave hundreds of MiB of allocated payload objects waiting for GC. This exists on dev at 3d44197868b9c42eec317dcc3bf5903841e6e582, after the incremental sync stack, without any diagnostics collector/interceptor or diagnostics SQL adapter.

Related: #535, #542. Reported separately; no product fix is included in the diagnostics stack.

Reproduction and evidence

  • Bun 1.4.4, PostgreSQL 18.6. Seed 500 threads, one thread with 1,000 turns and 7,099 items using scripts/bench/seed-client.ts.
  • Start eight legacy WatchThread streams without sequence/incremental options. Receive their initial snapshots, then stop reading them. Run the existing slow-receiver workload for 15 seconds, cancel the streams, and continue the metadata-read workload for 90 seconds.
  • Repeat with a fresh gateway process and a different fresh database in the same private PG cluster/driver, deliberately retaining the old harness layout. Both gateways import dev product modules and dev's migrator/dependencies. A/B below mean first/second identical dev builds, not a diagnostics toggle.
  • The following phase retains the benchmark's 70-second lock timeline, but dev has no diagnostics persistence table: the lock is on an inert fixture-only table. There is no failing diagnostics write on dev. Thus this reproduces allocation carryover into the phase called write_failure, not evidence that a Postgres error retains parameters.
  • At 65 seconds into that phase, record native JSC heap/type counts, force two Bun.gc(true) calls separated by an event-loop turn, and record again. Generate the native JSC heap snapshot only after the explicit GC.
Gateway Heap after slow receiver Heap before GC at checkpoint Heap after GC RSS before / after GC
First 344.77 MiB 70.35 MiB 64.95 MiB 170.04 / 167.68 MiB
Second 232.74 MiB 237.16 MiB 65.94 MiB 420.23 / 204.91 MiB

The second gateway has 166.82 MiB more heap before GC, but only 0.99 MiB more after GC. It has 1,452,622 objects before GC versus 390,691 in the first gateway. In the second gateway, GC reduces Objects 337,845 → 46,312, arrays 87,524 → 20,395, strings 297,621 → 44,629 and ArrayBuffers 284 → 32. The large difference is collectible allocation carryover, not a demonstrated permanent leak.

Post-GC snapshot differences are small: ArrayBuffer self bytes +983,040 B with the same count (32), then JIT code +32,876 B. The largest retained string is 84,884 B; its shortest path is a module code block, not a failed query/error. Both snapshots contain the same ~16 MiB module-context ArrayBuffer and native PG parser buffers; the latter have paths through PgConnectionImpl → parser closure → store → ArrayBuffer. JSC does not classify weak edges, and these are self bytes/shortest root paths, not dominator retained sizes.

Consequence and scope

Endpoint RSS/heap sampled before GC can differ by ~170–250 MiB depending on when collection follows large watch snapshots, including when neither side has diagnostics. The first run can also be large immediately after the watch phase; order alone does not prove shared database state is the cause. The new acceptance harness isolates the driver VM, PG cluster/WAL, database and filesystem per member and adds A/A controls.

Investigate reducing legacy snapshot allocations and releasing cancelled/backpressured watch payloads promptly while preserving compatibility. Modern incremental watches should remain the preferred path. Do not add forced production GC as a workaround.

Raw snapshots and logs are private because they can contain synthetic test content and ephemeral database credentials. The measurements and constructor/path summaries above are sanitised; no SQL text, parameters or string contents are attached.

Fresh-state A/A controls

Two full-sequence A/A pairs on 07b25362c33346f5537cdbc324ba3561e33340e9 retain Stage 1 in all members and disable Stage 2 in every member. Each member has a new driver VM, PG cluster/WAL, database and gateway/worker state directory. All scope swap samples are zero, and both pairs pass the host gates.

Write-failure nominal B−A RSS/heap deltas are +3.09/+1.82 MiB and +258.61/+172.15 MiB. In pair 2, the large nominal B is the first member (BA order); its heap is 248.80 MiB versus 76.65 MiB in the second member. This rules out shared cluster/driver state or simply running second as a necessary cause. It also demonstrates this size of difference with no Stage 2 diagnostic difference at all. The portable GC-dependent allocation finding above remains applicable; the original order correlation should not be treated as a proven root cause.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions