Skip to content

Commit 9a6c6a7

Browse files
authored
feat(core): Wrap cron withMonitor callback in withIsolationScope (#11797)
Multiple cron jobs can run at the same time and they may run on a server where other work is being performed. Wrapping the `withMonitor` callback in `withIsolationScope` ensures that scope and breadcrumbs for the cron tasks remain isolated.
1 parent eb35e54 commit 9a6c6a7

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

packages/core/src/exports.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
import { GLOBAL_OBJ, isThenable, logger, timestampInSeconds, uuid4 } from '@sentry/utils';
1818

1919
import { DEFAULT_ENVIRONMENT } from './constants';
20-
import { getClient, getCurrentScope, getIsolationScope } from './currentScopes';
20+
import { getClient, getCurrentScope, getIsolationScope, withIsolationScope } from './currentScopes';
2121
import { DEBUG_BUILD } from './debug-build';
2222
import { closeSession, makeSession, updateSession } from './session';
2323
import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent';
@@ -160,28 +160,30 @@ export function withMonitor<T>(
160160
captureCheckIn({ monitorSlug, status, checkInId, duration: timestampInSeconds() - now });
161161
}
162162

163-
let maybePromiseResult: T;
164-
try {
165-
maybePromiseResult = callback();
166-
} catch (e) {
167-
finishCheckIn('error');
168-
throw e;
169-
}
170-
171-
if (isThenable(maybePromiseResult)) {
172-
Promise.resolve(maybePromiseResult).then(
173-
() => {
174-
finishCheckIn('ok');
175-
},
176-
() => {
177-
finishCheckIn('error');
178-
},
179-
);
180-
} else {
181-
finishCheckIn('ok');
182-
}
183-
184-
return maybePromiseResult;
163+
return withIsolationScope(() => {
164+
let maybePromiseResult: T;
165+
try {
166+
maybePromiseResult = callback();
167+
} catch (e) {
168+
finishCheckIn('error');
169+
throw e;
170+
}
171+
172+
if (isThenable(maybePromiseResult)) {
173+
Promise.resolve(maybePromiseResult).then(
174+
() => {
175+
finishCheckIn('ok');
176+
},
177+
() => {
178+
finishCheckIn('error');
179+
},
180+
);
181+
} else {
182+
finishCheckIn('ok');
183+
}
184+
185+
return maybePromiseResult;
186+
});
185187
}
186188

187189
/**

0 commit comments

Comments
 (0)