Refactors API to ensure context propagation (#482)

This is an API breaking change that does a few things:

* Stop encouraging practice that can break context propagation:
  * Stops caching `context.Context` in `wazero.RuntimeConfig`
  * Stops caching `context.Context` in `api.Module`

* Fixes context propagation in function calls:
  * Changes `api.Function`'s arg0 from `api.Module` to `context.Context`
  * Adds `context.Context` parameter in instantiation (propagates to
    .start)

* Allows context propagation for heavy operations like compile:
  * Adds `context.Context` as the initial parameter of `CompileModule`

The design we had earlier was a good start, but this is the only way to
ensure coherence when users start correlating or tracing. While adding a
`context.Context` parameter may seem difficult, wazero is a low-level
library and WebAssembly is notoriously difficult to troubleshoot. In
other words, it will be easier to explain to users to pass (even nil) as
the context parameter vs try to figure out things without coherent
context.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-04-19 16:52:57 +08:00
committed by GitHub
parent 64d7379ad0
commit 45ccab589b
42 changed files with 592 additions and 607 deletions

View File

@@ -1,6 +1,7 @@
package wazeroir
import (
"context"
"testing"
"github.com/tetratelabs/wazero/api"
@@ -9,6 +10,9 @@ import (
"github.com/tetratelabs/wazero/internal/wasm/text"
)
// ctx is an arbitrary, non-default context.
var ctx = context.WithValue(context.Background(), struct{}{}, "arbitrary")
var (
f64, i32 = wasm.ValueTypeF64, wasm.ValueTypeI32
i32_i32 = &wasm.FunctionType{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}}
@@ -65,7 +69,7 @@ func TestCompile(t *testing.T) {
enabledFeatures = wasm.FeaturesFinished
}
res, err := CompileFunctions(enabledFeatures, tc.module)
res, err := CompileFunctions(ctx, enabledFeatures, tc.module)
require.NoError(t, err)
require.Equal(t, tc.expected, res[0])
})
@@ -395,7 +399,7 @@ func TestCompile_MultiValue(t *testing.T) {
if enabledFeatures == 0 {
enabledFeatures = wasm.FeaturesFinished
}
res, err := CompileFunctions(enabledFeatures, tc.module)
res, err := CompileFunctions(ctx, enabledFeatures, tc.module)
require.NoError(t, err)
require.Equal(t, tc.expected, res[0])
})
@@ -406,7 +410,7 @@ func requireCompilationResult(t *testing.T, enabledFeatures wasm.Features, expec
if enabledFeatures == 0 {
enabledFeatures = wasm.FeaturesFinished
}
res, err := CompileFunctions(enabledFeatures, module)
res, err := CompileFunctions(ctx, enabledFeatures, module)
require.NoError(t, err)
require.Equal(t, expected, res[0])
}