Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/go/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,10 @@ func {camel}({go_params}) {go_results} {{

(
if abi::guest_export_needs_post_return(resolve, func) {
format!("{PINNER} := &runtime.Pinner{{}}")
format!(
"{PINNER} := &runtime.Pinner{{}}
defer {PINNER}.Unpin()"
)
} else {
String::new()
},
Expand Down
35 changes: 35 additions & 0 deletions tests/runtime-async/async/return-string/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//@ wasmtime-flags = '-Wcomponent-model-async'

package export_wit_world

import (
"fmt"
"runtime"

test "wit_component/my_test_i"
)

/*

This tests for pinner leaks in generated Go code for async exported
function that return heap-allocated types (strings, lists, etc.). Without
`pinner.Unpin()`, the `runtime.Pinner` object goes out of scope after
the function returns with pinned pointers still alive. When GC finalizes
the Pinner, it panics:

```
panic: runtime error: runtime.Pinner: found leaking pinned pointer;
forgot to call Unpin()?
```
*/

func Run() {
// Perform a heap allocation
got := test.ReturnString()
if got != "hello" {
panic(fmt.Sprintf("expected \"hello\", got %q", got))
}

// Force GC to finalize any leaked Pinners
runtime.GC()
}
5 changes: 5 additions & 0 deletions tests/runtime-async/async/return-string/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package export_my_test_i

func ReturnString() string {
return "hello"
}
14 changes: 14 additions & 0 deletions tests/runtime-async/async/return-string/test.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package my:test;

interface i {
return-string: async func() -> string;
}

world test {
export i;
}

world runner {
import i;
export run: async func();
}
Loading