Skip to content

Commit b064e7d

Browse files
committed
fix: make sure we instantiate non-main modules
1 parent 9525604 commit b064e7d

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

plugin.go

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1+
// new
12
package extism
23

34
import (
45
"context"
56
"errors"
67
"fmt"
7-
observe "github.com/dylibso/observe-sdk/go"
8-
"github.com/tetratelabs/wazero"
9-
"github.com/tetratelabs/wazero/api"
10-
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
118
"os"
129
"strconv"
1310
"strings"
1411
"sync/atomic"
1512
"time"
13+
14+
observe "github.com/dylibso/observe-sdk/go"
15+
"github.com/tetratelabs/wazero"
16+
"github.com/tetratelabs/wazero/api"
17+
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
1618
)
1719

1820
type CompiledPlugin struct {
1921
runtime wazero.Runtime
2022
main wazero.CompiledModule
2123
extism wazero.CompiledModule
2224
env api.Module
25+
modules map[string]wazero.CompiledModule
2326

2427
// when a module (main) is instantiated, it may have a module name that's added
2528
// to the data section of the wasm. If this is the case, we won't be able to
@@ -120,6 +123,7 @@ func NewCompiledPlugin(
120123
observeAdapter: config.ObserveAdapter,
121124
observeOptions: config.ObserveOptions,
122125
enableHttpResponseHeaders: config.EnableHttpResponseHeaders,
126+
modules: make(map[string]wazero.CompiledModule),
123127
}
124128

125129
if config.EnableWasi {
@@ -158,19 +162,18 @@ func NewCompiledPlugin(
158162
// - If there is only one module in the manifest then that is the main module by default
159163
// - Otherwise the last module listed is the main module
160164

161-
modules := map[string]wazero.CompiledModule{}
162165
for i, wasm := range manifest.Wasm {
163166
data, err := wasm.ToWasmData(ctx)
164167
if err != nil {
165168
return nil, err
166169
}
167170

168-
_, mainExists := modules["main"]
171+
_, mainExists := p.modules["main"]
169172
if data.Name == "" || i == len(manifest.Wasm)-1 && !mainExists {
170173
data.Name = "main"
171174
}
172175

173-
_, okm := modules[data.Name]
176+
_, okm := p.modules[data.Name]
174177

175178
if data.Name == "extism:host/env" || okm {
176179
return nil, fmt.Errorf("module name collision: '%s'", data.Name)
@@ -194,7 +197,7 @@ func NewCompiledPlugin(
194197
if data.Name == "main" {
195198
p.main = m
196199
} else {
197-
modules[data.Name] = m
200+
p.modules[data.Name] = m
198201
}
199202
}
200203

@@ -251,8 +254,6 @@ func (p *CompiledPlugin) Instance(ctx context.Context, config PluginInstanceConf
251254
if err != nil {
252255
return nil, fmt.Errorf("failed to initialize Observe Adapter: %v", err)
253256
}
254-
255-
trace.Finish()
256257
}
257258

258259
// Compile and instantiate the extism runtime. This runtime is stateful and needs to be
@@ -266,6 +267,20 @@ func (p *CompiledPlugin) Instance(ctx context.Context, config PluginInstanceConf
266267
}
267268
closers = append(closers, extism.Close)
268269

270+
// Instantiate all non-main modules first
271+
instancedModules := make(map[string]api.Module)
272+
for name, module := range p.modules {
273+
instance, err := p.runtime.InstantiateModule(ctx, module, moduleConfig.WithName(name))
274+
if err != nil {
275+
for _, m := range instancedModules {
276+
m.Close(ctx)
277+
}
278+
return nil, fmt.Errorf("instantiating module %s: %w", name, err)
279+
}
280+
instancedModules[name] = instance
281+
closers = append(closers, instance.Close)
282+
}
283+
269284
main, err := p.runtime.InstantiateModule(ctx, p.main, moduleConfig)
270285
if err != nil {
271286
return nil, fmt.Errorf("instantiating module: %w", err)

0 commit comments

Comments
 (0)