|
| 1 | +import * as vscode from "vscode"; |
| 2 | + |
| 3 | +import { vscodeProposed } from "../vscode/proposed"; |
| 4 | + |
| 5 | +import { WindowLogReader, type WindowLogEvent } from "./windowLog"; |
| 6 | + |
| 7 | +import type { SshTelemetry } from "../instrumentation/ssh"; |
| 8 | +import type { Logger } from "../logging/logger"; |
| 9 | +import type { MementoManager } from "../storage/mementoManager"; |
| 10 | + |
| 11 | +export type ConnectionFailure = Exclude< |
| 12 | + WindowLogEvent, |
| 13 | + "extension_host_restarted" |
| 14 | +>; |
| 15 | + |
| 16 | +export interface ConnectionFailureWatcherOptions { |
| 17 | + readonly safeHostname: string; |
| 18 | + readonly remoteAuthority: string; |
| 19 | + readonly workspaceName: string; |
| 20 | + readonly codeLogDir: string; |
| 21 | + readonly mementoManager: MementoManager; |
| 22 | + readonly telemetry: SshTelemetry; |
| 23 | + readonly logger: Logger; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Longer than the 2s VS Code waits for the extension host's exit info before |
| 28 | + * it logs a restart: |
| 29 | + * https://github.com/microsoft/vscode/blob/e54c42b3917fa85e6f8e7c1eef3853f4db66cef6/src/vs/workbench/services/extensions/common/abstractExtensionService.ts#L899-L925 |
| 30 | + */ |
| 31 | +const EXTENSION_HOST_RESTART_WAIT_MS = 3000; |
| 32 | +const POLL_INTERVAL_MS = 250; |
| 33 | + |
| 34 | +/** |
| 35 | + * Offers a support bundle when VS Code gives up on the remote connection. The |
| 36 | + * failure stays stored until the user answers, so a reload offers it again. |
| 37 | + */ |
| 38 | +export class ConnectionFailureWatcher implements vscode.Disposable { |
| 39 | + private disposed = false; |
| 40 | + private timer: NodeJS.Timeout | undefined; |
| 41 | + private extensionHostLostAt: number | undefined; |
| 42 | + |
| 43 | + private constructor( |
| 44 | + private readonly options: ConnectionFailureWatcherOptions, |
| 45 | + ) {} |
| 46 | + |
| 47 | + public static start( |
| 48 | + options: ConnectionFailureWatcherOptions, |
| 49 | + ): ConnectionFailureWatcher { |
| 50 | + const watcher = new ConnectionFailureWatcher(options); |
| 51 | + void watcher.watch(); |
| 52 | + return watcher; |
| 53 | + } |
| 54 | + |
| 55 | + public dispose(): void { |
| 56 | + this.disposed = true; |
| 57 | + clearTimeout(this.timer); |
| 58 | + } |
| 59 | + |
| 60 | + private async watch(): Promise<void> { |
| 61 | + const { mementoManager, safeHostname, remoteAuthority, codeLogDir } = |
| 62 | + this.options; |
| 63 | + if (mementoManager.getConnectionFailure(safeHostname) === remoteAuthority) { |
| 64 | + this.offerSupportBundle( |
| 65 | + `The previous connection to ${this.options.workspaceName} failed. Create a support bundle to capture diagnostics?`, |
| 66 | + ); |
| 67 | + } |
| 68 | + const reader = await WindowLogReader.open(codeLogDir); |
| 69 | + if (!reader) { |
| 70 | + this.options.logger.debug( |
| 71 | + "Unrecognized log directory layout; not watching for connection failures", |
| 72 | + ); |
| 73 | + return; |
| 74 | + } |
| 75 | + this.schedulePoll(reader); |
| 76 | + } |
| 77 | + |
| 78 | + private schedulePoll(reader: WindowLogReader): void { |
| 79 | + if (!this.disposed) { |
| 80 | + this.timer = setTimeout(() => void this.poll(reader), POLL_INTERVAL_MS); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private async poll(reader: WindowLogReader): Promise<void> { |
| 85 | + try { |
| 86 | + for (const event of await reader.readEvents()) { |
| 87 | + this.handle(event); |
| 88 | + } |
| 89 | + } catch (error) { |
| 90 | + this.options.logger.debug("Could not read the VS Code window log", error); |
| 91 | + } |
| 92 | + if ( |
| 93 | + this.extensionHostLostAt !== undefined && |
| 94 | + Date.now() - this.extensionHostLostAt >= EXTENSION_HOST_RESTART_WAIT_MS |
| 95 | + ) { |
| 96 | + void this.report("extension_host_connection_lost"); |
| 97 | + } |
| 98 | + this.schedulePoll(reader); |
| 99 | + } |
| 100 | + |
| 101 | + private handle(event: WindowLogEvent): void { |
| 102 | + switch (event) { |
| 103 | + case "extension_host_connection_lost": |
| 104 | + // Restarts are logged at info level; without them a recovery looks like a failure. |
| 105 | + if (vscode.env.logLevel <= vscode.LogLevel.Info) { |
| 106 | + this.extensionHostLostAt ??= Date.now(); |
| 107 | + } |
| 108 | + return; |
| 109 | + case "extension_host_restarted": |
| 110 | + this.extensionHostLostAt = undefined; |
| 111 | + this.options.logger.info("VS Code restarted the remote extension host"); |
| 112 | + return; |
| 113 | + default: |
| 114 | + void this.report(event); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** Reports the first failure only, and stores it before the prompt shows. */ |
| 119 | + private async report(failure: ConnectionFailure): Promise<void> { |
| 120 | + if (this.disposed) { |
| 121 | + return; |
| 122 | + } |
| 123 | + this.dispose(); |
| 124 | + const { logger, telemetry, mementoManager, safeHostname, remoteAuthority } = |
| 125 | + this.options; |
| 126 | + logger.warn(`VS Code gave up on the remote connection: ${failure}`); |
| 127 | + logger.flush("remote_connection_failed"); |
| 128 | + telemetry.connectionFailed(failure); |
| 129 | + await this.persist( |
| 130 | + mementoManager.setConnectionFailure(safeHostname, remoteAuthority), |
| 131 | + ); |
| 132 | + this.offerSupportBundle( |
| 133 | + `The connection to ${this.options.workspaceName} failed. Create a support bundle to capture diagnostics?`, |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Never awaited: the notification can go away without resolving. The stored |
| 139 | + * failure clears only on an answer, so an unanswered one shows after a reload. |
| 140 | + */ |
| 141 | + private offerSupportBundle(message: string): void { |
| 142 | + void vscodeProposed.window |
| 143 | + .showErrorMessage(message, "Create Support Bundle") |
| 144 | + .then( |
| 145 | + (choice) => this.onAnswer(choice), |
| 146 | + (error: unknown) => |
| 147 | + this.options.logger.warn("Could not offer a support bundle", error), |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + private async onAnswer(choice: string | undefined): Promise<void> { |
| 152 | + const { mementoManager, safeHostname, logger } = this.options; |
| 153 | + await this.persist(mementoManager.clearConnectionFailure(safeHostname)); |
| 154 | + if (choice !== "Create Support Bundle") { |
| 155 | + return; |
| 156 | + } |
| 157 | + try { |
| 158 | + await vscodeProposed.commands.executeCommand("coder.supportBundle"); |
| 159 | + } catch (error) { |
| 160 | + logger.warn("Could not create a support bundle", error); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /** Best effort: a failed write only changes whether a reload prompts again. */ |
| 165 | + private async persist(write: Promise<void>): Promise<void> { |
| 166 | + try { |
| 167 | + await write; |
| 168 | + } catch (error) { |
| 169 | + this.options.logger.warn("Could not store the connection failure", error); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments