From aff2918b7372e5e468a20b6e01dede56883ad341 Mon Sep 17 00:00:00 2001 From: Dan Phillips <20820229+dphilla@users.noreply.github.com> Date: Fri, 8 Mar 2024 16:46:30 -0600 Subject: [PATCH] Fix growing memory on repeated Instance() invocation (#130) * Fix: memory growing on repeated Instance() call * rm accidental trunk files * Add instantiated mod close on single mod cleanup() * Chg to delete instance for fn chaining * ci linter fix --- module.go | 1 + scale.go | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/module.go b/module.go index e1a6d196..f3dce971 100644 --- a/module.go +++ b/module.go @@ -155,6 +155,7 @@ func (m *module[T]) register(function *function[T]) { func (m *module[T]) cleanup() { m.function = nil m.template.runtime.activeModulesMu.Lock() + // m.instantiatedModule.CloseWithExitCode(m.template.runtime.config.context, 0) delete(m.template.runtime.activeModules, m.instantiatedModule.Name()) m.template.runtime.activeModulesMu.Unlock() } diff --git a/scale.go b/scale.go index 5f9b214f..1e5255c3 100644 --- a/scale.go +++ b/scale.go @@ -70,6 +70,15 @@ func (r *Scale[T]) Instance(next ...Next[T]) (*Instance[T], error) { return newInstance(r.config.context, r, next...) } +func (r *Scale[T]) Clear() { + for key := range r.activeModules { + r.activeModules[key].instantiatedModule.CloseWithExitCode(r.config.context, 0) + delete(r.activeModules, key) + } + + r.activeModules = make(map[string]*module[T]) +} + // Reset any extensions between executions. func (r *Scale[T]) resetExtensions() { for _, ext := range r.config.extensions {