Skip to content

Commit dfdacb7

Browse files
committed
improve restore_allow_thread_sharing
1 parent 4155379 commit dfdacb7

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

channels/db.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@ def _inherit_main_thread_connections(self):
1919
2020
This is required for tests using Django's TestCase.
2121
"""
22-
restore_allow_thread_sharing = []
22+
restore_allow_thread_sharing = {}
2323

2424
for name in self.main_thread_connections:
2525
if self.main_thread_connections[name].in_atomic_block:
2626
connections[name] = self.main_thread_connections[name]
2727
if HAS_INC_THREAD_SHARING:
2828
connections[name].inc_thread_sharing()
29-
elif not connections[name].allow_thread_sharing:
30-
restore_allow_thread_sharing.append(name)
31-
connections[name].allow_thread_sharing = True
29+
else:
30+
saved_sharing = connections[name].allow_thread_sharing
31+
if not saved_sharing:
32+
restore_allow_thread_sharing[name] = saved_sharing
33+
connections[name].allow_thread_sharing = True
3234
return restore_allow_thread_sharing
3335

3436
def _close_old_connections(self):
@@ -47,8 +49,8 @@ def thread_handler(self, loop, *args, **kwargs):
4749
return super().thread_handler(loop, *args, **kwargs)
4850
finally:
4951
self._close_old_connections()
50-
for name in restore_allow_thread_sharing:
51-
connections[name].allow_thread_sharing = False
52+
for name, saved_sharing in restore_allow_thread_sharing.items():
53+
connections[name].allow_thread_sharing = saved_sharing
5254

5355

5456
# The class is TitleCased, but we want to encourage use as a callable/decorator

0 commit comments

Comments
 (0)