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

fix: make sure we instantiate non-main modules #93

Merged
merged 11 commits into from
Jan 30, 2025
Prev Previous commit
Next Next commit
address PR feedback
  • Loading branch information
mhmd-azeez committed Jan 16, 2025
commit ddc688323bf54edcd08f340ed8fb4d3b0ea91442
24 changes: 18 additions & 6 deletions plugin.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also a line:

moduleConfig = moduleConfig.WithName(strconv.Itoa(int(p.instanceCount.Add(1))))

It seems to be redundant now + p.instanceCount also can be removed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, i am not sure, the comment for p.instanceCount suggests otherwise. @evacchi what do you think? I moved the naming closer to the main module initialization to make it clearer that this naming is for the main module

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, you are correct, InstantiateModule comment states that module name can't be reused, this probably also means that other modules can't be instantiated multiple times?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC not with the same name, within the same runtime

Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ func NewCompiledPlugin(
// - If there is only one module in the manifest then that is the main module by default
// - Otherwise the last module listed is the main module

foundMain := false
for i, wasm := range manifest.Wasm {
data, err := wasm.ToWasmData(ctx)
if err != nil {
return nil, err
}

_, mainExists := p.modules["main"]
if data.Name == "" || i == len(manifest.Wasm)-1 && !mainExists {
if data.Name == "" || i == len(manifest.Wasm)-1 && !foundMain {
data.Name = "main"
}

Expand All @@ -194,8 +194,10 @@ func NewCompiledPlugin(
if err != nil {
return nil, err
}

if data.Name == "main" {
p.main = m
foundMain = true
} else {
p.modules[data.Name] = m
}
Expand Down Expand Up @@ -223,7 +225,6 @@ func (p *CompiledPlugin) Instance(ctx context.Context, config PluginInstanceConf
if moduleConfig == nil {
moduleConfig = wazero.NewModuleConfig()
}
moduleConfig = moduleConfig.WithName(strconv.Itoa(int(p.instanceCount.Add(1))))

// NOTE: this is only necessary for guest modules because
// host modules have the same access privileges as the host itself
Expand Down Expand Up @@ -265,26 +266,35 @@ func (p *CompiledPlugin) Instance(ctx context.Context, config PluginInstanceConf
if err != nil {
return nil, fmt.Errorf("instantiating extism module: %w", err)
}

closers = append(closers, extism.Close)

// Instantiate all non-main modules first
instancedModules := make(map[string]api.Module)
for name, module := range p.modules {
instance, err := p.runtime.InstantiateModule(ctx, module, moduleConfig.WithName(name))
if err != nil {
for _, m := range instancedModules {
m.Close(ctx)
for _, closer := range closers {
closer(ctx)
}

return nil, fmt.Errorf("instantiating module %s: %w", name, err)
}

instancedModules[name] = instance
closers = append(closers, instance.Close)
}

main, err := p.runtime.InstantiateModule(ctx, p.main, moduleConfig)
mainModuleName := strconv.Itoa(int(p.instanceCount.Add(1)))
main, err := p.runtime.InstantiateModule(ctx, p.main, moduleConfig.WithName(mainModuleName))
if err != nil {
for _, closer := range closers {
closer(ctx)
}

return nil, fmt.Errorf("instantiating module: %w", err)
}

closers = append(closers, main.Close)

p.maxHttp = int64(1024 * 1024 * 50)
Expand All @@ -301,6 +311,7 @@ func (p *CompiledPlugin) Instance(ctx context.Context, config PluginInstanceConf
if p.enableHttpResponseHeaders {
headers = map[string]string{}
}

instance := &Plugin{
close: closers,
extism: extism,
Expand All @@ -321,5 +332,6 @@ func (p *CompiledPlugin) Instance(ctx context.Context, config PluginInstanceConf
traceCtx: trace,
}
instance.guestRuntime = detectGuestRuntime(ctx, instance)

return instance, nil
}
Loading