diff --git a/src/vs/workbench/contrib/remote/browser/remoteExplorer.ts b/src/vs/workbench/contrib/remote/browser/remoteExplorer.ts index db88121bccbdd..0ba9d54a78e57 100644 --- a/src/vs/workbench/contrib/remote/browser/remoteExplorer.ts +++ b/src/vs/workbench/contrib/remote/browser/remoteExplorer.ts @@ -488,7 +488,15 @@ class ProcAutomaticPortForwarding extends Disposable { ) { super(); this.notifier = new OnAutoForwardedAction(notificationService, remoteExplorerService, openerService, externalOpenerService, tunnelService, hostService, logService); - this._register(configurationService.onDidChangeConfiguration(async (e) => { + this.initialize(); + } + + private async initialize() { + if (!this.remoteExplorerService.tunnelModel.environmentTunnelsSet) { + await new Promise(resolve => this.remoteExplorerService.tunnelModel.onEnvironmentTunnelsSet(() => resolve())); + } + + this._register(this.configurationService.onDidChangeConfiguration(async (e) => { if (e.affectsConfiguration(PORT_AUTO_FORWARD_SETTING)) { await this.startStopCandidateListener(); } @@ -524,14 +532,13 @@ class ProcAutomaticPortForwarding extends Disposable { this.portsFeatures.dispose(); } - if (!this.remoteExplorerService.tunnelModel.environmentTunnelsSet) { - await new Promise(resolve => this.remoteExplorerService.tunnelModel.onEnvironmentTunnelsSet(() => resolve())); - } - // Capture list of starting candidates so we don't auto forward them later. await this.setInitialCandidates(); - this.candidateListener = this._register(this.remoteExplorerService.tunnelModel.onCandidatesChanged(this.handleCandidateUpdate, this)); + // Need to check the setting again, since it may have changed while we waited for the initial candidates to be set. + if (this.configurationService.getValue(PORT_AUTO_FORWARD_SETTING)) { + this.candidateListener = this._register(this.remoteExplorerService.tunnelModel.onCandidatesChanged(this.handleCandidateUpdate, this)); + } } private async setInitialCandidates() {