Skip to content

Commit d97bb62

Browse files
committed
Try to fix SONAME...
1 parent 74b3b58 commit d97bb62

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

.github/workflows/CI.yml

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ jobs:
142142
git config --global url."ssh://git@github.com-http-handler/platformatic/http-handler.git".insteadOf "ssh://git@github.com/platformatic/http-handler.git"
143143
git config --global url."ssh://git@github.com-http-rewriter/platformatic/http-rewriter.git".insteadOf "ssh://git@github.com/platformatic/http-rewriter.git"
144144
145+
# Set Python-related environment variables for the build
146+
export PYO3_PYTHON=$(which python3)
147+
export PYTHON_SYS_EXECUTABLE=$(which python3)
148+
145149
${{ matrix.settings.build }}
146150
- name: Build
147151
run: ${{ matrix.settings.build }}
@@ -326,15 +330,94 @@ jobs:
326330
uses: addnab/docker-run-action@v3
327331
with:
328332
image: ${{ steps.docker.outputs.IMAGE }}
329-
options: -v ${{ steps.docker.outputs.PNPM_STORE_PATH }}:${{ steps.docker.outputs.PNPM_STORE_PATH }} -v ${{ github.workspace }}:${{ github.workspace }} -w ${{ github.workspace }} --platform ${{ steps.docker.outputs.PLATFORM }}
333+
options: -v ${{ steps.docker.outputs.PNPM_STORE_PATH }}:${{ steps.docker.outputs.PNPM_STORE_PATH }} -v ${{ github.workspace }}:${{ github.workspace }} -w ${{ github.workspace }} --platform ${{ steps.docker.outputs.PLATFORM }} -e CI=true -e GITHUB_ACTIONS=true
330334
run: |
331335
# Install Python 3.9+ (any version will work with abi3-py39)
332336
apt-get update -y
333-
apt-get install -y python3 python3-dev
337+
apt-get install -y python3 python3-dev patchelf
338+
339+
echo "=== Starting test setup ==="
340+
echo "Current directory: $(pwd)"
341+
echo "Python version: $(python3 --version)"
342+
echo "Patchelf version: $(patchelf --version)"
343+
echo "CI environment: CI=$CI, GITHUB_ACTIONS=$GITHUB_ACTIONS"
344+
345+
# Check what .node files exist
346+
echo "=== Available .node files ==="
347+
ls -la *.node || echo "No .node files found"
348+
349+
# Check what Python version the binary was built with and patch SONAME if needed
350+
echo "=== Checking .node file Python dependencies ==="
351+
for file in *.node; do
352+
if [ -f "$file" ]; then
353+
case "$file" in
354+
*linux*)
355+
echo "Checking $file..."
356+
echo "Python dependencies before patching:"
357+
ldd "$file" 2>/dev/null | grep python || echo "No Python dependencies found"
358+
359+
# Check if we need to patch SONAME
360+
current_python_lib=$(ldd "$file" 2>/dev/null | grep "libpython" | head -1 | awk '{print $1}')
361+
if [ -n "$current_python_lib" ]; then
362+
echo "Current Python library: $current_python_lib"
363+
364+
# Find the actual Python library on the system
365+
system_python_lib=$(find /usr/lib* -name "libpython3*.so.*" -type f 2>/dev/null | head -1)
366+
if [ -n "$system_python_lib" ]; then
367+
system_python_soname=$(basename "$system_python_lib")
368+
echo "System Python library: $system_python_soname"
369+
370+
# Only patch if they're different
371+
if [ "$current_python_lib" != "$system_python_soname" ]; then
372+
echo "Patching SONAME from $current_python_lib to $system_python_soname"
373+
patchelf --replace-needed "$current_python_lib" "$system_python_soname" "$file"
374+
echo "SONAME patching completed"
375+
else
376+
echo "SONAME already matches system Python"
377+
fi
378+
else
379+
echo "Warning: Could not find system Python library"
380+
fi
381+
else
382+
echo "No Python library dependency found"
383+
fi
384+
385+
echo "Python dependencies after patching:"
386+
ldd "$file" 2>/dev/null | grep python || echo "No Python dependencies found"
387+
echo "---"
388+
;;
389+
*)
390+
echo "Skipping non-Linux file: $file"
391+
;;
392+
esac
393+
fi
394+
done
334395
335396
# Install pnpm and run tests
397+
echo "=== Installing pnpm ==="
336398
corepack disable
337399
npm i -gf pnpm
400+
401+
echo "=== Running pnpm install ==="
402+
# Should be non-interactive in CI environment
403+
pnpm install --prefer-offline
404+
405+
echo "=== Setting up Python library path ==="
406+
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
407+
echo "LD_LIBRARY_PATH: $LD_LIBRARY_PATH"
408+
409+
# Find and preload the Python library
410+
echo "=== Finding Python library to preload ==="
411+
PYTHON_LIB=$(find /usr/lib* -name "libpython3*.so.*" -type f 2>/dev/null | head -1)
412+
if [ -n "$PYTHON_LIB" ]; then
413+
echo "Found Python library: $PYTHON_LIB"
414+
export LD_PRELOAD=$PYTHON_LIB
415+
echo "LD_PRELOAD: $LD_PRELOAD"
416+
else
417+
echo "Warning: Could not find Python library to preload"
418+
fi
419+
420+
echo "=== Running tests ==="
338421
pnpm test
339422
340423
publish:

build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@ extern crate napi_build;
44
fn main() {
55
#[cfg(feature = "napi-support")]
66
napi_build::setup();
7+
8+
// For Linux targets, ensure we link Python properly
9+
#[cfg(target_os = "linux")]
10+
{
11+
println!("cargo:rustc-link-arg=-Wl,-export-dynamic");
12+
println!("cargo:rustc-link-arg=-Wl,--no-as-needed");
13+
}
714
}

src/asgi/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ impl Asgi {
4747
docroot: Option<String>,
4848
app_target: Option<PythonHandlerTarget>,
4949
) -> Result<Self, HandlerError> {
50+
// Ensure Python is initialized
51+
pyo3::prepare_freethreaded_python();
52+
5053
// Determine document root
5154
let docroot = PathBuf::from(if let Some(docroot) = docroot {
5255
docroot
@@ -83,6 +86,14 @@ impl Asgi {
8386
let module_name = CString::new(target.file.clone()).map_err(HandlerError::StringCovertError)?;
8487

8588
Python::with_gil(|py| -> PyResult<PyObject> {
89+
// Ensure sys.path includes the docroot
90+
let sys = py.import("sys")?;
91+
let path = sys.getattr("path")?;
92+
path.call_method1("insert", (0, docroot.to_string_lossy()))?;
93+
94+
// Import asyncio to ensure it's loaded before we need it
95+
py.import("asyncio")?;
96+
8697
let module = PyModule::from_code(py, &code, &file_name, &module_name)?;
8798
Ok(module.getattr(&target.function)?.unbind())
8899
})

0 commit comments

Comments
 (0)