diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2023-03-06 19:06:28 +0100 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2023-03-10 10:29:51 +0100 |
| commit | b225bab068ae87d0a9209923019bb4d861b4617b (patch) | |
| tree | ba80861f3451c6b842940c6e751f4c472fe0768e | |
| parent | 8be326a45b53dae1e16bd62a57ac257ed1e9cf93 (diff) | |
| download | mullvadvpn-b225bab068ae87d0a9209923019bb4d861b4617b.tar.xz mullvadvpn-b225bab068ae87d0a9209923019bb4d861b4617b.zip | |
Disconnect daemon during suspend
| -rw-r--r-- | gui/src/main/daemon-rpc.ts | 13 | ||||
| -rw-r--r-- | gui/src/main/index.ts | 25 |
2 files changed, 37 insertions, 1 deletions
diff --git a/gui/src/main/daemon-rpc.ts b/gui/src/main/daemon-rpc.ts index 5d24870908..966846fdf6 100644 --- a/gui/src/main/daemon-rpc.ts +++ b/gui/src/main/daemon-rpc.ts @@ -137,6 +137,7 @@ type CallFunctionArgument<T, R> = export class DaemonRpc { private client: ManagementServiceClient; private isConnectedValue = false; + private isClosed = false; private connectionObservers: ConnectionObserver[] = []; private nextSubscriptionId = 0; private subscriptions: Map<number, grpc.ClientReadableStream<grpcTypes.DaemonEvent>> = new Map(); @@ -154,6 +155,17 @@ export class DaemonRpc { return this.isConnectedValue; } + public reopen() { + if (this.isClosed) { + this.isClosed = false; + this.client = new ManagementServiceClient( + DAEMON_RPC_PATH, + grpc.credentials.createInsecure(), + this.channelOptions(), + ); + } + } + public connect(): Promise<void> { return new Promise((resolve, reject) => { this.client.waitForReady(this.deadlineFromNow(), (error) => { @@ -179,6 +191,7 @@ export class DaemonRpc { this.removeSubscription(subscriptionId); } + this.isClosed = true; this.client.close(); if (this.reconnectionTimeout) { clearTimeout(this.reconnectionTimeout); diff --git a/gui/src/main/index.ts b/gui/src/main/index.ts index 7181f863bf..36cdcfe06c 100644 --- a/gui/src/main/index.ts +++ b/gui/src/main/index.ts @@ -1,5 +1,5 @@ import { exec, execFile } from 'child_process'; -import { app, nativeTheme, session, shell, systemPreferences } from 'electron'; +import { app, nativeTheme, powerMonitor, session, shell, systemPreferences } from 'electron'; import fs from 'fs'; import * as path from 'path'; import util from 'util'; @@ -181,6 +181,9 @@ class ApplicationMain log.info('quit received'); this.onQuit(); }); + + powerMonitor.on('suspend', this.onSuspend); + powerMonitor.on('resume', this.onResume); } public async performPostUpgradeCheck(): Promise<void> { @@ -442,6 +445,26 @@ class ApplicationMain ); }; + private onSuspend = () => { + log.info('Suspend event received, disconnecting from daemon'); + if (this.daemonEventListener) { + this.daemonRpc.unsubscribeDaemonEventListener(this.daemonEventListener); + } + + const wasConnected = this.daemonRpc.isConnected; + this.daemonRpc.disconnect(); + this.onDaemonDisconnected(wasConnected); + }; + + private onResume = () => { + log.info('Resume event received, connecting to daemon'); + this.daemonRpc.reopen(); + this.daemonRpc.addConnectionObserver( + new ConnectionObserver(this.onDaemonConnected, this.onDaemonDisconnected), + ); + this.connectToDaemon(); + }; + private onDaemonConnected = async () => { const firstDaemonConnection = this.beforeFirstDaemonConnection; this.beforeFirstDaemonConnection = false; |
