-
Notifications
You must be signed in to change notification settings - Fork 80
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
refactor: Remove runtime.KeepAlive in config.go #148
Conversation
@alexcrichton This is only the demonstration. We can extend the change to other files under the same guidelines. Here |
Unfortunately I don't think that this PR is correct and all of the func (cfg *Config) SetWasmThreads(enabled bool) {
C.wasmtime_config_wasm_threads_set(cfg.ptr(), C.bool(enabled))
runtime.KeepAlive(cfg)
} This can't be removed because if The |
@alexcrichton I think in this case func (cfg *Config) SetWasmThreads(enabled bool) {
p := cfg.ptr()
runtime.GC()
C.wasmtime_config_wasm_threads_set(p, C.bool(enabled))
} As you can see
Seeing the discussion here, Ian said The other thing is method receiver |
Sorry I don't know what to say other than that post you're pointing out just isn't correct. If you apply this patch: diff --git a/config.go b/config.go
index 1290708..cd50eb3 100644
--- a/config.go
+++ b/config.go
@@ -44,13 +44,16 @@ const (
// Config holds options used to create an Engine and customize its behavior.
type Config struct {
- _ptr *C.wasm_config_t
+ _ptr *C.wasm_config_t
+ freed *bool
}
// NewConfig creates a new `Config` with all default options configured.
func NewConfig() *Config {
- config := &Config{_ptr: C.wasm_config_new()}
+ freed := false
+ config := &Config{_ptr: C.wasm_config_new(), freed: &freed}
runtime.SetFinalizer(config, func(config *Config) {
+ freed = true
C.wasm_config_delete(config._ptr)
})
return config
@@ -64,8 +67,13 @@ func (cfg *Config) SetDebugInfo(enabled bool) {
// SetWasmThreads configures whether the wasm threads proposal is enabled
func (cfg *Config) SetWasmThreads(enabled bool) {
- C.wasmtime_config_wasm_threads_set(cfg.ptr(), C.bool(enabled))
- runtime.KeepAlive(cfg)
+ freed := cfg.freed
+ ptr := cfg.ptr()
+ if *freed {
+ panic("bad")
+ }
+ C.wasmtime_config_wasm_threads_set(ptr, C.bool(enabled))
+ //runtime.KeepAlive(cfg)
}
// SetWasmReferenceTypes configures whether the wasm reference types proposal is enabled
diff --git a/config_test.go b/config_test.go
index d76c137..3b47dc4 100644
--- a/config_test.go
+++ b/config_test.go
@@ -27,3 +27,9 @@ func TestConfig(t *testing.T) {
err = NewConfig().CacheConfigLoad("nonexistent.toml")
require.Error(t, err)
}
+
+func TestConfig2(t *testing.T) {
+ for i := 0; i < 1000; i++ {
+ NewConfig().SetWasmThreads(true)
+ }
+} And test with
It's worth noting that anything dealing with a gc and trying to get a specific behavior is notoriously unreliable. I don't think Go provides a ton of guarantees around |
close #147