Skip to content

Commit 2d6612a

Browse files
fix(server): background work and scheduler ticks start their own traces
The server command ran inside a span that lasted the whole process, and long-lived work forked under it (scheduler ticks, parked startup fibers, fibers forked from captured layer contexts) inherited it, so their spans landed in one rootless trace that grew until the backend rejected it. - runServerCommand and runDefaultServerCommand no longer open a span. - forkParkedFiber and each Scheduler run drop the caller's parent span, so every tick or background run starts its own trace. Fixes #43501 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0233dec commit 2d6612a

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

‎apps/server/src/cli/server.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import { type CliServerFlags, resolveServerConfig, sharedServerCommandFlags } fr
1616

1717
const encodeCommand = Schema.encodeEffect(Schema.fromJsonString(Schema.String));
1818

19-
const runServerCommand = Effect.fn("server.runServerCommand")(function* (
19+
// Untraced: a span that lasts the whole process would parent everything the server does.
20+
const runServerCommand = Effect.fnUntraced(function* (
2021
flags: CliServerFlags,
2122
options?: {
2223
readonly startupPresentation?: ServerConfig.StartupPresentation;
@@ -44,9 +45,7 @@ const runServerCommand = Effect.fn("server.runServerCommand")(function* (
4445
return yield* stopOnLauncherLoss(run);
4546
});
4647

47-
export const runDefaultServerCommand = Effect.fn("server.runDefaultServerCommand")(function* (
48-
flags: CliServerFlags,
49-
) {
48+
export const runDefaultServerCommand = Effect.fnUntraced(function* (flags: CliServerFlags) {
5049
if (Option.isSome(flags.cwd)) {
5150
const cwd = flags.cwd.value.trim();
5251
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/scheduling/Scheduler.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as Ref from "effect/Ref";
66
import * as Scope from "effect/Scope";
77
import * as Semaphore from "effect/Semaphore";
88

9-
import { forkParked } from "../serverActivation.ts";
9+
import { forkParked, withoutParentSpan } from "../serverActivation.ts";
1010

1111
export class Scheduler extends Context.Service<
1212
Scheduler,
@@ -20,14 +20,15 @@ export class Scheduler extends Context.Service<
2020

2121
const make = Effect.gen(function* () {
2222
const sources = yield* Ref.make(new Map<symbol, Effect.Effect<void>>());
23-
const register: Scheduler["Service"]["register"] = Effect.fn("Scheduler.register")(function* <
24-
E,
25-
R,
26-
>(name: string, runDueWork: Effect.Effect<void, E, R>) {
23+
const register: Scheduler["Service"]["register"] = Effect.fnUntraced(function* <E, R>(
24+
name: string,
25+
runDueWork: Effect.Effect<void, E, R>,
26+
) {
2727
const scope = yield* Effect.scope;
2828
const context = yield* Effect.context<R>();
2929
const permit = yield* Semaphore.make(1);
3030
const run = runDueWork.pipe(
31+
withoutParentSpan,
3132
Effect.provideContext(context),
3233
Effect.catchCause((cause) =>
3334
Cause.hasInterruptsOnly(cause)

‎apps/server/src/serverActivation.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@ import * as Deferred from "effect/Deferred";
33
import * as Effect from "effect/Effect";
44
import type * as Fiber from "effect/Fiber";
55
import type * as Scope from "effect/Scope";
6+
import * as Tracer from "effect/Tracer";
67

78
export class ServerActivation extends Context.Reference<Effect.Effect<void> | undefined>(
89
"t3/serverActivation",
910
{ defaultValue: () => undefined },
1011
) {}
1112

12-
export const forkParkedFiber = Effect.fn("serverActivation.forkParkedFiber")(function* <A, E, R>(
13-
effect: Effect.Effect<A, E, R>,
13+
// Background work outlives its caller's span; parenting to it puts every run in one endless trace.
14+
export const withoutParentSpan = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
15+
effect.pipe(
16+
// ParentSpan is optional to every reader, so removing it leaves R satisfied.
17+
Effect.updateContext(
18+
(context: Context.Context<R>) =>
19+
Context.omit(Tracer.ParentSpan)(context) as Context.Context<R>,
20+
),
21+
);
22+
23+
export const forkParkedFiber = Effect.fnUntraced(function* <A, E, R>(
24+
input: Effect.Effect<A, E, R>,
1425
): Effect.fn.Return<Fiber.Fiber<A, E>, never, Scope.Scope | R> {
26+
const effect = withoutParentSpan(input);
1527
const activation = yield* ServerActivation;
1628
if (activation === undefined) {
1729
return yield* Effect.forkScoped(effect);

0 commit comments

Comments
 (0)