Skip to content

Commit f66f372

Browse files
committed
Seal tracer-provider spans ended before the first end() call
1 parent b4b00de commit f66f372

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

packages/core/src/tracing/sentrySpan.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,12 @@ export class SentrySpan implements Span {
248248

249249
/** @inheritdoc */
250250
public end(endTimestamp?: SpanTimeInput): void {
251-
// If already ended, skip
251+
// If already ended, skip the end-of-span processing, but still seal a tracer-provider span. The
252+
// seal at the bottom of this method is skipped on this early return, and `_endTime` may have been
253+
// set before this first `end()` call (e.g. via the constructor's `endTimestamp`), which would
254+
// otherwise leave the span mutable after `end()`. End-of-span processing already ran in that case.
252255
if (this._endTime) {
256+
this._frozen = spanIsTracerProviderSpan(this);
253257
return;
254258
}
255259

packages/core/test/lib/tracing/sentrySpan.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,23 @@ describe('SentrySpan', () => {
193193
expect(json.start_timestamp).toBe(999);
194194
expect(json.links).toHaveLength(1);
195195
});
196+
197+
it('seals a tracer-provider span that ended via the constructor endTimestamp', () => {
198+
// `_endTime` is set in the constructor, so `end()` early-returns before reaching the seal at the
199+
// bottom of its body. The span must still be sealed once `end()` is invoked.
200+
const span = new SentrySpan({
201+
name: 'original',
202+
startTimestamp: 1,
203+
endTimestamp: 2,
204+
attributes: { key: 'before' },
205+
});
206+
markSpanAsTracerProviderSpan(span);
207+
208+
span.end();
209+
210+
span.setAttribute('key', 'after');
211+
expect(spanToJSON(span).data?.['key']).toBe('before');
212+
});
196213
});
197214

198215
describe('end', () => {

0 commit comments

Comments
 (0)