Skip to content

Django Server Hangs During Startup/Shutdown Due to Blocking LogtailHandler.flush() #35

@vikram-typeset

Description

@vikram-typeset

Problem

Django server hangs indefinitely during startup/shutdown when LogtailHandler is enabled. When commenting out the logtail configuration, the server works fine. This issue occurs consistently but not always - depends on timing and network conditions.

Root Cause

The LogtailHandler.flush() method blocks indefinitely in an infinite loop without timeout:

# logtail/flusher.py lines 80-84
def flush(self):
    self._flushing = True
    while not self._clean or not self.pipe.empty():
        time.sleep(self.check_interval)  # Infinite loop!
    self._flushing = False

Reproduction

  1. Add LogtailHandler to Django logging configuration
  2. Start Django server: python manage.py runserver
  3. Stop server with Ctrl+C
  4. Result: Server hangs and must be force-killed

Workaround: Commenting out logtail handler makes Django work normally.

Error When Force-Interrupted

Traceback (most recent call last):
  File "/usr/lib/python3.11/logging/__init__.py", line 2185, in shutdown
    h.flush()
  File "logtail/flusher.py", line 83, in flush
    time.sleep(self.check_interval)
KeyboardInterrupt:

Environment

  • Python 3.11, Django 3.2.8+, logtail 0.3.3

Proposed Fix

Add a flush_timeout parameter (default 5 seconds) to prevent infinite waiting:

def flush(self):
    self._flushing = True
    start_time = time.time()
    while not self._clean or not self.pipe.empty():
        if time.time() - start_time > self.flush_timeout:
            break  # Exit after timeout
        time.sleep(self.check_interval)
    self._flushing = False

Impact

  • Prevents graceful Django shutdown
  • Affects development workflow and deployment scripts
  • Forces developers to kill processes manually

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions