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:
@@ -344,7 +344,7 @@ func TestNewModuleBuilder_Build(t *testing.T) {
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
b := tc.input(NewRuntime()).(*moduleBuilder)
|
||||
m, err := b.Build()
|
||||
m, err := b.Build(testCtx)
|
||||
require.NoError(t, err)
|
||||
|
||||
requireHostModuleEquals(t, tc.expected, m.module)
|
||||
@@ -352,7 +352,7 @@ func TestNewModuleBuilder_Build(t *testing.T) {
|
||||
require.Equal(t, b.r.store.Engine, m.compiledEngine)
|
||||
|
||||
// Built module must be instantiable by Engine.
|
||||
_, err = b.r.InstantiateModule(m)
|
||||
_, err = b.r.InstantiateModule(testCtx, m)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
@@ -385,7 +385,7 @@ func TestNewModuleBuilder_Build_Errors(t *testing.T) {
|
||||
tc := tt
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, e := tc.input(NewRuntime()).Build()
|
||||
_, e := tc.input(NewRuntime()).Build(testCtx)
|
||||
require.EqualError(t, e, tc.expectedErr)
|
||||
})
|
||||
}
|
||||
@@ -394,7 +394,7 @@ func TestNewModuleBuilder_Build_Errors(t *testing.T) {
|
||||
// TestNewModuleBuilder_Instantiate ensures Runtime.InstantiateModule is called on success.
|
||||
func TestNewModuleBuilder_Instantiate(t *testing.T) {
|
||||
r := NewRuntime()
|
||||
m, err := r.NewModuleBuilder("env").Instantiate()
|
||||
m, err := r.NewModuleBuilder("env").Instantiate(testCtx)
|
||||
require.NoError(t, err)
|
||||
|
||||
// If this was instantiated, it would be added to the store under the same name
|
||||
@@ -404,10 +404,10 @@ func TestNewModuleBuilder_Instantiate(t *testing.T) {
|
||||
// TestNewModuleBuilder_Instantiate_Errors ensures errors propagate from Runtime.InstantiateModule
|
||||
func TestNewModuleBuilder_Instantiate_Errors(t *testing.T) {
|
||||
r := NewRuntime()
|
||||
_, err := r.NewModuleBuilder("env").Instantiate()
|
||||
_, err := r.NewModuleBuilder("env").Instantiate(testCtx)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = r.NewModuleBuilder("env").Instantiate()
|
||||
_, err = r.NewModuleBuilder("env").Instantiate(testCtx)
|
||||
require.EqualError(t, err, "module env has already been instantiated")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user