Skip to content

Commit

Permalink
fix: memory leak in wasm creation
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Oct 11, 2023
1 parent cb55488 commit e015cce
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions internal/phplint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/tetratelabs/wazero"
Expand All @@ -24,12 +25,14 @@ func LintFolder(ctx context.Context, phpVersion, folder string) (LintErrors, err
return nil, err
}

runtime, err := getWazeroRuntime(ctx)
wasmRuntime, err := getWazeroRuntime(ctx)
if err != nil {
return nil, err
}

wasmCompiled, _ := runtime.CompileModule(ctx, wasmFile)
defer wasmRuntime.Close(ctx)

Check warning on line 33 in internal/phplint/lint.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Unhandled error

Unhandled error

wasmCompiled, _ := wasmRuntime.CompileModule(ctx, wasmFile)

dirFs := os.DirFS(folder)

Expand All @@ -47,6 +50,8 @@ func LintFolder(ctx context.Context, phpVersion, folder string) (LintErrors, err

errorsChain := make(chan *LintError, len(paths))

runtime.GOMAXPROCS(2)

for _, file := range paths {
go func(file string) {
file, _ = filepath.Rel(folder, file)
Expand All @@ -58,7 +63,7 @@ func LintFolder(ctx context.Context, phpVersion, folder string) (LintErrors, err
WithArgs("php", "-l", file).
WithFS(dirFs)

if _, err := runtime.InstantiateModule(ctx, wasmCompiled, config); err != nil {
if wasmModule, err := wasmRuntime.InstantiateModule(ctx, wasmCompiled, config); err != nil {
if exitErr, ok := err.(*sys.ExitError); ok && exitErr.ExitCode() != 0 {

Check notice on line 67 in internal/phplint/lint.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Type assertion on errors fails on wrapped errors

Type assertion on errors fails on wrapped errors
errorsChain <- &LintError{
File: file,
Expand All @@ -72,7 +77,12 @@ func LintFolder(ctx context.Context, phpVersion, folder string) (LintErrors, err
} else {
errorsChain <- nil
}

if wasmModule != nil {
wasmModule.Close(ctx)

Check warning on line 82 in internal/phplint/lint.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Unhandled error

Unhandled error
}
} else {
wasmModule.Close(ctx)

Check warning on line 85 in internal/phplint/lint.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Unhandled error

Unhandled error
errorsChain <- nil
}
}(file)
Expand Down

0 comments on commit e015cce

Please sign in to comment.