Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect ping handling on connection close + reopen #454

Open
sknaumov opened this issue Sep 18, 2024 · 0 comments
Open

Incorrect ping handling on connection close + reopen #454

sknaumov opened this issue Sep 18, 2024 · 0 comments

Comments

@sknaumov
Copy link

sknaumov commented Sep 18, 2024

Describe the bug
I'm using django-clickhouse-backend and want to work with clickhouse database using multiple python processes. For that purpose I need to close db connections before spawning new processes by calling

from django.db import connections

connections.close_all()

The problem is that if I've already queried DB in the main process before closing connections, I have the following warnings + exception:

clickhouse_driver.connection - WARNING - Error on clickhouse.***:9000 ping: Unexpected EOF while reading bytes
clickhouse_driver.connection - WARNING - Error on clickhouse.***:9000 ping: Unexpected EOF while reading bytes
...
clickhouse_driver.connection - WARNING - Connection was closed, reconnecting.
clickhouse_driver.connection - WARNING - Connection was closed, reconnecting.
...
django.db.utils.OperationalError: Code: 100. Unknown packet 4 from server clickhouse.***:9000

It seems that pings are not properly handled at connection reopening.

To Reproduce

Not a complete code, but general idea:

from datetime import datetime
from multiprocessing import Pool

from django.db import connections

def worker(interval):
    """get objects from clickhouse database for a given time interval"""
    
    objs = clickhouse_model.objects.filter(
        created_at__gte=interval.from,
        created_at__lt=interval.to,
    )

def main():
    """spawn multiple workers over time intervals"""

    min_date = clickhouse_model.objects.all.aggregate(min=Min('created_at'))['min']
    intervals = get_invervals(from=min_date, to=datetime.now(), size=86400)

    connections.close_all()  # Django will automatically reopen connection on the first query
    with Pool(processes=processes) as pool:
        for _ in pool.imap_unordered(worker, intervals):
             pass

Expected behavior

  • No warnings issued on user-initiated disconnect.
  • Pings are properly stopped.
  • I do not know why ping packets from closed connection are received on reopen, but they have to be silently discarded.

Versions

  • django-clickhouse-backend v1.3.0 + clickhouse-driver v0.2.8.
  • ClickHouse server v22.3.20.29.
  • Python v3.12.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant