Skip to content
This repository was archived by the owner on Feb 21, 2023. It is now read-only.

Commit 950588c

Browse files
committed
Fixed unclosed transactions + pool (see #32)
1 parent 484ce6e commit 950588c

File tree

4 files changed

+10
-15
lines changed

4 files changed

+10
-15
lines changed

aioredis/commands/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ def connection(self):
5050
""":class:`aioredis.RedisConnection` instance."""
5151
return self._conn
5252

53+
@property
54+
def in_transaction(self):
55+
"""Set to True when MULTI command was issued."""
56+
return self._conn.in_transaction
57+
5358
@property
5459
def closed(self):
5560
"""True if connection is closed."""

aioredis/pool.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ def release(self, conn):
136136
assert conn in self._used, "Invalid connection, maybe from other pool"
137137
self._used.remove(conn)
138138
if not conn.closed:
139-
if conn.db == self.db:
139+
if conn.in_transaction:
140+
# TODO: log warning
141+
conn.close()
142+
elif conn.db == self.db:
140143
try:
141144
self._pool.put_nowait(conn)
142145
except asyncio.QueueFull:

tests/_testutil.py

-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import asyncio
22
import unittest
3-
import socket
4-
import random
53
import os
64

75
from functools import wraps
@@ -34,18 +32,6 @@ def tearDown(self):
3432
self.loop.close()
3533
del self.loop
3634

37-
def _find_port(self):
38-
s = socket.socket()
39-
while True:
40-
port = random.randint(1024, 65535)
41-
try:
42-
s.bind(('127.0.0.1', port))
43-
except OSError:
44-
pass
45-
else:
46-
s.close()
47-
return port
48-
4935

5036
class RedisTest(BaseTest):
5137

tests/pool_test.py

+1
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ def test_crappy_multiexec(self):
243243
yield from redis.set('abc', 'def')
244244
yield from redis.multi()
245245
yield from redis.set('abc', 'fgh')
246+
self.assertTrue(redis.closed)
246247
with (yield from pool) as redis:
247248
value = yield from redis.get('abc')
248249
self.assertEquals(value, 'def')

0 commit comments

Comments
 (0)