Skip to content

Commit ae9bf6b

Browse files
committed
fix(server): detach immortal parent spans from idle background work
Parked forkParked roots and the PortDiscovery poll loop inherited the ambient Effect ParentSpan, pinning short-lived startup spans for the process lifetime while idle pollTick/secret-get spans piled up under them (~25-30 MB/h Private, #5410). Re-root long-lived fibers under a fresh stateless external span via withDetachedSpan instead of omitting ParentSpan from the context: provideService keeps the Exclude<R, ParentSpan> return type honest, so effects that explicitly require a ParentSpan observe the fresh root instead of dying with Service-not-found. Idle poll ticks exit before Effect.fn so they create no span, and ServerSecretStore.get runs untraced.
1 parent b906ce2 commit ae9bf6b

5 files changed

Lines changed: 159 additions & 10 deletions

File tree

‎apps/server/src/auth/ServerSecretStore.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export const make = Effect.gen(function* () {
181181
}),
182182
),
183183
),
184-
Effect.withSpan("ServerSecretStore.get"),
184+
Effect.withTracerEnabled(false),
185185
);
186186

187187
const set: ServerSecretStore["Service"]["set"] = (name, value) => {

‎apps/server/src/preview/PortScanner.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as Layer from "effect/Layer";
1818
import * as PlatformError from "effect/PlatformError";
1919
import * as Scope from "effect/Scope";
2020
import * as TestClock from "effect/testing/TestClock";
21+
import * as Tracer from "effect/Tracer";
2122
import { expect } from "vite-plus/test";
2223
import { FetchHttpClient } from "effect/unstable/http";
2324

@@ -665,3 +666,40 @@ effectIt.effect("does not swallow process probe interruption", () =>
665666
}
666667
}),
667668
);
669+
670+
effectIt.effect("idle poll ticks create no spans under the ambient parent", () => {
671+
const spanNames: Array<string> = [];
672+
const tracer = Tracer.make({
673+
span: (options) => {
674+
const span = new Tracer.NativeSpan(options);
675+
const end = span.end.bind(span);
676+
span.end = (endTime, exit) => {
677+
end(endTime, exit);
678+
spanNames.push(span.name);
679+
};
680+
return span;
681+
},
682+
});
683+
// No retain call, so every tick takes the idle early-return path.
684+
const layer = makeProbeFailureLayer(processProbeFailure);
685+
686+
const idlePolls = Effect.gen(function* () {
687+
yield* PortScanner.PortDiscovery;
688+
yield* TestClock.adjust(Duration.seconds(30));
689+
});
690+
691+
// Nesting matters: the recording tracer must already be installed when the
692+
// ambient span is created, and the layer (which forks the poll fiber) must
693+
// build inside that ambient span so a leaked ParentSpan would be observed.
694+
// Assertions run after the scope closes so the ambient span has ended.
695+
return Effect.gen(function* () {
696+
yield* Effect.scoped(
697+
Effect.withTracer(
698+
Effect.withSpan("PortScannerTest.idlePollTick")(Effect.provide(idlePolls, layer)),
699+
tracer,
700+
),
701+
);
702+
expect(spanNames).toContain("PortScannerTest.idlePollTick");
703+
expect(spanNames.filter((name) => name === "PortDiscovery.pollTick")).toHaveLength(0);
704+
});
705+
});

‎apps/server/src/preview/PortScanner.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import * as Semaphore from "effect/Semaphore";
3939
import { FetchHttpClient, HttpClient } from "effect/unstable/http";
4040

4141
import * as ProcessRunner from "../processRunner.ts";
42+
import { withDetachedSpan } from "../serverActivation.ts";
4243

4344
export class PortDiscovery extends Context.Service<
4445
PortDiscovery,
@@ -547,9 +548,8 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
547548
);
548549
};
549550

