Deutsch   English   Français   Italiano  
<mailman.11.1720386402.2981.python-list@python.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail
From: Tomas Ukkonen <tomas.ukkonen@protonmail.ch>
Newsgroups: comp.lang.python
Subject: PyGILState_Ensure() deadlocks, why?
Date: Sun, 07 Jul 2024 19:24:04 +0000
Lines: 103
Message-ID: <mailman.11.1720386402.2981.python-list@python.org>
References: <y-6AMIAbWkvLII_tfcbRs5NtbKgQRos3XlZ83qbGCwuq7RSQJBGniGJEn4Nzwr_iVQfJOdvLVzOycTR0V-F-pDKt2rNPj3cHmFVY06vNQnI=@protonmail.ch>
Mime-Version: 1.0
Content-Type: multipart/signed; protocol="application/pgp-signature";
 micalg=pgp-sha256;
 boundary="------ce3546e9f767c4f407662caac2a8083d8c4baf7a1f1fc141736db6f000bc5b55";
 charset=utf-8
X-Trace: news.uni-berlin.de FKtOrZhAPRNTmi3ODBmFpgqAZEHM0ho26ChnM4i6O8AQ==
Cancel-Lock: sha1:gECVaiofmvM6379XtHvCU+gosLk= sha256:Vs636oROhxm+9KO3U1PztuDy8X+pm6/uJLiWR63yokg=
Return-Path: <tomas.ukkonen@protonmail.ch>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
 reason="2048-bit key; unprotected key"
 header.d=protonmail.ch header.i=@protonmail.ch header.b=jJpR0a7s;
 dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'content-
 type:multipart/signed': 0.05; 'thread': 0.05; 'content-
 type:application/pgp-signature': 0.09; 'filename:fname piece:asc':
 0.09; 'filename:fname piece:signature': 0.09;
 'filename:fname:signature.asc': 0.09; 'int': 0.09; 'linux': 0.09;
 'skip:` 10': 0.09; 'subject:skip:P 10': 0.09; 'threads': 0.09;
 'ubuntu': 0.09; '#include': 0.16; '<': 0.16; 'c++': 0.16;
 'content-disposition:attachment; filename="signature.asc"': 0.16;
 'gil': 0.16; 'gnu/linux': 0.16; 'initialize': 0.16; 'interpreter':
 0.16; 'long-running': 0.16; 'main()': 0.16; 'subject:() ': 0.16;
 'vector': 0.16; 'void': 0.16; 'python': 0.16; 'api': 0.17;
 'to:addr:python-list': 0.20; 'code': 0.23; 'skip:p 30': 0.23;
 'to:name:python-list@python.org': 0.24; 'received:185.70': 0.26;
 'received:protonmail.ch': 0.26; 'task': 0.26; 'release': 0.34;
 'received:ch': 0.35; 'following': 0.35; 'skip:t 40': 0.64;
 'content-type:multipart/mixed': 0.68; 'received:185': 0.71;
 'header:Received:2': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.ch;
 s=protonmail3; t=1720380248; x=1720639448;
 bh=8kkRbFLAJcKPou5dYM8yBDskAATb/w500GRGvm0922E=;
 h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date:
 Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector;
 b=jJpR0a7smrMC5oa/7NF0rCxpYglspYlQbTXrFErUk0H3kRwGq6DrX8Qne3nQCq363
 rooiyifal4FdCRJ86BxsOrSz1aHsPykvIi+8izO10g4MxhVhODri0Ar8r87oonB7QW
 xRKbcOjDlV+4wNqdL+ASTyYgFEn58ROPy9mlOhf1BU+hUgV8io4oAO2HPFIMz3zoG6
 W9aj/4iyAg4ysZ+xLZQMlajrxh3vdy1ianLGm2ILqP5F/KPqw4omm7cU2PpR9kknOS
 na4kYbc0BcLCgm3gndZf8kdvTgsmIPvDjEnjfQnYAeFIUOpWNeUTPoSBAwCihaJLVH
 mg+ybOhvu2g+Q==
Feedback-ID: 809323:user:proton
X-Pm-Message-ID: 7b589e65dd766691116ab9c0b1936ade8acab311
X-Mailman-Approved-At: Sun, 07 Jul 2024 17:06:42 -0400
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
 <python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
 <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
 <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <y-6AMIAbWkvLII_tfcbRs5NtbKgQRos3XlZ83qbGCwuq7RSQJBGniGJEn4Nzwr_iVQfJOdvLVzOycTR0V-F-pDKt2rNPj3cHmFVY06vNQnI=@protonmail.ch>
