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

Add ability to cancel wasm execution based on context #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ type (
Stdout io.Writer
// Stderr is the writer WASI uses for `fd_write` to file descriptor 2.
Stderr io.Writer
// CloseOnContextDone ensures the executions of functions to be closed under one of the following circumstances:
//
// - context.Context passed to the Call method of api.Function is canceled during execution. (i.e. ctx by context.WithCancel)
// - context.Context passed to the Call method of api.Function reaches timeout during execution. (i.e. ctx by context.WithTimeout or context.WithDeadline)
// - Close or CloseWithExitCode of api.Module is explicitly called during execution.
//
// This is especially useful when one wants to run untrusted Wasm binaries since otherwise, any invocation of
// api.Function can potentially block the corresponding Goroutine forever. Moreover, it might block the
// entire underlying OS thread which runs the api.Function call. See "Why it's safe to execute runtime-generated
// machine codes against async Goroutine preemption" section in internal/engine/compiler/RATIONALE.md for detail.
CloseOnContextDone bool
}

// Module is a WebAssembly Module.
Expand Down
14 changes: 11 additions & 3 deletions engines/wazero/wazero.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,17 @@ func DefaultRuntime(ctx context.Context) (wazero.Runtime, error) {

// New implements the same method as documented on wapc.Engine.
func (e *engine) New(ctx context.Context, host wapc.HostCallHandler, guest []byte, config *wapc.ModuleConfig) (mod wapc.Module, err error) {
r, err := e.newRuntime(ctx)
if err != nil {
return nil, err
//runtime conf changes
var r wazero.Runtime
if config.CloseOnContextDone {
runtimeConfig := wazero.NewRuntimeConfig().WithCloseOnContextDone(true)
r = wazero.NewRuntimeWithConfig(ctx, runtimeConfig)
wasi_snapshot_preview1.MustInstantiate(ctx, r)
} else {
r, err = e.newRuntime(ctx)
if err != nil {
return nil, err
}
}

m := &Module{runtime: r, wapcHostCallHandler: host}
Expand Down
Loading