550-
const pollTick = Effect.fn("PortDiscovery.pollTick")(
551+
const pollTickActive = Effect.fn("PortDiscovery.pollTick")(
551552
function* () {
552-
if ((yield* Ref.get(stateRef)).retainCount <= 0) return;
553553
const configuredUrls = [
554554
...new Set(
555555
[...(yield* Ref.get(stateRef)).listeners.values()].flatMap(
@@ -578,9 +578,17 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
578578
),
579579
);
580580

581+
// Idle early-return stays outside Effect.fn so no-op ticks create no span; detach ParentSpan (#5410).
582+
const pollTick = Effect.gen(function* () {
583+
if ((yield* Ref.get(stateRef)).retainCount <= 0) return;
584+
yield* pollTickActive();
585+
});
586+
581587
// Single layer-scoped polling fiber. Ticks are no-ops when no client is
582588
// currently retained, so the cost is one Ref.get every POLL_INTERVAL.
583-
yield* Effect.forkScoped(pollTick().pipe(Effect.repeat(Schedule.spaced(POLL_INTERVAL))));
589+
yield* Effect.forkScoped(
590+
withDetachedSpan(pollTick.pipe(Effect.repeat(Schedule.spaced(POLL_INTERVAL)))),
591+
);
584592

585593
const acquireRetention = Effect.fn("PortDiscovery.retain")(function* () {
586594
const wasIdle = yield* Ref.modify(stateRef, (state) => [
@@ -590,7 +598,7 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
590598
if (wasIdle) {
591599
// Run an immediate scan + broadcast so the new retainer doesn't have
592600
// to wait up to POLL_INTERVAL for the first emission.
593-
yield* pollTick();
601+
yield* pollTick;
594602
}
595603
});
596604

@@ -659,6 +667,6 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
659667
registerTerminalProcesses,
660668
unregisterTerminal,
661669
});
662-
}).pipe(Effect.withSpan("PortDiscovery.make"));
670+
});
663671

664672
export const layer = Layer.effect(PortDiscovery, make);

‎apps/server/src/serverActivation.test.ts‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { expect, it } from "@effect/vitest";
22
import * as Deferred from "effect/Deferred";
33
import * as Effect from "effect/Effect";
4+
import * as Option from "effect/Option";
5+
import * as Tracer from "effect/Tracer";
46

5-
import { forkParked, ServerActivation } from "./serverActivation.ts";
7+
import { forkParked, ServerActivation, withDetachedSpan } from "./serverActivation.ts";
68

79
it.effect("proves a root is parked before returning and releases it with one gate", () =>
810
Effect.scoped(
@@ -21,3 +23,75 @@ it.effect("proves a root is parked before returning and releases it with one gat
2123
}),
2224
),
2325
);
26+
27+
it.effect("withDetachedSpan re-roots instead of inheriting the ambient ParentSpan", () =>
28+
Effect.gen(function* () {
29+
const ambient = Tracer.externalSpan({
30+
traceId: "00000000000000000000000000000001",
31+
spanId: "0000000000000001",
32+
sampled: true,
33+
});
34+
35+
const detachedSpanId = yield* Effect.serviceOption(Tracer.ParentSpan).pipe(
36+
Effect.map(Option.map((span) => span.spanId)),
37+
withDetachedSpan,
38+
Effect.provideService(Tracer.ParentSpan, ambient),
39+
);
40+
expect(Option.isSome(detachedSpanId)).toBe(true);
41+
if (Option.isSome(detachedSpanId)) {
42+
expect(detachedSpanId.value).not.toBe("0000000000000001");
43+
}
44+
45+
const stillHasParent = yield* Effect.serviceOption(Tracer.ParentSpan).pipe(
46+
Effect.map(Option.map((span) => span.spanId)),
47+
Effect.provideService(Tracer.ParentSpan, ambient),
48+
);
49+
expect(Option.isSome(stillHasParent)).toBe(true);
50+
if (Option.isSome(stillHasParent)) {
51+
expect(stillHasParent.value).toBe("0000000000000001");
52+
}
53+
}),
54+
);
55+
56+
it.effect("withDetachedSpan keeps ParentSpan-requiring effects working under the fresh root", () =>
57+
Effect.gen(function* () {
58+
const span = yield* Effect.service(Tracer.ParentSpan).pipe(
59+
withDetachedSpan,
60+
Effect.provideService(
61+
Tracer.ParentSpan,
62+
Tracer.externalSpan({
63+
traceId: "00000000000000000000000000000001",
64+
spanId: "0000000000000001",
65+
sampled: true,
66+
}),
67+
),
68+
);
69+
expect(span.spanId).not.toBe("0000000000000001");
70+
}),
71+
);
72+
73+
it.effect("forkParked roots do not inherit the ambient ParentSpan", () =>
74+
Effect.scoped(
75+
Effect.gen(function* () {
76+
const ambient = Tracer.externalSpan({
77+
traceId: "00000000000000000000000000000002",
78+
spanId: "0000000000000002",
79+
sampled: true,
80+
});
81+
const observed = yield* Deferred.make<Option.Option<string>>();
82+
83+
yield* forkParked(
84+
Effect.serviceOption(Tracer.ParentSpan).pipe(
85+
Effect.map(Option.map((span) => span.spanId)),
86+
Effect.flatMap((spanId) => Deferred.succeed(observed, spanId)),
87+
),
88+
).pipe(Effect.provideService(Tracer.ParentSpan, ambient));
89+
90+
const spanId = yield* Deferred.await(observed);
91+
expect(Option.isSome(spanId)).toBe(true);
92+
if (Option.isSome(spanId)) {
93+
expect(spanId.value).not.toBe("0000000000000002");
94+
}
95+
}),
96+
),
97+
);

