Skip to content

Commit

Permalink
interp/wazero: correct handling for Init/Load/Halt to reclaim all res…
Browse files Browse the repository at this point in the history
…ources possible from runtime

Signed-off-by: deadprogram <ron@hybridgroup.com>
  • Loading branch information
deadprogram committed Apr 1, 2024
1 parent ca595b7 commit 0995e2f
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions interp/wazero/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"runtime"

"github.com/hybridgroup/mechanoid"
"github.com/hybridgroup/mechanoid/engine"
Expand All @@ -24,6 +25,10 @@ func (i *Interpreter) Name() string {
}

func (i *Interpreter) Init() error {
return i.init()
}

func (i *Interpreter) init() error {
mechanoid.DebugMemory("Interpreter Init")

ctx := context.Background()
Expand Down Expand Up @@ -55,6 +60,12 @@ func (i *Interpreter) SetModules(modules wypes.Modules) error {
}

func (i *Interpreter) Load(code engine.Reader) error {
if i.runtime == nil {
if err := i.init(); err != nil {
return fmt.Errorf("init wazero runtime: %v", err)
}
}

mechanoid.DebugMemory("Interpreter Load")

err := i.defineModules()
Expand Down Expand Up @@ -140,9 +151,13 @@ func (i *Interpreter) Halt() error {
mechanoid.DebugMemory("Interpreter Halt")

ctx := context.Background()
err := i.module.Close(ctx)
err := i.runtime.Close(ctx)
i.runtime = nil
i.module = nil
clear(i.modules)

// force a garbage collection to free memory
runtime.GC()
mechanoid.DebugMemory("Interpreter Halt after GC")

return err
}
Expand Down

0 comments on commit 0995e2f

Please sign in to comment.