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
4 changes: 4 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ organisation on `GitHub <https://github.com/openbiosim/sire>`__.
* Fixed ``System.set_time`` rejecting a zero time returned by ``System.time``, since a zero
``GeneralUnit`` has no units.

* Fixed converting a molecule to RDKit hanging, or aborting on Python 3.13 and later, when the
conversion emitted a warning, e.g. when bond orders couldn't be inferred. The console didn't
release the GIL after importing its Python implementation.

`2026.2.0 <https://github.com/openbiosim/sire/compare/2026.1.0...2026.2.0>`__ - September 2026
----------------------------------------------------------------------------------------------

Expand Down
34 changes: 34 additions & 0 deletions tests/convert/test_rdkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,37 @@ def test_rdkit_sdf_tags(tagged_sdf):
# check that the data is the same on the rdkit molecule
for prop in rdmol.GetPropNames():
assert rdmol.GetProp(prop) == rdmol2.GetProp(prop)


@pytest.mark.skipif(
"rdkit" not in sr.convert.supported_formats(),
reason="rdkit support is not available",
)
def test_rdkit_console_warning_without_gil():
"""
Make sure that a console warning raised during conversion, which runs
without the GIL, doesn't hang or abort the interpreter. The conversion
runs in a separate process, since the failure is a deadlock.
"""
import subprocess
import sys
import textwrap

# Hiding MDAnalysis makes bond-order inference of a molecule with dummy
# atoms fail, so the converter emits a warning.
script = textwrap.dedent(
"""
import sys
sys.modules["MDAnalysis"] = None

import sire as sr

mols = sr.morph.link_to_perturbed(sr.load_test_files("merged_molecule.s3"))
sr.convert.to_rdkit(mols.molecules("property is_perturbable")[0])
"""
)

result = subprocess.run(
[sys.executable, "-c", script], capture_output=True, text=True, timeout=300
)
assert result.returncode == 0, result.stderr
121 changes: 31 additions & 90 deletions wrapper/Base/releasegil_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "SireBase/console.h"
#include "SireBase/releasegil.h"

#include "Helpers/scoped_gil_acquire.hpp"

#include <QDebug>

static bool _is_ipython = false;
Expand Down Expand Up @@ -282,140 +284,80 @@ class ConsoleImpl : public SireBase::ConsoleBase
{
if (pyconsole)
{
PyGILState_STATE gilstate = PyGILState_Ensure();
SireHelpers::ScopedGILAcquire gil;
Py_DECREF(pyconsole);
delete pyconsole;
pyconsole = 0;
PyGILState_Release(gilstate);
}
}

void debug(const QString &message) const
{
const_cast<ConsoleImpl *>(this)->get_pyconsole();

if (pyconsole)
{
PyGILState_STATE gilstate = PyGILState_Ensure();

PyObject *result = PyObject_CallMethod(pyconsole, "debug", "s", message.toUtf8().constData());

if (result == 0)
{
qDebug() << "UNABLE TO CALL DEBUG";
qDebug() << message;
}
else
{
Py_DECREF(result);
}

PyGILState_Release(gilstate);
}
else
if (not this->call("debug", message))
{
qDebug() << "NO PYCONSOLE";
qDebug() << message;
}
}

void warning(const QString &message) const
{
const_cast<ConsoleImpl *>(this)->get_pyconsole();

if (pyconsole)
{
PyGILState_STATE gilstate = PyGILState_Ensure();

PyObject *result = PyObject_CallMethod(pyconsole, "warning", "s", message.toUtf8().constData());

if (result == 0)
{
qWarning() << message;
}
else
{
Py_DECREF(result);
}

PyGILState_Release(gilstate);
}
else
if (not this->call("warning", message))
{
qWarning() << message;
}
}

void error(const QString &message) const
{
const_cast<ConsoleImpl *>(this)->get_pyconsole();

if (pyconsole)
{
PyGILState_STATE gilstate = PyGILState_Ensure();

PyObject *result = PyObject_CallMethod(pyconsole, "error", "s", message.toUtf8().constData());

if (result == 0)
{
qCritical() << message;
}
else
{
Py_DECREF(result);
}

PyGILState_Release(gilstate);
}
else
if (not this->call("error", message))
{
qCritical() << message;
}
}

void info(const QString &message) const
{
const_cast<ConsoleImpl *>(this)->get_pyconsole();

if (pyconsole)
if (not this->call("info", message))
{
PyGILState_STATE gilstate = PyGILState_Ensure();
qInfo() << message;
}
}

PyObject *result = PyObject_CallMethod(pyconsole, "info", "s", message.toUtf8().constData());
private:
/** Call the named method of the Python console with the message,
* returning whether it succeeded. */
bool call(const char *method, const QString &message) const
{
SireHelpers::ScopedGILAcquire gil;

if (result == 0)
{
qInfo() << "UNABLE TO CALL INFO";
qInfo() << message;
}
else
{
Py_DECREF(result);
}
const_cast<ConsoleImpl *>(this)->get_pyconsole();

PyGILState_Release(gilstate);
}
else
if (not pyconsole)
return false;

PyObject *result = PyObject_CallMethod(pyconsole, method, "s", message.toUtf8().constData());

if (result == 0)
{
qInfo() << "NO PYCONSOLE";
qInfo() << message;
PyErr_Clear();
return false;
}

Py_DECREF(result);
return true;
}

private:
/** Import the Python console. This must be called with the GIL held. */
void get_pyconsole()
{
if (pyconsole)
return;

PyGILState_STATE gilstate = PyGILState_Ensure();

PyObject *sire_utils = PyImport_ImportModule("sire.utils");

if (sire_utils == 0)
{
PyErr_Clear();
qWarning() << "COULD NOT IMPORT SIRE.UTILS";
PyGILState_Release(gilstate);
return;
}

Expand All @@ -425,9 +367,8 @@ class ConsoleImpl : public SireBase::ConsoleBase

if (pyconsole == 0)
{
PyErr_Clear();
qWarning() << "Could not import Console from sire.utils";
PyGILState_Release(gilstate);
return;
}
}

Expand Down
16 changes: 4 additions & 12 deletions wrapper/Convert/SireOpenMM/pyqm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
#include "openmm/serialization/SerializationNode.h"
#include "openmm/serialization/SerializationProxy.h"

#include "Helpers/scoped_gil_acquire.hpp"
#include "SireError/errors.h"

#include "SireMaths/vector.h"
#include "SireStream/datastream.h"
#include "SireStream/shareddatastream.h"
Expand All @@ -51,16 +53,6 @@ using namespace SireVol;
// atoms, in Angstrom.
static const double VIRTUAL_PC_DELTA = 0.01;

class GILLock
{
public:
GILLock() { state_ = PyGILState_Ensure(); }
~GILLock() { PyGILState_Release(state_); }

private:
PyGILState_STATE state_;
};

/////////
///////// Implementation of PyQMCallback
/////////
Expand Down Expand Up @@ -159,7 +151,7 @@ PyQMCallback::call(
{

// Acquire GIL before calling Python code.
GILLock lock;
SireHelpers::ScopedGILAcquire gil;

if (this->is_method)
{
Expand Down Expand Up @@ -216,7 +208,7 @@ PyQMCallback::call4(
QVector<int> idx_mm) const
{
// Acquire GIL before calling Python code.
GILLock lock;
SireHelpers::ScopedGILAcquire gil;

bp::object result;
try
Expand Down
17 changes: 3 additions & 14 deletions wrapper/Convert/SireRDKit/sire_rdkit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "boost/python.hpp"

#include "Helpers/scoped_gil_acquire.hpp"

#include "SireStream/datastream.h"
#include "SireStream/shareddatastream.h"

Expand Down Expand Up @@ -63,19 +65,6 @@ using SireMol::SelectorMol;

namespace bp = boost::python;

// RAII wrapper to acquire/release the Python GIL before calling back into
// Python from arbitrary C++ contexts (including non-Python-created threads).
// Same pattern as wrapper/Convert/SireOpenMM/pyqm.cpp's GILLock.
class GILLock
{
public:
GILLock() { state_ = PyGILState_Ensure(); }
~GILLock() { PyGILState_Release(state_); }

private:
PyGILState_STATE state_;
};

namespace SireRDKit
{
// Optional Python callback used as a fallback for RDKit bond-order/
Expand Down Expand Up @@ -105,7 +94,7 @@ namespace SireRDKit
if (bond_order_inference_callback == nullptr or bond_order_inference_callback->is_none())
return false;

GILLock lock;
SireHelpers::ScopedGILAcquire gil;

try
{
Expand Down
1 change: 1 addition & 0 deletions wrapper/Helpers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set ( WRAPHELPERS_SOURCES

release_gil_policy.hpp
release_gil_policy.cpp
scoped_gil_acquire.hpp
scoped_gil_release.hpp

)
Expand Down
34 changes: 34 additions & 0 deletions wrapper/Helpers/scoped_gil_acquire.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef _HELPERS_SCOPED_GIL_ACQUIRE_HPP_
#define _HELPERS_SCOPED_GIL_ACQUIRE_HPP_

#include "boost/python.hpp"

namespace SireHelpers
{
/** Acquire the GIL for the lifetime of this object.
*
* Use this before calling into Python from C++ that may run without the
* GIL, e.g. within a ScopedGILRelease or on a worker thread. The GIL is
* released on every return path, including during stack unwinding.
*/
class ScopedGILAcquire
{
public:
ScopedGILAcquire() : state(PyGILState_Ensure())
{
}

~ScopedGILAcquire()
{
PyGILState_Release(state);
}

private:
ScopedGILAcquire(const ScopedGILAcquire &);
ScopedGILAcquire &operator=(const ScopedGILAcquire &);

PyGILState_STATE state;
};
}

#endif
Loading