From a74ea83b5c1eedfff8b6b729314d63805cfc658d Mon Sep 17 00:00:00 2001 From: Paula Camargo Date: Thu, 23 Nov 2023 14:50:29 -0500 Subject: [PATCH 1/5] Add functions to register ports --- .../debugPort/portAttributesProvider.ts | 17 +++++++++++++++-- src/extension/extensionInit.ts | 8 ++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/extension/debugger/debugPort/portAttributesProvider.ts b/src/extension/debugger/debugPort/portAttributesProvider.ts index 13fea1b5..ab90ba19 100644 --- a/src/extension/debugger/debugPort/portAttributesProvider.ts +++ b/src/extension/debugger/debugPort/portAttributesProvider.ts @@ -2,13 +2,26 @@ * 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 { + this.knownPorts.push(port); + } + + public resetPortAttribute(port: number): void { + this.knownPorts = this.knownPorts.filter((p) => p !== port); + } + public providePortAttributes( - _attributes: { port: number; pid?: number; commandLine?: string }, + attributes: { port: number; pid?: number; commandLine?: string }, _token: CancellationToken, ): ProviderResult { + if (this.knownPorts.includes(attributes.port)) { + return new PortAttributes(PortAutoForwardAction.Ignore); + } return undefined; } } diff --git a/src/extension/extensionInit.ts b/src/extension/extensionInit.ts index 477e7f1d..a9edb36f 100644 --- a/src/extension/extensionInit.ts +++ b/src/extension/extensionInit.ts @@ -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 { const childProcessAttachService = new ChildProcessAttachService(); @@ -154,4 +155,11 @@ export async function registerDebugger(context: IExtensionContext): Promise { + ignoreErrors(debugpySocketsHandler.handleCustomEvent(e)); + }), + ); } From b160cb39957baf113b9d7e560e2f06bb3d7c635e Mon Sep 17 00:00:00 2001 From: Paula Camargo Date: Thu, 23 Nov 2023 14:50:46 -0500 Subject: [PATCH 2/5] Addd debugport handler --- src/extension/debugger/hooks/constants.ts | 1 + .../debugger/hooks/debugpySocketsHandler.ts | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/extension/debugger/hooks/debugpySocketsHandler.ts diff --git a/src/extension/debugger/hooks/constants.ts b/src/extension/debugger/hooks/constants.ts index add0fcae..7b27a241 100644 --- a/src/extension/debugger/hooks/constants.ts +++ b/src/extension/debugger/hooks/constants.ts @@ -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', } diff --git a/src/extension/debugger/hooks/debugpySocketsHandler.ts b/src/extension/debugger/hooks/debugpySocketsHandler.ts new file mode 100644 index 00000000..8d0c8c6e --- /dev/null +++ b/src/extension/debugger/hooks/debugpySocketsHandler.ts @@ -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 { + 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; + } + } +} From 4c9ba606d87688a00df4e5edc9cf4489bc502ca0 Mon Sep 17 00:00:00 2001 From: Paula Camargo Date: Mon, 27 Nov 2023 11:13:24 -0500 Subject: [PATCH 3/5] check if ort already exist --- .../debugger/debugPort/portAttributesProvider.ts | 8 +++++--- src/extension/extensionInit.ts | 8 +++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/extension/debugger/debugPort/portAttributesProvider.ts b/src/extension/debugger/debugPort/portAttributesProvider.ts index ab90ba19..72119276 100644 --- a/src/extension/debugger/debugPort/portAttributesProvider.ts +++ b/src/extension/debugger/debugPort/portAttributesProvider.ts @@ -8,11 +8,13 @@ export class DebugPortAttributesProvider implements PortAttributesProvider { private knownPorts: number[] = []; public setPortAttribute(port: number): void { - this.knownPorts.push(port); + if (!this.knownPorts.includes(port)) { + this.knownPorts.push(port); + } } - public resetPortAttribute(port: number): void { - this.knownPorts = this.knownPorts.filter((p) => p !== port); + public resetPortAttribute(): void { + this.knownPorts.pop(); } public providePortAttributes( diff --git a/src/extension/extensionInit.ts b/src/extension/extensionInit.ts index a9edb36f..650cb3c5 100644 --- a/src/extension/extensionInit.ts +++ b/src/extension/extensionInit.ts @@ -151,7 +151,7 @@ export async function registerDebugger(context: IExtensionContext): Promise { + debugPortAttributesProvider.resetPortAttribute(); + }), + ); } From 119113d2555f211c3e53f7d901824e4cafe7c8ba Mon Sep 17 00:00:00 2001 From: Paula Camargo Date: Mon, 27 Nov 2023 11:17:29 -0500 Subject: [PATCH 4/5] fix lint --- .../debugger/debugPort/portAttributesProvider.ts | 10 ++++++++-- src/extension/debugger/hooks/debugpySocketsHandler.ts | 2 +- src/extension/extensionInit.ts | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/extension/debugger/debugPort/portAttributesProvider.ts b/src/extension/debugger/debugPort/portAttributesProvider.ts index 72119276..21737185 100644 --- a/src/extension/debugger/debugPort/portAttributesProvider.ts +++ b/src/extension/debugger/debugPort/portAttributesProvider.ts @@ -2,7 +2,13 @@ * Copyright (C) Microsoft Corporation. All rights reserved. *--------------------------------------------------------*/ -import { CancellationToken, PortAttributes, PortAttributesProvider, PortAutoForwardAction, ProviderResult } from 'vscode'; +import { + CancellationToken, + PortAttributes, + PortAttributesProvider, + PortAutoForwardAction, + ProviderResult, +} from 'vscode'; export class DebugPortAttributesProvider implements PortAttributesProvider { private knownPorts: number[] = []; @@ -16,7 +22,7 @@ export class DebugPortAttributesProvider implements PortAttributesProvider { public resetPortAttribute(): void { this.knownPorts.pop(); } - + public providePortAttributes( attributes: { port: number; pid?: number; commandLine?: string }, _token: CancellationToken, diff --git a/src/extension/debugger/hooks/debugpySocketsHandler.ts b/src/extension/debugger/hooks/debugpySocketsHandler.ts index 8d0c8c6e..b777a271 100644 --- a/src/extension/debugger/hooks/debugpySocketsHandler.ts +++ b/src/extension/debugger/hooks/debugpySocketsHandler.ts @@ -30,7 +30,7 @@ export class DebugpySocketsHandler implements IDebugSessionEventHandlers { } if (event.event == DebuggerEvents.DebugpySockets) { - let portSocket = event.body.sockets.find((socket: { [x: string]: any; }) => { + let portSocket = event.body.sockets.find((socket: { [x: string]: any }) => { return socket['internal'] == false; }); if (portSocket != undefined) { diff --git a/src/extension/extensionInit.ts b/src/extension/extensionInit.ts index 650cb3c5..1f5cc72a 100644 --- a/src/extension/extensionInit.ts +++ b/src/extension/extensionInit.ts @@ -151,12 +151,12 @@ export async function registerDebugger(context: IExtensionContext): Promise { ignoreErrors(debugpySocketsHandler.handleCustomEvent(e)); From e3f9681dcf2375de490b0fbd284736bd35b0897a Mon Sep 17 00:00:00 2001 From: Paula Camargo Date: Mon, 27 Nov 2023 12:50:51 -0500 Subject: [PATCH 5/5] fix error --- src/extension/extensionInit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extension/extensionInit.ts b/src/extension/extensionInit.ts index 1f5cc72a..d450de7f 100644 --- a/src/extension/extensionInit.ts +++ b/src/extension/extensionInit.ts @@ -164,7 +164,7 @@ export async function registerDebugger(context: IExtensionContext): Promise { + debug.onDidTerminateDebugSession(() => { debugPortAttributesProvider.resetPortAttribute(); }), );