Skip to content

Commit df60d86

Browse files
authored
Merge pull request #254 from mixpanel/jg-idle-fix
[SR] Fix idle timeout race condition
2 parents 41e7958 + 7b74d63 commit df60d86

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/recorder/session-recording.js

+4
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ SessionRecording.prototype.startRecording = function (shouldStopBatcher) {
196196
try {
197197
this._stopRecording = this._rrwebRecord({
198198
'emit': function (ev) {
199+
if (this.idleExpires < ev.timestamp) {
200+
this._onIdleTimeout();
201+
return;
202+
}
199203
if (isUserEvent(ev)) {
200204
if (this.batcher.stopped && new Date().getTime() - this.replayStartTime >= this.recordMinMs) {
201205
// start flushing again after user activity

tests/unit/session-recording.js

+23
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,29 @@ describe(`SessionRecording`, function() {
333333
expect(params2.get(`batch_start_time`)).to.equal(((NOW_MS + 1000) / 1000).toString());
334334
});
335335

336+
it(`runs the idle timeout handler when timing out`, function () {
337+
sessionRecording.startRecording();
338+
clock.tick(60 * 1000);
339+
340+
mockRrweb.emit(EventType.IncrementalSnapshot, IncrementalSource.MouseMove);
341+
clock.tick(29 * 60 * 1000);
342+
expect(onIdleTimeoutSpy.calledOnce).to.be.false;
343+
clock.tick(61 * 1000);
344+
345+
expect(onIdleTimeoutSpy.calledOnce).to.be.true;
346+
});
347+
348+
it(`will respect the idle timeout even when setTimeout fails (race condition where a browser tab might pause timers)`, function () {
349+
sessionRecording.startRecording();
350+
351+
const rightBeforeTimeout = 30 * 60 * 1000 - 1;
352+
clock.tick(rightBeforeTimeout);
353+
354+
expect(onIdleTimeoutSpy.calledOnce).to.be.false;
355+
mockRrweb.emit(EventType.IncrementalSnapshot, IncrementalSource.MouseMove, NOW_MS + rightBeforeTimeout + 2);
356+
expect(onIdleTimeoutSpy.calledOnce).to.be.true;
357+
});
358+
336359
it(`respects tracking opt-out check`, async function() {
337360
sessionRecording.startRecording();
338361
expect(sessionRecording.isRrwebStopped()).to.be.false;

0 commit comments

Comments
 (0)