Skip to content

Commit cd4dbdf

Browse files
Register port in port attribute provider (#140)
* Add functions to register ports * Addd debugport handler * check if ort already exist * fix lint * fix error
1 parent 4bf3b6a commit cd4dbdf

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

src/extension/debugger/debugPort/portAttributesProvider.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,34 @@
22
* Copyright (C) Microsoft Corporation. All rights reserved.
33
*--------------------------------------------------------*/
44

5-
import { CancellationToken, PortAttributes, PortAttributesProvider, ProviderResult } from 'vscode';
5+
import {
6+
CancellationToken,
7+
PortAttributes,
8+
PortAttributesProvider,
9+
PortAutoForwardAction,
10+
ProviderResult,
11+
} from 'vscode';
612

713
export class DebugPortAttributesProvider implements PortAttributesProvider {
14+
private knownPorts: number[] = [];
15+
16+
public setPortAttribute(port: number): void {
17+
if (!this.knownPorts.includes(port)) {
18+
this.knownPorts.push(port);
19+
}
20+
}
21+
22+
public resetPortAttribute(): void {
23+
this.knownPorts.pop();
24+
}
25+
826
public providePortAttributes(
9-
_attributes: { port: number; pid?: number; commandLine?: string },
27+
attributes: { port: number; pid?: number; commandLine?: string },
1028
_token: CancellationToken,
1129
): ProviderResult<PortAttributes> {
30+
if (this.knownPorts.includes(attributes.port)) {
31+
return new PortAttributes(PortAutoForwardAction.Ignore);
32+
}
1233
return undefined;
1334
}
1435
}

src/extension/debugger/hooks/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export enum DebuggerEvents {
88
// Event sent by PTVSD when a child process is launched and ready to be attached to for multi-proc debugging.
99
PtvsdAttachToSubprocess = 'ptvsd_attach',
1010
DebugpyAttachToSubprocess = 'debugpyAttach',
11+
DebugpySockets = 'debugpySockets',
1112
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
'use strict';
5+
6+
import { inject, injectable } from 'inversify';
7+
import { DebugSessionCustomEvent } from 'vscode';
8+
import { swallowExceptions } from '../../common/utils/decorators';
9+
import { DebuggerEvents } from './constants';
10+
import { DebuggerTypeName } from '../../constants';
11+
import { DebugPortAttributesProvider } from '../debugPort/portAttributesProvider';
12+
import { IDebugSessionEventHandlers } from './types';
13+
14+
/**
15+
* This class is responsible for register ports using by debugpy in the portProvider.
16+
* @export
17+
* @class ChildProcessAttachEventHandler
18+
* @implements {IDebugSessionEventHandlers}
19+
*/
20+
@injectable()
21+
export class DebugpySocketsHandler implements IDebugSessionEventHandlers {
22+
constructor(
23+
@inject(DebugPortAttributesProvider) private readonly debugPortAttributesProvider: DebugPortAttributesProvider,
24+
) {}
25+
26+
@swallowExceptions('Handle child process launch')
27+
public async handleCustomEvent(event: DebugSessionCustomEvent): Promise<void> {
28+
if (!event || event.session.configuration.type !== DebuggerTypeName) {
29+
return;
30+
}
31+
32+
if (event.event == DebuggerEvents.DebugpySockets) {
33+
let portSocket = event.body.sockets.find((socket: { [x: string]: any }) => {
34+
return socket['internal'] == false;
35+
});
36+
if (portSocket != undefined) {
37+
this.debugPortAttributesProvider.setPortAttribute(portSocket.port);
38+
}
39+
} else {
40+
return;
41+
}
42+
}
43+
}

src/extension/extensionInit.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { ignoreErrors } from './common/promiseUtils';
3333
import { pickArgsInput } from './common/utils/localize';
3434
import { DebugPortAttributesProvider } from './debugger/debugPort/portAttributesProvider';
3535
import { getConfigurationsByUri } from './debugger/configuration/launch.json/launchJsonReader';
36+
import { DebugpySocketsHandler } from './debugger/hooks/debugpySocketsHandler';
3637

3738
export async function registerDebugger(context: IExtensionContext): Promise<void> {
3839
const childProcessAttachService = new ChildProcessAttachService();
@@ -155,4 +156,17 @@ export async function registerDebugger(context: IExtensionContext): Promise<void
155156
debugPortAttributesProvider,
156157
),
157158
);
159+
160+
const debugpySocketsHandler = new DebugpySocketsHandler(debugPortAttributesProvider);
161+
context.subscriptions.push(
162+
debug.onDidReceiveDebugSessionCustomEvent((e) => {
163+
ignoreErrors(debugpySocketsHandler.handleCustomEvent(e));
164+
}),
165+
);
166+
167+
context.subscriptions.push(
168+
debug.onDidTerminateDebugSession(() => {
169+
debugPortAttributesProvider.resetPortAttribute();
170+
}),
171+
);
158172
}

0 commit comments

Comments
 (0)