Skip to content

Commit a13d17d

Browse files
committed
fix(diagnostics): fire asyncStart synchronously, asyncEnd in finally
maybeTraceMixed was firing asyncStart and asyncEnd back-to-back inside the .then callback. That left no meaningful window between the two events: for APM subscribers, 'async work begins' and 'async work completes' fired in the same microtask, making the pair useless for measuring the async duration. Correct shape: asyncStart publishes synchronously as soon as we know the operation is in-flight asynchronously (right after end), and asyncEnd publishes in a .finally after settlement. Both the .then and .finally are attached inside start.runStores, so AsyncLocalStorage context is preserved end-to-end via async_hooks. Event order now: sync path: [start, end] async path: [start, end, asyncStart, ..., asyncEnd] async error: [start, end, asyncStart, error, asyncEnd] Mirrors the same fix in the FakeTracingChannel helper.
1 parent 4855e1e commit a13d17d

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed

src/__testUtils__/fakeDiagnosticsChannel.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,27 +122,22 @@ export class FakeTracingChannel implements MinimalTracingChannel {
122122
throw err;
123123
}
124124
this.end.publish(ctx);
125-
return promise.then(
126-
(result) => {
127-
(ctx as { result: unknown }).result = result;
128-
this.asyncStart.publish(ctx);
129-
try {
125+
this.asyncStart.publish(ctx);
126+
return promise
127+
.then(
128+
(result) => {
129+
(ctx as { result: unknown }).result = result;
130130
return result;
131-
} finally {
132-
this.asyncEnd.publish(ctx);
133-
}
134-
},
135-
(err: unknown) => {
136-
(ctx as { error: unknown }).error = err;
137-
this.error.publish(ctx);
138-
this.asyncStart.publish(ctx);
139-
try {
131+
},
132+
(err: unknown) => {
133+
(ctx as { error: unknown }).error = err;
134+
this.error.publish(ctx);
140135
throw err;
141-
} finally {
142-
this.asyncEnd.publish(ctx);
143-
}
144-
},
145-
);
136+
},
137+
)
138+
.finally(() => {
139+
this.asyncEnd.publish(ctx);
140+
});
146141
});
147142
}
148143
}

src/diagnostics.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -235,26 +235,22 @@ export function maybeTraceMixed<T>(
235235
}
236236

237237
channel.end.publish(ctx);
238-
return result.then(
239-
(value) => {
240-
ctx.result = value;
241-
channel.asyncStart.publish(ctx);
242-
try {
238+
channel.asyncStart.publish(ctx);
239+
240+
return result
241+
.then(
242+
(value) => {
243+
ctx.result = value;
243244
return value;
244-
} finally {
245-
channel.asyncEnd.publish(ctx);
246-
}
247-
},
248-
(err: unknown) => {
249-
ctx.error = err;
250-
channel.error.publish(ctx);
251-
channel.asyncStart.publish(ctx);
252-
try {
245+
},
246+
(err: unknown) => {
247+
ctx.error = err;
248+
channel.error.publish(ctx);
253249
throw err;
254-
} finally {
255-
channel.asyncEnd.publish(ctx);
256-
}
257-
},
258-
);
250+
},
251+
)
252+
.finally(() => {
253+
channel.asyncEnd.publish(ctx);
254+
});
259255
});
260256
}

0 commit comments

Comments
 (0)