Skip to content

Commit

Permalink
fix: check module IsClosed before a function call, not after (#98)
Browse files Browse the repository at this point in the history
Fixes #95

As far as i can think about it, this check should be before a function
call. Because it's okay for command modules to call `proc_exit` (and
thus closing the module). From my understanding command modules are
one-time use anyway
  • Loading branch information
mhmd-azeez authored Mar 2, 2025
1 parent c3b5de6 commit b1e59a8
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 5 deletions.
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.

0 comments on commit b1e59a8

Please sign in to comment.