Skip to content

Commit 9e658ff

Browse files
committed
Seal tracer-provider spans ended before the first end() call
1 parent a416506 commit 9e658ff

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
@@ -303,8 +303,12 @@ export class SentrySpan implements Span {
303303

304304
/** @inheritdoc */
305305
public end(endTimestamp?: SpanTimeInput): void {
306-
// If already ended, skip
306+
// If already ended, skip the end-of-span processing, but still seal a tracer-provider span. The
307+
// seal at the bottom of this method is skipped on this early return, and `_endTime` may have been
308+
// set before this first `end()` call (e.g. via the constructor's `endTimestamp`), which would
309+
// otherwise leave the span mutable after `end()`. End-of-span processing already ran in that case.
307310
if (this._endTime) {
311+
this._frozen = spanIsTracerProviderSpan(this);
308312
return;
309313
}
310314

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,23 @@ describe('SentrySpan', () => {
205205
expect(json.start_timestamp).toBe(999);
206206
expect(json.links).toHaveLength(1);
207207
});
208+
209+
it('seals a tracer-provider span that ended via the constructor endTimestamp', () => {
210+
// `_endTime` is set in the constructor, so `end()` early-returns before reaching the seal at the
211+
// bottom of its body. The span must still be sealed once `end()` is invoked.
212+
const span = new SentrySpan({
213+
name: 'original',
214+
startTimestamp: 1,
215+
endTimestamp: 2,
216+
attributes: { key: 'before' },
217+
});
218+
markSpanAsTracerProviderSpan(span);
219+
220+
span.end();
221+
222+
span.setAttribute('key', 'after');
223+
expect(spanToJSON(span).data?.['key']).toBe('before');
224+
});
208225
});
209226

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

0 commit comments

Comments
 (0)