Skip to content

Commit b2ab524

Browse files
authored
Merge pull request microsoft#187285 from microsoft/tyriar/187282
Ensure layout info is fetched from the pty host on reconnects
2 parents 9e390f3 + dc189ed commit b2ab524

File tree

4 files changed

+61
-61
lines changed

4 files changed

+61
-61
lines changed

src/vs/platform/terminal/node/ptyHostService.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,10 @@ export class PtyHostService extends Disposable implements IPtyHostService {
326326
return this._proxy.setTerminalLayoutInfo(args);
327327
}
328328
async getTerminalLayoutInfo(args: IGetTerminalLayoutInfoArgs): Promise<ITerminalsLayoutInfo | undefined> {
329-
return await this._proxy.getTerminalLayoutInfo(args);
329+
// This is optional as we want reconnect requests to go through only if the pty host exists.
330+
// Revive is handled specially as reviveTerminalProcesses is guaranteed to be called before
331+
// the request for layout info.
332+
return this._optionalProxy?.getTerminalLayoutInfo(args);
330333
}
331334

332335
async requestDetachInstance(workspaceId: string, instanceId: number): Promise<IProcessDetails | undefined> {

src/vs/platform/terminal/node/ptyService.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,9 @@ export class PtyService extends Disposable implements IPtyService {
552552
};
553553
} catch (e) {
554554
this._logService.warn(`Couldn't get layout info, a terminal was probably disconnected`, e.message);
555-
this._logService.info('Reattach to wrong terminal debug info - layout info by id', t);
556-
this._logService.info('Reattach to wrong terminal debug info - _revivePtyIdMap', Array.from(this._revivedPtyIdMap.values()));
555+
this._logService.debug('Reattach to wrong terminal debug info - layout info by id', t);
556+
this._logService.debug('Reattach to wrong terminal debug info - _revivePtyIdMap', Array.from(this._revivedPtyIdMap.values()));
557+
this._logService.debug('Reattach to wrong terminal debug info - _ptys ids', Array.from(this._ptys.keys()));
557558
// this will be filtered out and not reconnected
558559
return {
559560
terminal: null,

src/vs/workbench/contrib/terminal/browser/remoteTerminalBackend.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -337,29 +337,27 @@ class RemoteTerminalBackend extends BaseTerminalBackend implements ITerminalBack
337337

338338
// Revive processes if needed
339339
const serializedState = this._storageService.get(TerminalStorageKeys.TerminalBufferState, StorageScope.WORKSPACE);
340-
const parsed = this._deserializeTerminalState(serializedState);
341-
if (!parsed) {
342-
return undefined;
343-
}
344-
345-
try {
346-
// Note that remote terminals do not get their environment re-resolved unlike in local terminals
347-
348-
mark('code/terminal/willReviveTerminalProcessesRemote');
349-
await this._remoteTerminalChannel.reviveTerminalProcesses(parsed, Intl.DateTimeFormat().resolvedOptions().locale);
350-
mark('code/terminal/didReviveTerminalProcessesRemote');
351-
this._storageService.remove(TerminalStorageKeys.TerminalBufferState, StorageScope.WORKSPACE);
352-
// If reviving processes, send the terminal layout info back to the pty host as it
353-
// will not have been persisted on application exit
354-
const layoutInfo = this._storageService.get(TerminalStorageKeys.TerminalLayoutInfo, StorageScope.WORKSPACE);
355-
if (layoutInfo) {
356-
mark('code/terminal/willSetTerminalLayoutInfoRemote');
357-
await this._remoteTerminalChannel.setTerminalLayoutInfo(JSON.parse(layoutInfo));
358-
mark('code/terminal/didSetTerminalLayoutInfoRemote');
359-
this._storageService.remove(TerminalStorageKeys.TerminalLayoutInfo, StorageScope.WORKSPACE);
340+
const reviveBufferState = this._deserializeTerminalState(serializedState);
341+
if (reviveBufferState && reviveBufferState.length > 0) {
342+
try {
343+
// Note that remote terminals do not get their environment re-resolved unlike in local terminals
344+
345+
mark('code/terminal/willReviveTerminalProcessesRemote');
346+
await this._remoteTerminalChannel.reviveTerminalProcesses(reviveBufferState, Intl.DateTimeFormat().resolvedOptions().locale);
347+
mark('code/terminal/didReviveTerminalProcessesRemote');
348+
this._storageService.remove(TerminalStorageKeys.TerminalBufferState, StorageScope.WORKSPACE);
349+
// If reviving processes, send the terminal layout info back to the pty host as it
350+
// will not have been persisted on application exit
351+
const layoutInfo = this._storageService.get(TerminalStorageKeys.TerminalLayoutInfo, StorageScope.WORKSPACE);
352+
if (layoutInfo) {
353+
mark('code/terminal/willSetTerminalLayoutInfoRemote');
354+
await this._remoteTerminalChannel.setTerminalLayoutInfo(JSON.parse(layoutInfo));
355+
mark('code/terminal/didSetTerminalLayoutInfoRemote');
356+
this._storageService.remove(TerminalStorageKeys.TerminalLayoutInfo, StorageScope.WORKSPACE);
357+
}
358+
} catch (e: unknown) {
359+
this._logService.warn('RemoteTerminalBackend#getTerminalLayoutInfo Error', e && typeof e === 'object' && 'message' in e ? e.message : e);
360360
}
361-
} catch (e: unknown) {
362-
this._logService.warn('RemoteTerminalBackend#getTerminalLayoutInfo Error', e && typeof e === 'object' && 'message' in e ? e.message : e);
363361
}
364362

365363
return this._remoteTerminalChannel.getTerminalLayoutInfo();

src/vs/workbench/contrib/terminal/electron-sandbox/localTerminalBackend.ts

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -317,43 +317,41 @@ class LocalTerminalBackend extends BaseTerminalBackend implements ITerminalBacke
317317

318318
// Revive processes if needed
319319
const serializedState = this._storageService.get(TerminalStorageKeys.TerminalBufferState, StorageScope.WORKSPACE);
320-
const parsed = this._deserializeTerminalState(serializedState);
321-
if (!parsed) {
322-
return undefined;
323-
}
324-
325-
try {
326-
// Create variable resolver
327-
const activeWorkspaceRootUri = this._historyService.getLastActiveWorkspaceRoot();
328-
const lastActiveWorkspace = activeWorkspaceRootUri ? withNullAsUndefined(this._workspaceContextService.getWorkspaceFolder(activeWorkspaceRootUri)) : undefined;
329-
const variableResolver = terminalEnvironment.createVariableResolver(lastActiveWorkspace, await this._terminalProfileResolverService.getEnvironment(this.remoteAuthority), this._configurationResolverService);
330-
331-
// Re-resolve the environments and replace it on the state so local terminals use a fresh
332-
// environment
333-
mark('code/terminal/willGetReviveEnvironments');
334-
await Promise.all(parsed.map(state => new Promise<void>(r => {
335-
this._resolveEnvironmentForRevive(variableResolver, state.shellLaunchConfig).then(freshEnv => {
336-
state.processLaunchConfig.env = freshEnv;
337-
r();
338-
});
339-
})));
340-
mark('code/terminal/didGetReviveEnvironments');
341-
342-
mark('code/terminal/willReviveTerminalProcesses');
343-
await this._proxy.reviveTerminalProcesses(workspaceId, parsed, Intl.DateTimeFormat().resolvedOptions().locale);
344-
mark('code/terminal/didReviveTerminalProcesses');
345-
this._storageService.remove(TerminalStorageKeys.TerminalBufferState, StorageScope.WORKSPACE);
346-
// If reviving processes, send the terminal layout info back to the pty host as it
347-
// will not have been persisted on application exit
348-
const layoutInfo = this._storageService.get(TerminalStorageKeys.TerminalLayoutInfo, StorageScope.WORKSPACE);
349-
if (layoutInfo) {
350-
mark('code/terminal/willSetTerminalLayoutInfo');
351-
await this._proxy.setTerminalLayoutInfo(JSON.parse(layoutInfo));
352-
mark('code/terminal/didSetTerminalLayoutInfo');
353-
this._storageService.remove(TerminalStorageKeys.TerminalLayoutInfo, StorageScope.WORKSPACE);
320+
const reviveBufferState = this._deserializeTerminalState(serializedState);
321+
if (reviveBufferState && reviveBufferState.length > 0) {
322+
try {
323+
// Create variable resolver
324+
const activeWorkspaceRootUri = this._historyService.getLastActiveWorkspaceRoot();
325+
const lastActiveWorkspace = activeWorkspaceRootUri ? withNullAsUndefined(this._workspaceContextService.getWorkspaceFolder(activeWorkspaceRootUri)) : undefined;
326+
const variableResolver = terminalEnvironment.createVariableResolver(lastActiveWorkspace, await this._terminalProfileResolverService.getEnvironment(this.remoteAuthority), this._configurationResolverService);
327+
328+
// Re-resolve the environments and replace it on the state so local terminals use a fresh
329+
// environment
330+
mark('code/terminal/willGetReviveEnvironments');
331+
await Promise.all(reviveBufferState.map(state => new Promise<void>(r => {
332+
this._resolveEnvironmentForRevive(variableResolver, state.shellLaunchConfig).then(freshEnv => {
333+
state.processLaunchConfig.env = freshEnv;
334+
r();
335+
});
336+
})));
337+
mark('code/terminal/didGetReviveEnvironments');
338+
339+
mark('code/terminal/willReviveTerminalProcesses');
340+
await this._proxy.reviveTerminalProcesses(workspaceId, reviveBufferState, Intl.DateTimeFormat().resolvedOptions().locale);
341+
mark('code/terminal/didReviveTerminalProcesses');
342+
this._storageService.remove(TerminalStorageKeys.TerminalBufferState, StorageScope.WORKSPACE);
343+
// If reviving processes, send the terminal layout info back to the pty host as it
344+
// will not have been persisted on application exit
345+
const layoutInfo = this._storageService.get(TerminalStorageKeys.TerminalLayoutInfo, StorageScope.WORKSPACE);
346+
if (layoutInfo) {
347+
mark('code/terminal/willSetTerminalLayoutInfo');
348+
await this._proxy.setTerminalLayoutInfo(JSON.parse(layoutInfo));
349+
mark('code/terminal/didSetTerminalLayoutInfo');
350+
this._storageService.remove(TerminalStorageKeys.TerminalLayoutInfo, StorageScope.WORKSPACE);
351+
}
352+
} catch (e: unknown) {
353+
this._logService.warn('LocalTerminalBackend#getTerminalLayoutInfo Error', e && typeof e === 'object' && 'message' in e ? e.message : e);
354354
}
355-
} catch (e: unknown) {
356-
this._logService.warn('LocalTerminalBackend#getTerminalLayoutInfo Error', e && typeof e === 'object' && 'message' in e ? e.message : e);
357355
}
358356

359357
return this._proxy.getTerminalLayoutInfo(layoutArgs);

0 commit comments

Comments
 (0)