Skip to content

Commit 018c4ed

Browse files
authored
Merge pull request #36 from voegelas/after-fork
Replace the deprecated PyOS_AfterFork() function
2 parents f2f604b + 5c603a7 commit 018c4ed

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

.github/workflows/basic-tests.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Basic Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- '*'
7+
tags-ignore:
8+
- '*'
9+
pull_request:
10+
11+
jobs:
12+
basic-tests:
13+
runs-on: ubuntu-24.04
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
python-version: ["3.8", "3.13"]
18+
steps:
19+
- uses: actions/checkout@v4
20+
21+
- name: Set up Python ${{ matrix.python-version }}
22+
uses: actions/setup-python@v5
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
26+
- name: Install setuptools
27+
run: python -m pip install setuptools
28+
29+
- name: Install LXC
30+
run: sudo apt-get install -y lxc lxc-dev
31+
32+
- name: Configure LXC
33+
run: |
34+
mkdir -p ~/.config/lxc /var/tmp/lxc
35+
cat >~/.config/lxc/lxc.conf <<EOF
36+
lxc.lxcpath = /var/tmp/lxc
37+
EOF
38+
cat >~/.config/lxc/default.conf <<EOF
39+
lxc.include = /etc/lxc/default.conf
40+
lxc.idmap = u 0 165536 65536
41+
lxc.idmap = g 0 165536 65536
42+
lxc.apparmor.profile = unconfined
43+
EOF
44+
echo "$(id -un) veth lxcbr0 10" | sudo tee -a /etc/lxc/lxc-usernet
45+
46+
- name: Build module
47+
run: python3 setup.py build
48+
49+
- name: Install module
50+
run: pip install .
51+
52+
- name: Enable unprivileged user namespaces
53+
run: sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
54+
55+
- name: Create container
56+
run: >
57+
python3 -c 'import lxc;exit(0 if lxc.Container("mycontainer").create("download", 0, {"dist": "alpine", "release": "edge", "arch": "amd64"}) else 1)'
58+
59+
- name: Start container
60+
run: python3 -c 'import lxc;exit(0 if lxc.Container("mycontainer").start() else 1)'
61+
62+
- name: Run command inside container
63+
run: python3 -c 'import lxc;exit(lxc.Container("mycontainer").attach_wait(lxc.attach_run_command, ["uname", "-a"]))'
64+
65+
- name: Stop container
66+
if: success() || failure()
67+
run: python3 -c 'import lxc;exit(0 if lxc.Container("mycontainer").stop() else 1)'
68+
69+
- name: Destroy container
70+
if: success() || failure()
71+
run: python3 -c 'import lxc;exit(0 if lxc.Container("mycontainer").destroy() else 1)'

lxc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ static int lxc_attach_python_exec(void* _payload)
198198
* container. As lxc_attach() calls fork() PyOS_AfterFork should be called
199199
* in the new process if the Python interpreter will continue to be used.
200200
*/
201+
#if PY_VERSION_HEX >= 0x030700F0
202+
PyOS_AfterFork_Child();
203+
#else
201204
PyOS_AfterFork();
205+
#endif
202206

203207
struct lxc_attach_python_payload *payload =
204208
(struct lxc_attach_python_payload *)_payload;
@@ -748,8 +752,14 @@ Container_attach_and_possibly_wait(Container *self, PyObject *args,
748752
if (!options)
749753
return NULL;
750754

755+
#if PY_VERSION_HEX >= 0x030700F0
756+
PyOS_BeforeFork();
757+
#endif
751758
ret = self->container->attach(self->container, lxc_attach_python_exec,
752759
&payload, options, &pid);
760+
#if PY_VERSION_HEX >= 0x030700F0
761+
PyOS_AfterFork_Parent();
762+
#endif
753763
if (ret < 0)
754764
goto out;
755765

0 commit comments

Comments
 (0)