Skip to content

Commit a0512db

Browse files
committed
shell: Look at the wall clock to measure session idle time
Counting timer ticks works only when the ticks come on time. Usually they do, but browsers might decide to not run our JavaScript for whatever reason, especially if the tab is actually idle or minimized or otherwise hidden. By looking at the wall clock time, the session gets logged out immediately once the timer ticks start coming again (and it has indeed been idle long enough). See https://issues.redhat.com/browse/COCKPIT-1418
1 parent 0c10eca commit a0512db

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

pkg/shell/idle.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ export class IdleTimeoutState extends EventEmitter<IdleTimeoutStateEvents> {
2727

2828
#final_countdown_timer: number = -1;
2929
#session_timeout: number = 0;
30-
#current_idle_time: number = 0;
30+
#idle_start_time: number;
3131

3232
final_countdown: null | number = null;
3333

3434
constructor() {
3535
super();
3636

37+
this.#idle_start_time = Date.now();
38+
3739
cockpit.dbus(null, { bus: "internal" }).call("/config", "cockpit.Config", "GetUInt",
3840
["Session", "IdleTimeout", 0, 240, 0], {})
3941
.then(result => {
@@ -54,27 +56,24 @@ export class IdleTimeoutState extends EventEmitter<IdleTimeoutStateEvents> {
5456
}
5557

5658
#idleTick = () => {
57-
this.#current_idle_time += 5000;
58-
if (this.final_countdown === null &&
59-
this.#current_idle_time >= this.#session_timeout - this.#final_countdown_secs * 1000) {
59+
const idle_time = Date.now() - this.#idle_start_time;
60+
if (this.final_countdown === null && idle_time >= this.#session_timeout - this.#final_countdown_secs * 1000) {
6061
// It's the final countdown...
61-
this.final_countdown = this.#final_countdown_secs;
6262
this.#final_countdown_timer = window.setInterval(this.#finalCountdownTick, 1000);
63-
this.#update();
63+
this.#finalCountdownTick();
6464
}
6565
};
6666

6767
#finalCountdownTick = () => {
68-
cockpit.assert(this.final_countdown !== null);
69-
this.final_countdown -= 1;
68+
this.final_countdown = Math.floor((this.#idle_start_time + this.#session_timeout - Date.now()) / 1000);
7069
if (this.final_countdown <= 0)
7170
cockpit.logout(true, _("You have been logged out due to inactivity."));
7271
this.#update();
7372
};
7473

7574
#resetTimer = () => {
7675
if (this.final_countdown === null)
77-
this.#current_idle_time = 0;
76+
this.#idle_start_time = Date.now();
7877
};
7978

8079
setupIdleResetEventListeners(win: Window) {
@@ -92,8 +91,8 @@ export class IdleTimeoutState extends EventEmitter<IdleTimeoutStateEvents> {
9291
}
9392

9493
cancel_final_countdown() {
95-
this.#current_idle_time = 0;
9694
this.final_countdown = null;
95+
this.#resetTimer();
9796
window.clearInterval(this.#final_countdown_timer);
9897
this.#update();
9998
}

0 commit comments

Comments
 (0)