Skip to content

Commit

Permalink
fixed container stopping on disconnect and application close (#14542)
Browse files Browse the repository at this point in the history
Signed-off-by: Jonah Iden <[email protected]>
  • Loading branch information
jonah-iden authored Nov 27, 2024
1 parent 8ba447d commit 99f24f5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { DockerContainerService } from './docker-container-service';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { WriteStream } from 'tty';
import { PassThrough } from 'stream';
import { exec } from 'child_process';
import { exec, execSync } from 'child_process';
import { DevContainerFileService } from './dev-container-file-service';
import { ContainerOutputProvider } from '../electron-common/container-output-provider';

Expand Down Expand Up @@ -303,9 +303,13 @@ export class RemoteDockerContainerConnection implements RemoteConnection {
return deferred.promise;
}

async dispose(): Promise<void> {
disposeSync(): void {
// cant use dockerrode here since this needs to happen on one tick
exec(`docker stop ${this.container.id}`);
execSync(`docker stop ${this.container.id}`);
}

async dispose(): Promise<void> {
return this.container.stop();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,13 @@ export class RemoteFrontendContribution implements CommandContribution, Frontend
});
}

protected disconnectRemote(): void {
const port = new URLSearchParams(location.search).get('localPort');
if (port) {
this.windowService.reload({ search: { port } });
protected async disconnectRemote(): Promise<void> {
const searchParams = new URLSearchParams(location.search);
const localPort = searchParams.get('localPort');
if (localPort) {
const currentPort = searchParams.get('port');
this.remoteStatusService.connectionClosed(parseInt(currentPort ?? '0'));
this.windowService.reload({ search: { port: localPort } });
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/remote/src/electron-common/remote-status-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ export const RemoteStatusServicePath = '/remote/status';
export const RemoteStatusService = Symbol('RemoteStatusService');

export interface RemoteStatusService {
getStatus(localPort: number): Promise<RemoteStatus>
getStatus(localPort: number): Promise<RemoteStatus>;

connectionClosed(localPort: number): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ export class RemoteConnectionService implements BackendApplicationContribution {

onStop(): void {
for (const connection of this.connections.values()) {
connection.dispose();
if (connection.disposeSync) {
connection.disposeSync();
} else {
connection.dispose();
};
}
}
}
7 changes: 7 additions & 0 deletions packages/remote/src/electron-node/remote-status-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ export class RemoteStatusServiceImpl implements RemoteStatusService {
};
}
}

async connectionClosed(localPort: number): Promise<void> {
const connection = this.remoteConnectionService.getConnectionFromPort(localPort);
if (connection) {
connection.dispose();
}
}
}
5 changes: 5 additions & 0 deletions packages/remote/src/electron-node/remote-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ export interface RemoteConnection extends Disposable {
* copy files from local to remote
*/
copy(localPath: string | Buffer | NodeJS.ReadableStream, remotePath: string): Promise<void>;

/**
* used for disposing when theia is shutting down
*/
disposeSync?(): void;
}

0 comments on commit 99f24f5

Please sign in to comment.