From 662ec894f6493463e759aa603a6989572c5b6180 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@ericsson.com>
Date: Thu, 4 Apr 2024 15:00:03 +0200
Subject: [PATCH] fixed #22

---
 CHANGELOG.md         |  1 +
 coolamqp/__init__.py |  2 +-
 coolamqp/objects.py  | 18 ++++++++++--------
 3 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4e6bb2b..ee83991 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,3 +4,4 @@ have been made so far, between releases.
 
 # v1.3.1
 
+* coolamqp.objects.Callable made threadsafe (fixes #22)
\ No newline at end of file
diff --git a/coolamqp/__init__.py b/coolamqp/__init__.py
index a1d2230..d98f7f7 100644
--- a/coolamqp/__init__.py
+++ b/coolamqp/__init__.py
@@ -1 +1 @@
-__version__ = '1.3.1a1'
+__version__ = '1.3.1a2'
diff --git a/coolamqp/objects.py b/coolamqp/objects.py
index 65d9db0..b396d35 100644
--- a/coolamqp/objects.py
+++ b/coolamqp/objects.py
@@ -3,6 +3,7 @@
 Core objects used in CoolAMQP
 """
 import logging
+import threading
 import typing as tp
 import uuid
 
@@ -33,23 +34,24 @@ class Callable(object):
     """
     Add a bunch of callables to one list, and just invoke'm.
     INTERNAL USE ONLY
-    #todo not thread safe
     """
-    __slots__ = ('callables', 'oneshots')
+    __slots__ = ('callables', 'oneshots', 'lock')
 
     def __init__(self, oneshots=False):
         """:param oneshots: if True, callables will be called and discarded"""
         self.callables = []
+        self.lock = threading.Lock()
         self.oneshots = oneshots
 
-    def add(self, callable):
-        self.callables.append(callable)
+    def add(self, clbl):
+        self.callables.append(clbl)
 
     def __call__(self, *args, **kwargs):
-        for callable in self.callables:
-            callable(*args, **kwargs)
-        if self.oneshots:
-            self.callables = []
+        with self.lock:
+            for clbl in self.callables:
+                clbl(*args, **kwargs)
+            if self.oneshots:
+                self.callables = []
 
 
 class Message(object):
-- 
GitLab