Bytes: 7122

This is an OpenPGP/MIME signed message (RFC 4880 and 3156)
--------ce3546e9f767c4f407662caac2a8083d8c4baf7a1f1fc141736db6f000bc5b55
Content-Type: multipart/mixed;boundary=---------------------c50b1a0d029d02ab0c0c864f7ca0375d

-----------------------c50b1a0d029d02ab0c0c864f7ca0375d
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;charset=utf-8

Hello
Is this python c api bug? The following C++ code (g++) deadlocks on Ubuntu=
 Linux.

/*=C2=A0* This code deadlocks on python3-dev 3.12.3 (ubuntu 24.04 lts)
=C2=A0*
=C2=A0* g++ python_thread_test.cpp `python3-config --cflags --libs --embed=
`
=C2=A0* ./a.out
=C2=A0*
=C2=A0* uname:
=C2=A0* Linux softice 6.8.0-36-generic SMP PREEMPT_DYNAMIC x86_64 GNU/Linu=
x
=C2=A0*/

#include <Python.h>
#include <thread>
#include <vector>
#include <iostream>

void perform_long_operation() {
=C2=A0 =C2=A0 // Simulate a long-running task
=C2=A0 =C2=A0 std::this_thread::sleep_for(std::chrono::seconds(5));
}

void thread_function() {
=C2=A0 =C2=A0 // Ensure this thread has the GIL
=C2=A0 =C2=A0 PyGILState_STATE gstate =3D PyGILState_Ensure();

=C2=A0 =C2=A0 // Execute some Python code
=C2=A0 =C2=A0 PyRun_SimpleString("print('Hello from std::thread!')");

=C2=A0 =C2=A0 // Release the GIL for long operation
=C2=A0 =C2=A0 Py_BEGIN_ALLOW_THREADS
=C2=A0 =C2=A0 perform_long_operation();
=C2=A0 =C2=A0 Py_END_ALLOW_THREADS

=C2=A0 =C2=A0 // Re-acquire the GIL and execute more Python code
=C2=A0 =C2=A0 gstate =3D PyGILState_Ensure();
=C2=A0 =C2=A0 PyRun_SimpleString("print('Thread operation completed!')");

=C2=A0 =C2=A0 // Release the GIL
=C2=A0 =C2=A0 PyGILState_Release(gstate);
}

int main() {
=C2=A0 =C2=A0 // Initialize the Python Interpreter
=C2=A0 =C2=A0 Py_Initialize();

=C2=A0 =C2=A0 // Create a vector of threads
=C2=A0 =C2=A0 std::vector<std::thread> threads;

=C2=A0 =C2=A0 // Launch threads
=C2=A0 =C2=A0 for (int i =3D 0; i < 5; ++i) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 threads.push_back(std::thread(thread_function)=
);
=C2=A0 =C2=A0 }

=C2=A0 =C2=A0 // Join threads
=C2=A0 =C2=A0 for (auto& t : threads) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 t.join();
=C2=A0 =C2=A0 }

=C2=A0 =C2=A0 // Finalize the Python Interpreter
=C2=A0 =C2=A0 Py_Finalize();

=C2=A0 =C2=A0 return 0;
}



Tomas Ukkonen
-----------------------c50b1a0d029d02ab0c0c864f7ca0375d--

--------ce3546e9f767c4f407662caac2a8083d8c4baf7a1f1fc141736db6f000bc5b55
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: ProtonMail

wsBzBAEBCAAnBYJmiutFCZCaEYwMt4b97BYhBJbmzyegv3Wo8O5gpZoRjAy3
hv3sAACH0wgAk6xX7goQyDG6b2zgFvqJ0HoSqH5qjNyXyKb8JrdWMS08dw53
aOwYhRvRRvNK17zv1RPp8G8ROjg9S3zjuURb4ChofqUZk8iHKDFG782JTdPG
EE1XP8e/j0VNHwRJZyif/cKcu6JUnVlISv6xMKCvBxCC3dDmVEi2S3YQEC3T
VJCw2XKpP1TEVXoDpnXgBIbHaGlm+EGCADIuZqXRI3idHnsPM+6tQVRX8R8y
L3/7jooYtXx7UIoiX6t3o5TWc42lD/eCmWyDNHAxZHz+8KiolxCXi+IDy9RD
Kpwwvj3gPUaQlk7HjTb7EkuoU9xl4V63QwfruvCtDX3gJak4+ac4sg==
=0jND
-----END PGP SIGNATURE-----


--------ce3546e9f767c4f407662caac2a8083d8c4baf7a1f1fc141736db6f000bc5b55--