Skip to content

Do not fail interpreter discovery if accessing Windows registry fails. #13369

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
merged 2 commits into from
Aug 11, 2020
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
1 change: 1 addition & 0 deletions news/2 Fixes/12962.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not fail interpreter discovery if accessing Windows registry fails.
13 changes: 11 additions & 2 deletions src/client/common/platform/registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { injectable } from 'inversify';
import { Options } from 'winreg';
import { traceError } from '../logger';
import { Architecture } from '../utils/platform';
import { IRegistry, RegistryHive } from './types';

Expand All @@ -11,10 +12,18 @@ enum RegistryArchitectures {
@injectable()
export class RegistryImplementation implements IRegistry {
public async getKeys(key: string, hive: RegistryHive, arch?: Architecture) {
return getRegistryKeys({ hive: translateHive(hive)!, arch: translateArchitecture(arch), key });
return getRegistryKeys({ hive: translateHive(hive)!, arch: translateArchitecture(arch), key }).catch((ex) => {
traceError('Fetching keys from windows registry resulted in an error', ex);
return [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at the getRegistryKeys implementation. We already return empty array. I don't think we will hit this case.

Copy link
Author

@karrtikr karrtikr Aug 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async function getRegistryKeys(options: Options): Promise<string[]> {
// tslint:disable-next-line:no-require-imports
const Registry = require('winreg') as typeof import('winreg');
// https://github.com/python/peps/blob/master/pep-0514.txt#L85
return new Promise<string[]>((resolve) => {
new Registry(options).keys((error, result) => {
if (error || !Array.isArray(result)) {
return resolve([]);
}
resolve(result.filter((item) => typeof item.key === 'string').map((item) => item.key));
});
});
}

I think you're assuming that new Registry(options).keys at line 59 does not throw any errors and respects the callback. I checked the error trace user uploaded,

console.ts:137 [Extension Host] Error Python Extension: 2020-07-15 14:50:32: Get Interpreters, Class name = m, completed in 9ms, has a falsy return value, Arg 1: <Uri:c:\Users\chmay\source\mkl\mkl_email_service>, Arg 2: {"onSuggestion":true}, Return Value: undefined Error: spawn EPERM    
    at ChildProcess.spawn (internal/child_process.js:394:11)
    at spawn (child_process.js:553:9)
    at y.keys (c:\Users\chmay\.vscode\extensions\ms-python.python-2020.6.91350\out\client\extension.js:1:621980)
    at c:\Users\chmay\.vscode\extensions\ms-python.python-2020.6.91350\out\client\extension.js:1:617508
    at new Promise (<anonymous>)
    at c:\Users\chmay\.vscode\extensions\ms-python.python-2020.6.91350\out\client\extension.js:1:617483
    at c.getKeys (c:\Users\chmay\.vscode\extensions\ms-python.python-2020.6.91350\out\client\extension.js:1:617615)
    at b.getCompanies (c:\Users\chmay\.vscode\extensions\ms-python.python-2020.6.91350\out\client\extension.js:48:796340)
    at b.getInterpretersFromRegistry (c:\Users\chmay\.vscode\extensions\ms-python.python-2020.6.91350\out\client\extension.js:48:795831)

It fails at y.keys in the extension code, which is new Registry(options).keys

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Go it.

});
}
public async getValue(key: string, hive: RegistryHive, arch?: Architecture, name: string = '') {
return getRegistryValue({ hive: translateHive(hive)!, arch: translateArchitecture(arch), key }, name);
return getRegistryValue({ hive: translateHive(hive)!, arch: translateArchitecture(arch), key }, name).catch(
(ex) => {
traceError('Fetching key value from windows registry resulted in an error', ex);
return undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

}
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ export class WindowsRegistryService extends CacheableLocatorService {
// tslint:disable-next-line:no-empty
public dispose() {}
protected async getInterpretersImplementation(_resource?: Uri): Promise<PythonInterpreter[]> {
return this.platform.isWindows ? this.getInterpretersFromRegistry() : [];
return this.platform.isWindows
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have put this just to make sure we fix the bug. We should probably have done this with all locators.

? this.getInterpretersFromRegistry().catch((ex) => {
traceError('Fetching interpreters from registry failed with error', ex);
return [];
})
: [];
}
private async getInterpretersFromRegistry() {
// https://github.com/python/peps/blob/master/pep-0514.txt#L357
Expand Down