Skip to content

Commit eed95ca

Browse files
committed
fix(logging): Ensure thread safety by initializing running attribute and using getattr to avoid AttributeError
1 parent 101140e commit eed95ca

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

crossbar_llm/backend/main.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,24 @@ def __init__(self):
122122
super().__init__()
123123
# Create a thread-safe queue for log messages
124124
self.log_messages = queue.Queue()
125+
# Initialize running attribute before starting the thread
126+
self.running = True
125127
# Start a worker thread to process log messages
126128
self.worker_thread = threading.Thread(target=self._worker, daemon=True)
127129
self.worker_thread.start()
128-
self.running = True
129130

130131
def emit(self, record):
131-
if not self.running:
132+
# Use getattr with default to avoid AttributeError
133+
if not getattr(self, 'running', True):
132134
return
133135
log_entry = self.format(record)
134136
# Add to the thread-safe queue
135137
self.log_messages.put(log_entry)
136138

137139
def _worker(self):
138140
"""Worker thread that transfers messages from the thread-safe queue to the asyncio queue."""
139-
while self.running:
141+
# Use getattr with default to avoid AttributeError
142+
while getattr(self, 'running', True):
140143
try:
141144
# Get message from the thread-safe queue (blocking with timeout)
142145
log_entry = self.log_messages.get(timeout=0.5)
@@ -175,8 +178,9 @@ def _worker(self):
175178
continue
176179

177180
def close(self):
181+
# Set running to False
178182
self.running = False
179-
if self.worker_thread.is_alive():
183+
if hasattr(self, 'worker_thread') and self.worker_thread.is_alive():
180184
self.worker_thread.join(timeout=1.0)
181185
super().close()
182186

0 commit comments

Comments
 (0)