Skip to content
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

[plug-in] memory: fix memory leak on plug-ins reload #4931

Merged
merged 1 commit into from
Apr 17, 2019
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
2 changes: 1 addition & 1 deletion packages/plugin-ext-vscode/src/node/plugin-vscode-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ export const doInitialization: BackendInitializationFn = (apiFactory: PluginAPIF

pluginsApiImpl.set(plugin.model.id, vscode);
plugins.push(plugin);
pluginApiFactory = apiFactory;

if (!isLoadOverride) {
overrideInternalLoad();
isLoadOverride = true;
pluginApiFactory = apiFactory;
}
};

Expand Down
27 changes: 25 additions & 2 deletions packages/plugin-ext/src/hosted/node/plugin-host-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export class PluginHostRPC {
* note: stopPlugin can also be invoked through RPC proxy.
*/
stopContext(): PromiseLike<void> {
return this.pluginManager.$stopPlugin('');
const promise = this.pluginManager.$stopPlugin('');
promise.then(() => delete this.apiFactory);
return promise;
}

// tslint:disable-next-line:no-any
Expand All @@ -92,11 +94,32 @@ export class PluginHostRPC {
console.log('PLUGIN_HOST(' + process.pid + '): PluginManagerExtImpl/loadPlugin(' + plugin.pluginPath + ')');
try {
// cleaning the cache for all files of that plug-in.
Object.keys(require.cache).forEach(key => {
Object.keys(require.cache).forEach(function (key) {
const mod = require.cache[key];

// remove children that are extensions
let i = mod.children.length;
while (i--) {
const childMod: NodeJS.Module = mod.children[i];
if (childMod && childMod.id.startsWith(plugin.pluginFolder)) {
// cleanup exports
childMod.exports = {};
mod.children.splice(i, 1);
for (let j = 0; j < childMod.children.length; j++) {
delete childMod.children[j];
}
}
}

if (key.startsWith(plugin.pluginFolder)) {
// delete entry
delete require.cache[key];
const ix = mod.parent.children.indexOf(mod);
if (ix >= 0) {
mod.parent.children.splice(ix, 1);
}
}

});
return require(plugin.pluginPath);
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ export const doInitialization: BackendInitializationFn = (apiFactory: PluginAPIF
pluginsApiImpl.set(plugin.model.id, apiImpl);

plugins.push(plugin);
pluginApiFactory = apiFactory;

if (!isLoadOverride) {
overrideInternalLoad();
isLoadOverride = true;
pluginApiFactory = apiFactory;
}

};
Expand Down
6 changes: 6 additions & 0 deletions packages/plugin-ext/src/plugin/plugin-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export class PluginManagerExtImpl implements PluginManagerExt, PluginManager {
dispose(pluginContext.subscriptions);
}
});

// clean map
this.activatedPlugins.clear();
this.pluginActivationPromises.clear();
this.pluginContextsMap.clear();

return Promise.resolve();
}

Expand Down