Skip to content

πŸŽ› πŸ”Š A Python library for audio.

Add fine-grained locks to prevent use of plugins after free.

Peter Sobot4880821d9f03
diff --git a/pedalboard/PluginContainer.h b/pedalboard/PluginContainer.h
index 2acccdd..65bd777 100644
--- a/pedalboard/PluginContainer.h
+++ b/pedalboard/PluginContainer.h
@@ -80,6 +80,7 @@ inline void init_plugin_container(py::module &m) {
.def(
"__getitem__",
[](PluginContainer &s, int i) {
+ std::scoped_lock lock(s.mutex);
if (i < 0)
i = s.getPlugins().size() + i;
if (i < 0)
@@ -94,6 +95,7 @@ inline void init_plugin_container(py::module &m) {
.def(
"__setitem__",
[](PluginContainer &s, int i, std::shared_ptr<Plugin> plugin) {
+ std::scoped_lock lock(s.mutex);
if (i < 0)
i = s.getPlugins().size() + i;
if (i < 0)
@@ -108,6 +110,7 @@ inline void init_plugin_container(py::module &m) {
.def(
"__delitem__",
[](PluginContainer &s, int i) {
+ std::scoped_lock lock(s.mutex);
if (i < 0)
i = s.getPlugins().size() + i;
if (i < 0)
@@ -120,12 +123,16 @@ inline void init_plugin_container(py::module &m) {
py::arg("index"),
"Delete a plugin by its index. Index may be negative. If the index "
"is out of range, an IndexError will be thrown.")
- .def(
- "__len__", [](PluginContainer &s) { return s.getPlugins().size(); },
+ .def("__len__",
+ [](PluginContainer &s) {
+ std::scoped_lock lock(s.mutex);
+ return s.getPlugins().size();
+ },
"Get the number of plugins in this container.")
.def(
"insert",
[](PluginContainer &s, int i, std::shared_ptr<Plugin> plugin) {
+ std::scoped_lock lock(s.mutex);
if (i < 0)
i = s.getPlugins().size() + i;
if (i < 0)
@@ -140,12 +147,14 @@ inline void init_plugin_container(py::module &m) {
.def(
"append",
[](PluginContainer &s, std::shared_ptr<Plugin> plugin) {
+ std::scoped_lock lock(s.mutex);
s.getPlugins().push_back(plugin);
},
py::arg("plugin"), "Append a plugin to the end of this container.")
.def(
"remove",
[](PluginContainer &s, std::shared_ptr<Plugin> plugin) {
+ std::scoped_lock lock(s.mutex);
auto &plugins = s.getPlugins();
auto position = std::find(plugins.begin(), plugins.end(), plugin);
if (position == plugins.end())
@@ -163,6 +172,7 @@ inline void init_plugin_container(py::module &m) {
.def(
"__contains__",
[](PluginContainer &s, std::shared_ptr<Plugin> plugin) {
+ std::scoped_lock lock(s.mutex);
auto &plugins = s.getPlugins();
return std::find(plugins.begin(), plugins.end(), plugin) !=
plugins.end();
diff --git a/pedalboard/io/AudioStream.h b/pedalboard/io/AudioStream.h
index 5dcfa3b..9231ab0 100644
--- a/pedalboard/io/AudioStream.h
+++ b/pedalboard/io/AudioStream.h
@@ -112,15 +112,23 @@ public:
while (isRunning) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
- // Check to see if the live pedalboard and the regular pedalboard
- // differ, and apply any changes as necessary:
- if (pedalboard->getAllPlugins() != livePedalboard.getAllPlugins()) {
- juce::SpinLock::ScopedLockType lock(livePedalboardMutex);
-
- livePedalboard.getPlugins().clear();
- for (auto plugin : pedalboard->getPlugins()) {
- plugin->prepare(spec);
- livePedalboard.getPlugins().push_back(plugin);
+ // Make sure nobody modifies the Python-side object while we're reading
+ // it (without taking the GIL, which would be expensive)
+ std::unique_lock<std::mutex> lock(pedalboard->mutex, std::try_to_lock);
+ if (lock.owns_lock()) {
+ // We can read livePedalboard's plugins without a lock, as we're the
+ // only writer:
+ if (pedalboard->getAllPlugins() != livePedalboard.getAllPlugins()) {
+ // But if we need to write, then we need to try to get the lock:
+ juce::SpinLock::ScopedTryLockType tryLock(livePedalboardMutex);
+ if (tryLock.isLocked()) {
+ livePedalboard.getPlugins().clear();
+
+ for (auto plugin : pedalboard->getPlugins()) {
+ plugin->prepare(spec);
+ livePedalboard.getPlugins().push_back(plugin);
+ }
+ }
}
}
}
@@ -183,7 +191,13 @@ public:
juce::SpinLock::ScopedTryLockType tryLock(livePedalboardMutex);
if (tryLock.isLocked()) {
for (auto plugin : livePedalboard.getPlugins()) {
- int outputSamples = plugin->process(context);
+ std::unique_lock<std::mutex> lock(pedalboard->mutex, std::try_to_lock);
+ // If someone's running audio through this plugin in parallel (offline,
+ // or in a different AudioStream object) then don't corrupt its state by
+ // calling it here too; instead, just skip it:
+ if (lock.owns_lock()) {
+ plugin->process(context);
+ }
}
}
}
@@ -225,6 +239,9 @@ private:
// A simple spin-lock mutex that we can try to acquire from
// the audio thread, allowing us to avoid modifying state that's
// currently being used for rendering.
+ // The `livePedalboard` object already has a std::mutex on it,
+ // but we want something that's faster and callable from the
+ // dudio thread.
juce::SpinLock livePedalboardMutex;
// A "live" pedalboard, called from the audio thread.