Skip to content

Commit 72ca2f5

Browse files
authored
fix memory leaks in generated bindings (#153)
* fix memory leaks in generated bindings This addresses a couple of issues: - We weren't freeing canonical ABI results after calling imports and then converting the results to Python values. - We were using the PyO3 `Bound` APIs incorrectly in a few places, resulting in refcounts being incremented too many times. While debugging the latter issue, I took the opportunity to update to the latest PyO3 release, which required addressing the usual API breakage. It also required temporarily forking the repo to add a patch to disable `extern "C-unwind"` in `pyo3_ffi` on `wasm32`. We should be able to drop that fork once we upgrade everything to Python 3.14. Finally, I discovered https://pyo3.rs/v0.25.0/features.html#pyo3_disable_reference_pool which supposedly optimizes Rust<->Python transitions by disabling a feature we don't rely on anyway, so I went ahead and enabled it. Fixes #152 Signed-off-by: Joel Dice <joel.dice@fermyon.com> * bump version to 0.17.1 Signed-off-by: Joel Dice <joel.dice@fermyon.com> * update python.rs to use new PyO3 API Signed-off-by: Joel Dice <joel.dice@fermyon.com> --------- Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent a63e030 commit 72ca2f5

File tree

16 files changed

+204
-157
lines changed

16 files changed

+204
-157
lines changed

Cargo.lock

Lines changed: 20 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "componentize-py"
3-
version = "0.17.0"
3+
version = "0.17.1"
44
edition = "2021"
55
exclude = ["cpython"]
66

@@ -22,7 +22,9 @@ wasmparser = "0.227.0"
2222
indexmap = "2.6.0"
2323
bincode = "1.3.3"
2424
heck = "0.5.0"
25-
pyo3 = { version = "0.22.5", features = [
25+
# TODO: switch back to upstream once we've updated to Python 3.14, at which
26+
# point the following patch will no longer be needed:
27+
pyo3 = { git = "https://github.com/dicej/pyo3", branch = "v0.25.0-no-wasm32-unwind", features = [
2628
"abi3-py39",
2729
"extension-module",
2830
], optional = true }

build.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,12 @@ fn package_all_the_things(out_dir: &Path) -> Result<()> {
124124
}
125125
}
126126

127-
cmd.env("RUSTFLAGS", "-C relocation-model=pic")
128-
.env("CARGO_TARGET_DIR", out_dir)
129-
.env("PYO3_CONFIG_FILE", out_dir.join("pyo3-config.txt"));
127+
cmd.env(
128+
"RUSTFLAGS",
129+
"-C relocation-model=pic --cfg pyo3_disable_reference_pool",
130+
)
131+
.env("CARGO_TARGET_DIR", out_dir)
132+
.env("PYO3_CONFIG_FILE", out_dir.join("pyo3-config.txt"));
130133

131134
let status = cmd.status()?;
132135
assert!(status.success());

examples/cli/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ run a Python-based component targetting the [wasi-cli] `command` world.
1010
## Prerequisites
1111

1212
* `Wasmtime` 26.0.0 or later
13-
* `componentize-py` 0.17.0
13+
* `componentize-py` 0.17.1
1414

1515
Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
1616
you don't have `cargo`, you can download and install from
1717
https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0.
1818

1919
```
2020
cargo install --version 26.0.0 wasmtime-cli
21-
pip install componentize-py==0.17.0
21+
pip install componentize-py==0.17.1
2222
```
2323

2424
## Running the demo

examples/http/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ run a Python-based component targetting the [wasi-http] `proxy` world.
1010
## Prerequisites
1111

1212
* `Wasmtime` 26.0.0 or later
13-
* `componentize-py` 0.17.0
13+
* `componentize-py` 0.17.1
1414

1515
Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
1616
you don't have `cargo`, you can download and install from
1717
https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0.
1818

1919
```
2020
cargo install --version 26.0.0 wasmtime-cli
21-
pip install componentize-py==0.17.0
21+
pip install componentize-py==0.17.1
2222
```
2323

2424
## Running the demo

examples/matrix-math/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ within a guest component.
1111
## Prerequisites
1212

1313
* `wasmtime` 26.0.0 or later
14-
* `componentize-py` 0.17.0
14+
* `componentize-py` 0.17.1
1515
* `NumPy`, built for WASI
1616

1717
Note that we use an unofficial build of NumPy since the upstream project does
@@ -23,7 +23,7 @@ https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0.
2323

2424
```
2525
cargo install --version 26.0.0 wasmtime-cli
26-
pip install componentize-py==0.17.0
26+
pip install componentize-py==0.17.1
2727
curl -OL https://github.com/dicej/wasi-wheels/releases/download/v0.0.1/numpy-wasi.tar.gz
2828
tar xf numpy-wasi.tar.gz
2929
```

examples/sandbox/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ sandboxed Python code snippets from within a Python app.
88
## Prerequisites
99

1010
* `wasmtime-py` 25.0.0 or later
11-
* `componentize-py` 0.17.0
11+
* `componentize-py` 0.17.1
1212

1313
```
14-
pip install componentize-py==0.17.0 wasmtime==25.0.0
14+
pip install componentize-py==0.17.1 wasmtime==25.0.0
1515
```
1616

1717
## Running the demo

examples/tcp/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ making an outbound TCP request using `wasi-sockets`.
1111
## Prerequisites
1212

1313
* `Wasmtime` 26.0.0 or later
14-
* `componentize-py` 0.17.0
14+
* `componentize-py` 0.17.1
1515

1616
Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
1717
you don't have `cargo`, you can download and install from
1818
https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0.
1919

2020
```
2121
cargo install --version 26.0.0 wasmtime-cli
22-
pip install componentize-py==0.17.0
22+
pip install componentize-py==0.17.1
2323
```
2424

2525
## Running the demo

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ features = ["pyo3/extension-module"]
77

88
[project]
99
name = "componentize-py"
10-
version = "0.17.0"
10+
version = "0.17.1"
1111
description = "Tool to package Python applications as WebAssembly components"
1212
readme = "README.md"
1313
license = { file = "LICENSE" }

runtime/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ crate-type = ["staticlib"]
99
[dependencies]
1010
anyhow = "1.0.91"
1111
once_cell = "1.20.2"
12-
pyo3 = { version = "0.22.5", features = ["abi3-py312", "num-bigint"] }
12+
# TODO: switch back to upstream once we've updated to Python 3.14, at which
13+
# point the following patch will no longer be needed:
14+
pyo3 = { git = "https://github.com/dicej/pyo3", branch = "v0.25.0-no-wasm32-unwind", features = ["abi3-py312", "num-bigint"] }
1315
componentize-py-shared = { path = "../shared" }
1416
num-bigint = "0.4.6"
1517
wit-bindgen = { version = "0.40.0", default-features = false, features = ["macros", "realloc"] }

0 commit comments

Comments
 (0)