long2ice / asynch

An asyncio ClickHouse Python Driver with native (TCP) interface support.
https://github.com/long2ice/asynch
Apache License 2.0
186 stars 43 forks source link

Exception #10

Closed ikrivosheev closed 3 years ago

ikrivosheev commented 3 years ago

Clickhouse version: 21.3 Code:

async with self._pool.acquire() as conn:
    async with conn.cursor() as cursor:
        rc = await cursor.execute('INSERT INTO test VALUES', items)

Traceback

File "/usr/local/lib/python3.8/site-packages/ptms/event_combiner/writer.py", line 30, in write
    rc = await cursor.execute(self._query, self._items)
  File "/usr/local/lib/python3.8/site-packages/asynch/cursors.py", line 61, in execute
    response = await execute(query, args=args, with_column_types=True, **execute_kwargs)
  File "/usr/local/lib/python3.8/site-packages/asynch/proto/connection.py", line 526, in execute
    await self.force_connect()
  File "/usr/local/lib/python3.8/site-packages/asynch/proto/connection.py", line 575, in force_connect
    elif not await self.ping():
  File "/usr/local/lib/python3.8/site-packages/asynch/proto/connection.py", line 246, in ping
    packet_type = await self.reader.read_varint()
  File "/usr/local/lib/python3.8/site-packages/asynch/proto/io.py", line 117, in read_varint
    packet = self._read_one()
  File "/usr/local/lib/python3.8/site-packages/asynch/proto/io.py", line 107, in _read_one
    packet = self.buffer[self.position]
IndexError: bytearray index out of range
long2ice commented 3 years ago

That's the items?

ikrivosheev commented 3 years ago

I write some debug print:

    async def read_varint(self):
        if self.position == self.current_buffer_size:
            self._reset_buffer()
            await self._read_into_buffer()
        packets = bytearray()
        count = 0
        while True:
            count += 1
            try:
                packet = self._read_one()
            except Exception as e:
                print('-' * 10)
                print('Position: ', self.position)
                print('Current buffer size: ', self.current_buffer_size)
                print('Count: ', count)
                print('-' * 10)
                raise
            packets.append(packet)
            if packet < 0x80:
                break
        return leb128.u.decode(packets)

Output:

----------
Position:  13562
Current buffer size:  8688
Count:  1
----------
ikrivosheev commented 3 years ago

@long2ice can you make release?

long2ice commented 3 years ago

OK

abhinavraj23 commented 3 years ago

@long2ice I am still facing the exact same issue.

File "./connections/clickhouse_db.py", line 75, in fetchall
    await cursor.execute(query)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asynch/cursors.py", line 61, in execute
    response = await execute(query, args=args, with_column_types=True, **execute_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asynch/proto/connection.py", line 526, in execute
    await self.force_connect()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asynch/proto/connection.py", line 575, in force_connect
    elif not await self.ping():
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asynch/proto/connection.py", line 246, in ping
    packet_type = await self.reader.read_varint()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asynch/proto/io.py", line 127, in read_varint
    packet = self._read_one()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asynch/proto/io.py", line 117, in _read_one
    packet = self.buffer[self.position]
IndexError: bytearray index out of range

Here is my connection pool code

self.pool = await asynch.create_pool(
          dsn=self.dsn,
          minsize=self.min_size,
          maxsize=self.max_size,
)
async with self.pool.acquire() as con:
       try:
          wrapped_con = Connection(con)
          return await func(*args, connection=wrapped_con, **kwargs)
      except Exception as e:
          log.exception(e)
class Connection:
    def __init__(self, connection):
        self.asynch_connection = connection

    async def fetchone(self, query):
        async with self.asynch_connection.cursor(cursor=DictCursor) as cursor:
            await cursor.execute(query)
            result = cursor.fetchone()
            return result

    async def fetchall(self, query):
        async with self.asynch_connection.cursor(cursor=DictCursor) as cursor:
            await cursor.execute(query)
            results = cursor.fetchall()
            return results
jayvynl commented 2 years ago

@long2ice I am still facing the exact same issue.

File "./connections/clickhouse_db.py", line 75, in fetchall
    await cursor.execute(query)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asynch/cursors.py", line 61, in execute
    response = await execute(query, args=args, with_column_types=True, **execute_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asynch/proto/connection.py", line 526, in execute
    await self.force_connect()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asynch/proto/connection.py", line 575, in force_connect
    elif not await self.ping():
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asynch/proto/connection.py", line 246, in ping
    packet_type = await self.reader.read_varint()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asynch/proto/io.py", line 127, in read_varint
    packet = self._read_one()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asynch/proto/io.py", line 117, in _read_one
    packet = self.buffer[self.position]
IndexError: bytearray index out of range

Here is my connection pool code

self.pool = await asynch.create_pool(
          dsn=self.dsn,
          minsize=self.min_size,
          maxsize=self.max_size,
)
async with self.pool.acquire() as con:
       try:
          wrapped_con = Connection(con)
          return await func(*args, connection=wrapped_con, **kwargs)
      except Exception as e:
          log.exception(e)
class Connection:
    def __init__(self, connection):
        self.asynch_connection = connection

    async def fetchone(self, query):
        async with self.asynch_connection.cursor(cursor=DictCursor) as cursor:
            await cursor.execute(query)
            result = cursor.fetchone()
            return result

    async def fetchall(self, query):
        async with self.asynch_connection.cursor(cursor=DictCursor) as cursor:
            await cursor.execute(query)
            results = cursor.fetchall()
            return results

I am facing exactly the same issue.