From 498db57ec84083d92a9f512fa3ac1ed90625eb69 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Wed, 12 Sep 2018 00:34:53 +0100 Subject: [PATCH] issue #360: ansible: missing lock around ContextService.put(). --- ansible_mitogen/services.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ansible_mitogen/services.py b/ansible_mitogen/services.py index 59c26ba2..fdc7e2a7 100644 --- a/ansible_mitogen/services.py +++ b/ansible_mitogen/services.py @@ -139,11 +139,15 @@ class ContextService(mitogen.service.Service): count reaches zero. """ LOG.debug('%r.put(%r)', self, context) - if self._refs_by_context.get(context, 0) == 0: - LOG.warning('%r.put(%r): refcount was 0. shutdown_all called?', - self, context) - return - self._refs_by_context[context] -= 1 + self._lock.acquire() + try: + if self._refs_by_context.get(context, 0) == 0: + LOG.warning('%r.put(%r): refcount was 0. shutdown_all called?', + self, context) + return + self._refs_by_context[context] -= 1 + finally: + self._lock.release() def key_from_kwargs(self, **kwargs): """