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: check module IsClosed before a function call, not after #98

Merged
merged 1 commit into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 4 additions & 5 deletions extism.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ func (p *Plugin) Call(name string, data []byte) (uint32, []byte, error) {

// Call a function by name with the given input and context, returning the output
func (p *Plugin) CallWithContext(ctx context.Context, name string, data []byte) (uint32, []byte, error) {
if p.mainModule.IsClosed() {
return 0, nil, fmt.Errorf("module is closed")
}

ctx = context.WithValue(ctx, PluginCtxKey("extism"), p.extism)
if p.Timeout > 0 {
var cancel context.CancelFunc
Expand Down Expand Up @@ -497,11 +501,6 @@ func (p *Plugin) CallWithContext(ctx context.Context, name string, data []byte)
exitCode := exitErr.ExitCode()

if exitCode == 0 {
// It's possible for the function to return 0 as an error code, even
// if the module is closed.
if p.mainModule.IsClosed() {
return 0, nil, fmt.Errorf("module is closed")
}
err = nil
}

Expand Down
17 changes: 17 additions & 0 deletions extism_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,23 @@ func TestCountVowels(t *testing.T) {
}
}

func TestCountVowelsStdGo(t *testing.T) {
manifest := manifest("std_command.wasm")

if plugin, ok := pluginInstance(t, manifest); ok {
defer plugin.Close(context.Background())

exit, output, err := plugin.Call("_start", []byte("ben"))

result := string(output)
if assertCall(t, err, exit) {
expected := "hello ben"

assert.Equal(t, expected, result)
}
}
}

func TestMultipleCallsOutput(t *testing.T) {
manifest := manifest("count_vowels.wasm")

Expand Down
2 changes: 2 additions & 0 deletions plugins/std_command/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# standard go compiler, command module
GOOS=wasip1 GOARCH=wasm go build -tags std -o ../../wasm/std_command.wasm .
5 changes: 5 additions & 0 deletions plugins/std_command/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/extism/extism-sdk-plugins-count-vowels

go 1.23.6

require github.com/extism/go-pdk v1.1.1
2 changes: 2 additions & 0 deletions plugins/std_command/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/extism/go-pdk v1.1.1 h1:c8twD4RXAgh5Q3bgQ4srBLnVJ1OJK1ZlKhD/0P/pbxA=
github.com/extism/go-pdk v1.1.1/go.mod h1:Gz+LIU/YCKnKXhgge8yo5Yu1F/lbv7KtKFkiCSzW/P4=
14 changes: 14 additions & 0 deletions plugins/std_command/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build std
// +build std

package main

import (
"github.com/extism/go-pdk"
)

func main() {
input := pdk.InputString()

pdk.OutputString("hello " + input)
}
Binary file added wasm/count_vowels_std.wasm
Binary file not shown.
Binary file added wasm/fs_std.wasm
Binary file not shown.
Binary file added wasm/std_command.wasm
Binary file not shown.