cmd/tsconnect: close the SSH session an unload event instead of beforeunload

The window may not end up getting unloaded (if other beforeunload
handlers prevent the event), thus we should only close the SSH session
if it's truly getting unloaded.

Updates tailscale/corp#7304

Signed-off-by: Mihai Parparita <mihai@tailscale.com>
pull/5866/head
Mihai Parparita 2022-10-06 12:27:50 -07:00 committed by Mihai Parparita
parent 84e8f25c21
commit 92ad56ddcb
1 changed files with 5 additions and 5 deletions

View File

@ -42,7 +42,7 @@ export function runSSHSession(
term.focus()
let resizeObserver: ResizeObserver | undefined
let handleBeforeUnload: ((e: BeforeUnloadEvent) => void) | undefined
let handleUnload: ((e: Event) => void) | undefined
const sshSession = ipn.ssh(def.hostname, def.username, {
writeFn(input) {
@ -60,8 +60,8 @@ export function runSSHSession(
onDone() {
resizeObserver?.disconnect()
term.dispose()
if (handleBeforeUnload) {
parentWindow.removeEventListener("beforeunload", handleBeforeUnload)
if (handleUnload) {
parentWindow.removeEventListener("unload", handleUnload)
}
onDone()
},
@ -75,6 +75,6 @@ export function runSSHSession(
// Close the session if the user closes the window without an explicit
// exit.
handleBeforeUnload = () => sshSession.close()
parentWindow.addEventListener("beforeunload", handleBeforeUnload)
handleUnload = () => sshSession.close()
parentWindow.addEventListener("unload", handleUnload)
}