Skip to content

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

Closed

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

Activity

  1. dbartenstein commented on Sep 29, 2025

    @dbartenstein
  2. dbartenstein commented on Oct 13, 2025

    @dbartenstein

    @PetrHeinz what’s your take on this?

  3. PetrHeinz commented on Oct 13, 2025

    @PetrHeinz
    Member

    Hi @dbartenstein,

    Thank you for such a detailed report 🙌 We have recently added a configurable timeout in v0.3.4, which should make sure that server doesn't hang indefinitely.

    Would that solve this issue as well? Would you be so kind as to upgrade from v0.3.3 🙏

    The default timeout might still be a little too high for the development workflow though... What do you think?

  4. dbartenstein commented on Oct 13, 2025

    @dbartenstein

    Hi @dbartenstein,

    Thank you for such a detailed report 🙌 We have recently added a configurable timeout in v0.3.4, which should make sure that server doesn't hang indefinitely.

    Would that solve this issue as well? Would you be so kind as to upgrade from v0.3.3 🙏

    The default timeout might still be a little too high for the development workflow though... What do you think?

    Wow @PetrHeinz,
    thanks for your swift reply! 🙇 That’s highly appreciated.

    The Kudos go out to @vikram-typeset for the detailed report - I just sent a ping ;-)

    We’re currently using Mezmo (previously logdna) and face this issue:

    Unfortunately there has been no reaction by Mezmo. And yes, we’re currently evaluating betterstack :-)
    As soon as we try out betterstack with Django, we’ll give you an update whether v0.3.3 does the job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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