‎apps/server/src/serverActivation.ts‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,54 @@ import * as Context from "effect/Context";
22
import * as Deferred from "effect/Deferred";
33
import * as Effect from "effect/Effect";
44
import type * as Scope from "effect/Scope";
5+
import * as Tracer from "effect/Tracer";
6+
import * as NodeCrypto from "node:crypto";
57

68
export class ServerActivation extends Context.Reference<Effect.Effect<void> | undefined>(
79
"t3/serverActivation",
810
{ defaultValue: () => undefined },
911
) {}
1012

13+
// Detached background work runs under a fresh stateless root instead of the
14+
// ambient ParentSpan, so long-lived fibers never pin the short-lived span that
15+
// happened to be ambient at fork time (#5410). An external span carries only
16+
// trace/span ids and never accumulates children, so unlike a live startup span
17+
// it cannot pin unbounded tracing state. Re-rooting (instead of omitting
18+
// ParentSpan from the context) keeps every effect working: effects that
19+
// explicitly require a ParentSpan observe the fresh root instead of dying with
20+
// Service-not-found, which makes the Exclude in the return type honest.
21+
export const withDetachedSpan = <A, E, R>(
22+
effect: Effect.Effect<A, E, R>,
23+
): Effect.Effect<A, E, Exclude<R, Tracer.ParentSpan>> =>
24+
Effect.flatMap(
25+
Effect.sync(() => {
26+
const traceId = NodeCrypto.randomUUID().replaceAll("-", "");
27+
return Tracer.externalSpan({
28+
traceId,
29+
spanId: traceId.slice(0, 16),
30+
sampled: true,
31+
});
32+
}),
33+
(root) => Effect.provideService(effect, Tracer.ParentSpan, root),
34+
);
35+
1136
/** Forks a long-running root before commit and proves it is parked at the activation boundary. */
1237
export const forkParked = <A, E, R>(
1338
effect: Effect.Effect<A, E, R>,
14-
): Effect.Effect<void, never, Scope.Scope | R> =>
39+
): Effect.Effect<void, never, Scope.Scope | Exclude<R, Tracer.ParentSpan>> =>
1540
Effect.gen(function* () {
1641
const activation = yield* ServerActivation;
42+
const detached = withDetachedSpan(effect);
1743
if (activation === undefined) {
18-
yield* Effect.forkScoped(effect);
44+
yield* Effect.forkScoped(detached);
1945
return;
2046
}
2147
const parked = yield* Deferred.make<void>();
2248
yield* Effect.forkScoped(
23-
Deferred.succeed(parked, undefined).pipe(Effect.andThen(activation), Effect.andThen(effect)),
49+
Deferred.succeed(parked, undefined).pipe(
50+
Effect.andThen(activation),
51+
Effect.andThen(detached),
52+
),
2453
);
2554
yield* Deferred.await(parked);
2655
});

0 commit comments

Comments
 (0)