fix(server): stop idle private-memory growth from immortal parent spans #5411
ClosedBenchance(GitHub profile, opens in a new tab)wants to merge 3 commits into
main fromrefs/pull/5411/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- @@ -6,11 +7,21 @@67 export class ServerActivation extends Context.Reference<Effect.Effect<void> | undefined>(78 "t3/serverActivation",89 { defaultValue: () => undefined },910 ) {}101112+// Clear inherited ParentSpan so long-lived forks don't pin short-lived startup spans (#5410).13+export const withoutAmbientParentSpan = <A, E, R>(14+ effect: Effect.Effect<A, E, R>,15+): Effect.Effect<A, E, R> =>16+ Effect.updateContext(17+ effect,18+ (context) => Context.omit(Tracer.ParentSpan)(context) as Context.Context<NoInfer<R>>,19+ );20+1121 /** Forks a long-running root before commit and proves it is parked at the activation boundary. */1222 export const forkParked = <A, E, R>(1323 effect: Effect.Effect<A, E, R>,1424 ): Effect.Effect<void, never, Scope.Scope | R> =>1525 Effect.gen(function* () {1626 const activation = yield* ServerActivation;27+ const detached = withoutAmbientParentSpan(effect);
- @@ -23,0 +31,21 @@31+ traceId: "00000000000000000000000000000001",32+ spanId: "0000000000000001",33+ sampled: true,34+ });35+36+ const sawParent = yield* Effect.serviceOption(Tracer.ParentSpan).pipe(37+ Effect.map(Option.isSome),38+ withoutAmbientParentSpan,39+ Effect.provideService(Tracer.ParentSpan, ambient),40+ );41+42+ expect(sawParent).toBe(false);43+44+ const stillHasParent = yield* Effect.serviceOption(Tracer.ParentSpan).pipe(45+ Effect.map(Option.isSome),46+ Effect.provideService(Tracer.ParentSpan, ambient),47+ );48+ expect(stillHasParent).toBe(true);49+50+ const stripped = Context.omit(Tracer.ParentSpan)(Context.make(Tracer.ParentSpan, ambient));51+ expect(Context.getOption(stripped, Tracer.ParentSpan)._tag).toBe("None");@@ -309,6 +310,14 @@309310 );310311311312 // Single layer-scoped polling fiber. Ticks are no-ops when no client is312- // currently retained, so the cost is one Ref.get every POLL_INTERVAL.313- yield* Effect.forkScoped(pollTick().pipe(Effect.repeat(Schedule.spaced(POLL_INTERVAL))));314-313+ // currently retained. Run detached from any ambient ParentSpan (e.g.314+ // PortDiscovery.make) and without tracing idle no-ops — otherwise every315+ // 3s tick parents under an immortal span and grows server private memory.316+ yield* Effect.forkScoped(317+ withoutAmbientParentSpan(318+ pollTick().pipe(319+ Effect.repeat(Schedule.spaced(POLL_INTERVAL)),320+ Effect.withTracerEnabled(false),321+ ),322+ ),323+ );
Benchance(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
35947756-e453-402a-83ae-5f16291f2e75You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share