petter2025 commited on
Commit
d5d92b1
·
verified ·
1 Parent(s): 9722336

Update app.py

Browse files

Fix FAISS thread initialization race condition

Files changed (1) hide show
  1. app.py +4 -3
app.py CHANGED
@@ -240,6 +240,9 @@ class ProductionFAISSIndex:
240
  self.texts = texts
241
  self._lock = threading.RLock()
242
 
 
 
 
243
  # Single writer thread (no concurrent write conflicts)
244
  self._write_queue: Queue = Queue()
245
  self._writer_thread = threading.Thread(
@@ -247,13 +250,11 @@ class ProductionFAISSIndex:
247
  daemon=True,
248
  name="FAISSWriter"
249
  )
250
- self._writer_thread.start()
251
 
252
  # ProcessPool for encoding (avoids GIL + memory leaks)
253
  self._encoder_pool = ProcessPoolExecutor(max_workers=2)
254
 
255
- self._shutdown = threading.Event()
256
-
257
  logger.info(
258
  f"Initialized ProductionFAISSIndex with {len(texts)} vectors, "
259
  f"single-writer pattern"
 
240
  self.texts = texts
241
  self._lock = threading.RLock()
242
 
243
+ # FIXED: Initialize shutdown event BEFORE starting thread
244
+ self._shutdown = threading.Event()
245
+
246
  # Single writer thread (no concurrent write conflicts)
247
  self._write_queue: Queue = Queue()
248
  self._writer_thread = threading.Thread(
 
250
  daemon=True,
251
  name="FAISSWriter"
252
  )
253
+ self._writer_thread.start() # ← Only start ONCE, AFTER _shutdown exists
254
 
255
  # ProcessPool for encoding (avoids GIL + memory leaks)
256
  self._encoder_pool = ProcessPoolExecutor(max_workers=2)
257
 
 
 
258
  logger.info(
259
  f"Initialized ProductionFAISSIndex with {len(texts)} vectors, "
260
  f"single-writer pattern"