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
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,47 @@ jobs:
dist/bin/*
dist/pulsing-*.tar.gz

# ============================================
# 构建 Windows Wheel(与发布路径保持同一编译/链接覆盖)
# ============================================
build-windows:
name: Build (Windows wheel + binary)
runs-on: windows-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable

- name: Build wheels
uses: PyO3/maturin-action@v1
with:
args: --release --out dist
sccache: 'true'

- name: Build pulsing binary
shell: bash
run: bash scripts/build-binary.sh --release --package

- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-windows-ci
path: dist/*.whl

- name: Upload binary
uses: actions/upload-artifact@v4
with:
name: bin-windows-ci
path: |
dist/bin/*
dist/pulsing-*.zip

# ============================================
# 构建 Linux x86-64 (wheel + binary)
# ============================================
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

本文档记录 Pulsing 的版本范围和重要兼容性变化。本文档与仓库固定提交、发布说明和实际 wheel 验证结果配合使用。

## [0.1.6] - 2026-11-30

### Fixed

- Windows 的 `abi3-py310` 构建不再链接 Python 3.11 才进入 stable ABI 的 buffer 符号;Windows tensor buffer 通过 Python `bytes()` 显式拷贝,Unix 保留现有零拷贝路径。
- CI 增加 Windows wheel 和 binary 构建,在合并前覆盖发布所用的编译与链接路径。

## [0.1.5] - 2026-11-30

### Changed
Expand Down
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ resolver = "3"
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage_nightly)'] }

[workspace.package]
version = "0.1.5"
version = "0.1.6"
edition = "2021"
description = "Pulsing - Distributed Actor Framework"
authors = ["Reiase <reiase@gmail.com>"]
Expand Down
220 changes: 126 additions & 94 deletions crates/pulsing-py/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyDict};
use serde::{Deserialize, Serialize};
use std::cmp::min;
#[cfg(not(windows))]
use std::ffi::{c_char, c_int, c_void};
use std::net::SocketAddr;
#[cfg(not(windows))]
use std::ptr;
#[cfg(not(windows))]
use std::slice;
use std::sync::Arc;
use std::sync::Mutex as StdMutex;
Expand Down Expand Up @@ -416,108 +419,117 @@ impl PyMessage {
}
}

/// Holds a Python buffer export for as long as `Bytes` references it.
///
/// Safety contract: callers must not mutate or resize the source buffer until
/// the send completes. PulsingQueue prepares immutable contiguous CPU buffers
/// before constructing TensorMessage.
struct PythonBufferOwner {
view: Box<RawPyBuffer>,
}

// Py_buffer entered Python's Stable ABI in Python 3.11, while Pulsing still
// builds an abi3-py310 extension. CPython 3.10 nevertheless exposes the same
// public C buffer ABI, so declare the two functions locally instead of
// disabling abi3 or silently copying through PyBytes.
#[repr(C)]
struct RawPyBuffer {
buf: *mut c_void,
obj: *mut c_void,
len: isize,
itemsize: isize,
readonly: c_int,
ndim: c_int,
format: *mut c_char,
shape: *mut isize,
strides: *mut isize,
suboffsets: *mut isize,
internal: *mut c_void,
}
#[cfg(not(windows))]
mod raw_buffer_owner {
use super::*;

impl RawPyBuffer {
fn new() -> Self {
Self {
buf: ptr::null_mut(),
obj: ptr::null_mut(),
len: 0,
itemsize: 0,
readonly: 0,
ndim: 0,
format: ptr::null_mut(),
shape: ptr::null_mut(),
strides: ptr::null_mut(),
suboffsets: ptr::null_mut(),
internal: ptr::null_mut(),
/// Holds a Python buffer export for as long as `Bytes` references it.
///
/// Safety contract: callers must not mutate or resize the source buffer until
/// the send completes. PulsingQueue prepares immutable contiguous CPU buffers
/// before constructing TensorMessage.
pub(crate) struct PythonBufferOwner {
view: Box<RawPyBuffer>,
}

// Py_buffer entered Python's Stable ABI in Python 3.11, while Pulsing still
// builds an abi3-py310 extension. CPython 3.10 nevertheless exposes the same
// public C buffer ABI, so declare the two functions locally instead of
// disabling abi3 or silently copying through PyBytes.
#[repr(C)]
struct RawPyBuffer {
buf: *mut c_void,
obj: *mut c_void,
len: isize,
itemsize: isize,
readonly: c_int,
ndim: c_int,
format: *mut c_char,
shape: *mut isize,
strides: *mut isize,
suboffsets: *mut isize,
internal: *mut c_void,
}

impl RawPyBuffer {
fn new() -> Self {
Self {
buf: ptr::null_mut(),
obj: ptr::null_mut(),
len: 0,
itemsize: 0,
readonly: 0,
ndim: 0,
format: ptr::null_mut(),
shape: ptr::null_mut(),
strides: ptr::null_mut(),
suboffsets: ptr::null_mut(),
internal: ptr::null_mut(),
}
}
}
}

unsafe extern "C" {
fn PyObject_GetBuffer(obj: *mut c_void, view: *mut RawPyBuffer, flags: c_int) -> c_int;
fn PyBuffer_Release(view: *mut RawPyBuffer);
}
unsafe extern "C" {
fn PyObject_GetBuffer(obj: *mut c_void, view: *mut RawPyBuffer, flags: c_int) -> c_int;
fn PyBuffer_Release(view: *mut RawPyBuffer);
}

const PYBUF_SIMPLE: c_int = 0;

// CPython buffer exports may be moved between the Python executor and Tokio
// send task. The exporter remains pinned by `view.obj`; callers must obey the
// documented no-mutation-while-sending contract.
unsafe impl Send for PythonBufferOwner {}

const PYBUF_SIMPLE: c_int = 0;

// CPython buffer exports may be moved between the Python executor and Tokio
// send task. The exporter remains pinned by `view.obj`; callers must obey the
// documented no-mutation-while-sending contract.
unsafe impl Send for PythonBufferOwner {}

impl PythonBufferOwner {
fn acquire(item: &Bound<'_, pyo3::PyAny>) -> PyResult<Self> {
let mut view = Box::new(RawPyBuffer::new());
// SAFETY: `view` points to writable Py_buffer storage and `item` is a
// live Python object. On success CPython gives view.obj an owned ref.
let result = unsafe { PyObject_GetBuffer(item.as_ptr().cast(), &mut *view, PYBUF_SIMPLE) };
if result != 0 {
return Err(PyErr::fetch(item.py()));
impl PythonBufferOwner {
pub(crate) fn acquire(item: &Bound<'_, pyo3::PyAny>) -> PyResult<Self> {
let mut view = Box::new(RawPyBuffer::new());
// SAFETY: `view` points to writable Py_buffer storage and `item` is a
// live Python object. On success CPython gives view.obj an owned ref.
let result =
unsafe { PyObject_GetBuffer(item.as_ptr().cast(), &mut *view, PYBUF_SIMPLE) };
if result != 0 {
return Err(PyErr::fetch(item.py()));
}
if view.len < 0 || (view.len > 0 && view.buf.is_null()) {
// SAFETY: acquisition succeeded, so it must be released exactly once.
unsafe { PyBuffer_Release(&mut *view) };
return Err(PyValueError::new_err("Invalid Python buffer export"));
}
Ok(Self { view })
}
if view.len < 0 || (view.len > 0 && view.buf.is_null()) {
// SAFETY: acquisition succeeded, so it must be released exactly once.
unsafe { PyBuffer_Release(&mut *view) };
return Err(PyValueError::new_err("Invalid Python buffer export"));

pub(crate) fn len(&self) -> usize {
self.view.len as usize
}
Ok(Self { view })
}

fn len(&self) -> usize {
self.view.len as usize
impl AsRef<[u8]> for PythonBufferOwner {
fn as_ref(&self) -> &[u8] {
let len = self.len();
if len == 0 {
return &[];
}
// SAFETY: PyBuffer owns an active CPython buffer export, so its pointer
// remains valid until this owner is dropped by Bytes. The constructor
// only accepts C-contiguous byte views.
unsafe { slice::from_raw_parts(self.view.buf.cast::<u8>(), len) }
}
}
}

impl AsRef<[u8]> for PythonBufferOwner {
fn as_ref(&self) -> &[u8] {
let len = self.len();
if len == 0 {
return &[];
impl Drop for PythonBufferOwner {
fn drop(&mut self) {
Python::with_gil(|_| {
// SAFETY: this owner is constructed only after a successful
// PyObject_GetBuffer and owns the one matching release.
unsafe { PyBuffer_Release(&mut *self.view) };
});
}
// SAFETY: PyBuffer owns an active CPython buffer export, so its pointer
// remains valid until this owner is dropped by Bytes. The constructor
// only accepts C-contiguous byte views.
unsafe { slice::from_raw_parts(self.view.buf.cast::<u8>(), len) }
}
}

impl Drop for PythonBufferOwner {
fn drop(&mut self) {
Python::with_gil(|_| {
// SAFETY: this owner is constructed only after a successful
// PyObject_GetBuffer and owns the one matching release.
unsafe { PyBuffer_Release(&mut *self.view) };
});
}
}
#[cfg(not(windows))]
use raw_buffer_owner::PythonBufferOwner;

/// Python owner used behind a ctypes array for received Rust `Bytes`.
///
Expand Down Expand Up @@ -569,17 +581,37 @@ fn normalize_tensor_buffer(
PyValueError::new_err("TensorMessage.buffers items must support a contiguous byte view")
})?
};
let buffer = PythonBufferOwner::acquire(&byte_view).map_err(|_| {
PyValueError::new_err("TensorMessage.buffers items must expose a contiguous byte buffer")
})?;
let len = buffer.len();
drop(buffer);
#[cfg(not(windows))]
let len = {
let buffer = PythonBufferOwner::acquire(&byte_view).map_err(|_| {
PyValueError::new_err(
"TensorMessage.buffers items must expose a contiguous byte buffer",
)
})?;
buffer.len()
};
#[cfg(windows)]
let len = byte_view.len()?;
Ok((byte_view.unbind(), len))
}

fn python_buffer_to_bytes(item: &PyObject, py: Python<'_>) -> PyResult<Bytes> {
let owner = PythonBufferOwner::acquire(item.bind(py))?;
Ok(Bytes::from_owner(owner))
#[cfg(not(windows))]
{
let owner = PythonBufferOwner::acquire(item.bind(py))?;
Ok(Bytes::from_owner(owner))
}
#[cfg(windows)]
{
// Py_buffer entered the stable ABI after Python 3.10. Use one explicit
// copy on Windows rather than linking version-specific Python symbols.
let bytes = py
.import("builtins")?
.getattr("bytes")?
.call1((item.bind(py),))?;
let data = bytes.extract::<&[u8]>()?;
Ok(Bytes::copy_from_slice(data))
}
}

/// Create a Python buffer exporter with explicit ownership semantics.
Expand Down
2 changes: 1 addition & 1 deletion docs/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pulsing-docs"
version = "0.1.5"
version = "0.1.6"
description = "Documentation for Pulsing - Backbone for Distributed AI Systems"
readme = "README.md"
requires-python = ">=3.10"
Expand Down
Loading
Loading