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

Compiled modules may share state #2336

Open
emcfarlane opened this issue Nov 6, 2024 · 0 comments
Open

Compiled modules may share state #2336

emcfarlane opened this issue Nov 6, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@emcfarlane
Copy link
Contributor

emcfarlane commented Nov 6, 2024

Describe the bug
Closing a CompiledModule will affect another CompiledModule if they are compiled to the same runtime using the same wasm bytes.

To Reproduce

package main

import (
	"context"
	"log"

	"github.com/tetratelabs/wazero"
)

func main() {
	ctx := context.Background()
	rt := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig())

	wasm := []byte{0, 'a', 's', 'm', 1, 0, 0, 0}
	compiled1, _ := rt.CompileModule(ctx, wasm) // Original.
	compiled2, _ := rt.CompileModule(ctx, wasm) // Duplicate.

	// Instantiating the module then closing the compiled module should not affect the other compiled module.
	if _, err := rt.InstantiateModule(ctx, compiled1, wazero.NewModuleConfig().WithName("foo")); err != nil {
		log.Fatal(err)
	}
	_ = compiled1.Close(ctx)

	// This compiled2 module should still be usable.
	if _, err := rt.InstantiateModule(ctx, compiled2, wazero.NewModuleConfig().WithName("bar")); err != nil {
		log.Fatal(err)
	}
	_ = compiled2.Close(ctx)
}

This causes the following error on the second log line:

source module must be compiled before instantiation

main...emcfarlane:wazero:ed/dupCompCache

Expected behavior
The CompiledModule should still be able to be instantiated as the caller isn't aware that the module is shared between multiple instances.

Screenshots
N/A

Environment (please complete the relevant information):

  • Go version: go version go1.23.2 darwin/arm64
  • wazero Version: dc08732
  • Host architecture: arm64
  • Runtime mode: compiler

Additional context
The current behaviour makes it difficult to bound the number of active modules. The caller needs to manage access to the CompiledModule and must infer the cache key (ModuleID could be provided by CompiledModule interface?). To Close a compiled module shared between multiple viewers we must wait for all viewers to go away and then syncronize the call of Close to happen before anymore calls to CompileModule to avoid a race.

Related issues #1913

@emcfarlane emcfarlane added the bug Something isn't working label Nov 6, 2024
@emcfarlane emcfarlane reopened this Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant