Skip to content

Commit daa7935

Browse files
Fix deadlock issue
1 parent b264035 commit daa7935

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

pythonfmu3/pythonfmu-export/src/pythonfmu/PySlaveInstance.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ std::string getClassName(PyObject * pModule)
5757
className = classNameCandidate;
5858
Py_DECREF(baseNameObj);
5959
Py_DECREF(bases);
60-
break;
60+
Py_DECREF(keys);
61+
return className;
6162
}
6263
}
6364
Py_DECREF(baseNameObj);
@@ -75,13 +76,27 @@ std::string getClassName(PyObject * pModule)
7576
return className;
7677
}
7778

79+
class GILStateGuard {
80+
public:
81+
GILStateGuard() : gil_state_(PyGILState_Ensure()) {}
82+
~GILStateGuard() { PyGILState_Release(gil_state_); }
83+
84+
GILStateGuard(const GILStateGuard&) = delete;
85+
GILStateGuard& operator=(const GILStateGuard&) = delete;
86+
87+
PyGILState_STATE get() const { return gil_state_; }
88+
89+
private:
90+
PyGILState_STATE gil_state_;
91+
};
92+
7893
inline void py_safe_run(const std::function<void(PyGILState_STATE gilState)>& f)
7994
{
80-
PyGILState_STATE gil_state = PyGILState_Ensure();
81-
f(gil_state);
82-
PyGILState_Release(gil_state);
95+
GILStateGuard guard;
96+
f(guard.get());
8397
}
8498

99+
85100
PySlaveInstance::PySlaveInstance(std::string instanceName, std::string resources, const cppfmu::Logger& logger, const bool visible, std::shared_ptr<IPyState> pyState)
86101
: pyState_{ std::move(pyState) }
87102
, instanceName_(std::move(instanceName))

0 commit comments

Comments
 (0)