Skip to content

Register port in port attribute provider #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/extension/debugger/debugPort/portAttributesProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,34 @@
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import { CancellationToken, PortAttributes, PortAttributesProvider, ProviderResult } from 'vscode';
import {
CancellationToken,
PortAttributes,
PortAttributesProvider,
PortAutoForwardAction,
ProviderResult,
} from 'vscode';

export class DebugPortAttributesProvider implements PortAttributesProvider {
private knownPorts: number[] = [];

public setPortAttribute(port: number): void {
if (!this.knownPorts.includes(port)) {
this.knownPorts.push(port);
}
}

public resetPortAttribute(): void {
this.knownPorts.pop();
}

public providePortAttributes(
_attributes: { port: number; pid?: number; commandLine?: string },
attributes: { port: number; pid?: number; commandLine?: string },
_token: CancellationToken,
): ProviderResult<PortAttributes> {
if (this.knownPorts.includes(attributes.port)) {
return new PortAttributes(PortAutoForwardAction.Ignore);
}
return undefined;
}
}
1 change: 1 addition & 0 deletions src/extension/debugger/hooks/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export enum DebuggerEvents {
// Event sent by PTVSD when a child process is launched and ready to be attached to for multi-proc debugging.
PtvsdAttachToSubprocess = 'ptvsd_attach',
DebugpyAttachToSubprocess = 'debugpyAttach',
DebugpySockets = 'debugpySockets',
}
43 changes: 43 additions & 0 deletions src/extension/debugger/hooks/debugpySocketsHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import { inject, injectable } from 'inversify';
import { DebugSessionCustomEvent } from 'vscode';
import { swallowExceptions } from '../../common/utils/decorators';
import { DebuggerEvents } from './constants';
import { DebuggerTypeName } from '../../constants';
import { DebugPortAttributesProvider } from '../debugPort/portAttributesProvider';
import { IDebugSessionEventHandlers } from './types';

/**
* This class is responsible for register ports using by debugpy in the portProvider.
* @export
* @class ChildProcessAttachEventHandler
* @implements {IDebugSessionEventHandlers}
*/
@injectable()
export class DebugpySocketsHandler implements IDebugSessionEventHandlers {
constructor(
@inject(DebugPortAttributesProvider) private readonly debugPortAttributesProvider: DebugPortAttributesProvider,
) {}

@swallowExceptions('Handle child process launch')
public async handleCustomEvent(event: DebugSessionCustomEvent): Promise<void> {
if (!event || event.session.configuration.type !== DebuggerTypeName) {
return;
}

if (event.event == DebuggerEvents.DebugpySockets) {
let portSocket = event.body.sockets.find((socket: { [x: string]: any }) => {
return socket['internal'] == false;
});
if (portSocket != undefined) {
this.debugPortAttributesProvider.setPortAttribute(portSocket.port);
}
} else {
return;
}
}
}
14 changes: 14 additions & 0 deletions src/extension/extensionInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { ignoreErrors } from './common/promiseUtils';
import { pickArgsInput } from './common/utils/localize';
import { DebugPortAttributesProvider } from './debugger/debugPort/portAttributesProvider';
import { getConfigurationsByUri } from './debugger/configuration/launch.json/launchJsonReader';
import { DebugpySocketsHandler } from './debugger/hooks/debugpySocketsHandler';

export async function registerDebugger(context: IExtensionContext): Promise<void> {
const childProcessAttachService = new ChildProcessAttachService();
Expand Down Expand Up @@ -154,4 +155,17 @@ export async function registerDebugger(context: IExtensionContext): Promise<void
debugPortAttributesProvider,
),
);

const debugpySocketsHandler = new DebugpySocketsHandler(debugPortAttributesProvider);
context.subscriptions.push(
debug.onDidReceiveDebugSessionCustomEvent((e) => {
ignoreErrors(debugpySocketsHandler.handleCustomEvent(e));
}),
);

context.subscriptions.push(
debug.onDidTerminateDebugSession(() => {
debugPortAttributesProvider.resetPortAttribute();
}),
);
}