From 09d535ef9817a63096dc724cc4d9f0671ee6e5a3 Mon Sep 17 00:00:00 2001
From: John Livingston <git@john-livingston.fr>
Date: Thu, 8 Apr 2021 17:37:14 +0200
Subject: [PATCH] Fix #3940: unload all children from the plugin module on
 updates.

---
 package.json                         | 1 +
 server/lib/plugins/plugin-manager.ts | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/package.json b/package.json
index e3766e318..e6ae0a27e 100644
--- a/package.json
+++ b/package.json
@@ -99,6 +99,7 @@
     "cookie-parser": "^1.4.3",
     "cors": "^2.8.1",
     "create-torrent": "^4.0.0",
+    "decache": "^4.6.0",
     "deep-object-diff": "^1.1.0",
     "email-templates": "^8.0.3",
     "express": "^4.12.4",
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts
index c19b40135..0086a0e2e 100644
--- a/server/lib/plugins/plugin-manager.ts
+++ b/server/lib/plugins/plugin-manager.ts
@@ -23,6 +23,8 @@ import { ClientHtml } from '../client-html'
 import { RegisterHelpers } from './register-helpers'
 import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn'
 
+const decache = require('decache')
+
 export interface RegisteredPlugin {
   npmName: string
   name: string
@@ -411,7 +413,7 @@ export class PluginManager implements ServerHook {
 
     // Delete cache if needed
     const modulePath = join(pluginPath, packageJSON.library)
-    delete require.cache[modulePath]
+    decache(modulePath)
     const library: PluginLibrary = require(modulePath)
 
     if (!isLibraryCodeValid(library)) {