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

Fix fs.watch leak with a timestamp to prevent repetitive buildFromConfig #7238

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Changes from 1 commit
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
16 changes: 7 additions & 9 deletions pkg/rancher-desktop/main/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export class Tray {
private settings: Settings;
private currentNetworkStatus: networkStatus = networkStatus.CHECKING;
private static instance: Tray;
private abortController: AbortController | undefined;
private networkState: boolean | undefined;
private networkInterval: NodeJS.Timeout;
private lastConfigBuild = 0;

protected contextMenuItems: Electron.MenuItemConstructorOptions[] = [
{
Expand Down Expand Up @@ -130,15 +130,11 @@ export class Tray {
* triggered, close the watcher and restart after a duration (one second).
*/
private async watchForChanges() {
const abortController = new AbortController();

this.abortController = abortController;
const paths = await kubeconfig.getKubeConfigPaths();
const options: fs.WatchOptions = {
persistent: false,
recursive: !this.isLinux(), // Recursive not implemented in Linux
encoding: 'utf-8',
signal: abortController.signal,
};

paths.map(filepath => fs.watch(filepath, options, async(eventType) => {
mikeseese marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -151,10 +147,7 @@ export class Tray {
}
}

this.abortController?.abort();
this.buildFromConfig();

setTimeout(this.watchForChanges.bind(this), 1_000);
}));
}

Expand Down Expand Up @@ -242,7 +235,6 @@ export class Tray {
*/
public hide() {
this.trayMenu.destroy();
this.abortController?.abort();
mainEvents.off('k8s-check-state', this.k8sStateChangedEvent);
mainEvents.off('settings-update', this.settingsUpdateEvent);
ipcMainProxy.removeListener('update-network-status', this.updateNetworkStatusEvent);
Expand All @@ -269,6 +261,12 @@ export class Tray {
}

protected buildFromConfig() {
if (Date.now() - this.lastConfigBuild < 1000) {
return;
}

this.lastConfigBuild = Date.now();
mikeseese marked this conversation as resolved.
Show resolved Hide resolved

try {
this.updateContexts();
const contextMenu = Electron.Menu.buildFromTemplate(this.contextMenuItems);
Expand Down
Loading