Skip to content

[Bugfix] Fix PrometheusStatsLogger crash on re-instantiation and non-daemon thread#905

Open
SuperMarioYL wants to merge 1 commit intoModelEngine-Group:developfrom
SuperMarioYL:fix/observability-init-and-daemon-thread
Open

[Bugfix] Fix PrometheusStatsLogger crash on re-instantiation and non-daemon thread#905
SuperMarioYL wants to merge 1 commit intoModelEngine-Group:developfrom
SuperMarioYL:fix/observability-init-and-daemon-thread

Conversation

@SuperMarioYL
Copy link
Copy Markdown
Contributor

Purpose

Fix two functional bugs in PrometheusStatsLogger that cause crashes in multi-engine scenarios and prevent clean process shutdown.

Modifications

Bug 1: AttributeError on re-instantiation

When _metric_mappings is already populated (e.g. multi-engine deployment or hot-reload), __init__ returns early at line 71 before initializing any instance attributes (is_running, thread, config, log_interval, etc.).

Any subsequent call to shutdown() or the __del__ destructor raises AttributeError: 'PrometheusStatsLogger' object has no attribute 'is_running'.

Fix: Move self.config, self.log_interval, self.is_running, and self.thread initialization before the early-return guard. The second instance becomes a safe no-op that can be cleanly shut down.

Bug 2: Non-daemon update_stats_loop thread blocks process exit

The background thread is created without daemon=True. If shutdown() is never explicitly called (e.g. crash, unclean teardown, __del__ not invoked by GC), this thread keeps the Python process alive indefinitely.

Fix: Set daemon=True on the thread. Also guard shutdown() with if self.thread is not None to handle the re-instantiation case safely.

Test

  • black --check and isort --check --profile=black pass on the modified file.
  • Both bugs are deterministic: Bug 1 triggers on any second instantiation, Bug 2 triggers on any unclean shutdown.

…daemon thread

Two bugs in observability.py:

1. When _metric_mappings is already populated (e.g. multi-engine or
   hot-reload), __init__ returns early without initializing instance
   attributes (is_running, thread, config, etc.). Any subsequent call
   to shutdown() or __del__() raises AttributeError. Fix: always
   initialize core instance attributes before the early-return guard.

2. The update_stats_loop thread is not created as a daemon thread.
   If shutdown() is never explicitly called, this thread prevents the
   Python process from exiting cleanly. Fix: set daemon=True, and
   guard shutdown() against thread being None.

Signed-off-by: supermario_leo <leo.stack@outlook.com>
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

Successfully merging this pull request may close these issues.

1 participant