Skip to content

Commit 00e7729

Browse files
authored
fix crash on Windows; add version embedding (#17)
This fixes an issue causing `wizer` to fail due to `CPython` calling `fd_filestat_get` on stdio descriptors, which causes Wasmtime's WASI implementation to trap if those descriptors don't point to actual files. This also updates the `spin-sdk` dependency to allow version embedding per https://github.com/fermyon/spin/blob/main/docs/content/sips/011-component-versioning.md. Signed-off-by: Joel Dice <[email protected]>
1 parent 2a9ba2f commit 00e7729

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

crates/spin-python-cli/src/main.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use {
55
clap::Parser,
66
std::{
77
env, fs,
8-
io::Cursor,
8+
io::{self, Cursor, Seek},
99
path::{Path, PathBuf},
1010
process::Command,
1111
str,
@@ -105,6 +105,17 @@ fn main() -> Result<()> {
105105
)
106106
}
107107

108+
// Spawn a subcommand to do the real work. This gives us an opportunity to clear the environment so that
109+
// build-time environment variables don't end up in the Wasm module we're building.
110+
//
111+
// Note that we need to use temporary files for stdio instead of the default inheriting behavior since (as
112+
// of this writing) CPython interacts poorly with Wasmtime's WASI implementation if any of the stdio
113+
// descriptors point to non-files on Windows. Specifically, the WASI implementation will trap when CPython
114+
// calls `fd_filestat_get` on non-files.
115+
116+
let mut stdout = tempfile::tempfile()?;
117+
let mut stderr = tempfile::tempfile()?;
118+
108119
let status = Command::new(env::args().next().unwrap())
109120
.env_clear()
110121
.env("SPIN_PYTHON_WIZEN", "1")
@@ -116,9 +127,18 @@ fn main() -> Result<()> {
116127
)
117128
.arg(&python_path)
118129
.arg(&options.output)
130+
.stdin(tempfile::tempfile()?)
131+
.stdout(stdout.try_clone()?)
132+
.stderr(stderr.try_clone()?)
119133
.status()?;
120134

121135
if !status.success() {
136+
stdout.rewind()?;
137+
io::copy(&mut stdout, &mut io::stdout().lock())?;
138+
139+
stderr.rewind()?;
140+
io::copy(&mut stderr, &mut io::stderr().lock())?;
141+
122142
bail!("Couldn't create wasm from input");
123143
}
124144

crates/spin-python-engine/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ crate-type = [ "cdylib" ]
1111
anyhow = "1"
1212
bytes = { version = "1.2.1", features = ["serde"] }
1313
http = "0.2"
14-
spin-sdk = { git = "https://github.com/fermyon/spin" }
14+
spin-sdk = { git = "https://github.com/fermyon/spin", default-features = false }
1515
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "dde4694aaa6acf9370206527a798ac4ba6a8c5b8" }
1616
pyo3 = { version = "0.17.3", features = [ "abi3-py310" ] }
1717
once_cell = "1.16.0"

crates/spin-python-engine/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ thread_local! {
2424
static ENVIRON: OnceCell<Py<PyMapping>> = OnceCell::new();
2525
}
2626

27+
#[export_name = "spin-sdk-language-python"]
28+
extern "C" fn __spin_sdk_language() {}
29+
2730
fn bytes(py: Python<'_>, src: &[u8]) -> PyResult<Py<PyBytes>> {
2831
Ok(PyBytes::new_with(py, src.len(), |dst| {
2932
dst.copy_from_slice(src);

0 commit comments

Comments
 (0)