Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

fix(scoring): surface sparse model errors from stop() instead of failing silently (#1403)

Raphael Malikian260b36fb4b9b
diff --git a/src/python/txtai/scoring/sparse.py b/src/python/txtai/scoring/sparse.py
index 64e17b6d..5101618d 100644
--- a/src/python/txtai/scoring/sparse.py
+++ b/src/python/txtai/scoring/sparse.py
@@ -52,6 +52,12 @@ class Sparse(Scoring):
self.batch = self.config.get("batch", 1024)
self.thread, self.queue, self.data = None, None, None
+ # Error raised by the encoding thread, re-raised by stop()
+ self.error = None
+
+ # True once stream() has consumed the COMPLETE message
+ self.streamcomplete = False
+
def insert(self, documents, index=None, checkpoint=None):
# Start processing thread, if necessary
self.start(checkpoint)
@@ -152,12 +158,16 @@ class Sparse(Scoring):
if not self.thread:
self.queue = Queue(5)
+ self.error, self.streamcomplete = None, False
self.thread = Thread(target=self.encode, args=(checkpoint,))
self.thread.start()
def stop(self):
"""
- Stops an encoding processing thread. Return processed results.
+ Stops an encoding processing thread. Returns processed results.
+
+ Raises:
+ Exception: re-raises any exception raised by the encoding thread
Returns:
results
@@ -171,6 +181,12 @@ class Sparse(Scoring):
self.thread.join()
self.thread, self.queue = None, None
+ # Re-raise encoding thread errors, so sparse index/upsert failures
+ # surface the same as the dense path
+ if self.error:
+ error, self.error = self.error, None
+ raise error
+
# Get return value
results = self.data
self.data = None
@@ -185,11 +201,23 @@ class Sparse(Scoring):
checkpoint: checkpoint directory
"""
- # Streaming encoding of data
- _, dimensions, self.data = self.model.vectors(self.stream(), self.batch, checkpoint)
+ try:
+ # Streaming encoding of data
+ _, dimensions, self.data = self.model.vectors(self.stream(), self.batch, checkpoint)
+
+ # Save number of dimensions
+ self.config["dimensions"] = dimensions
+ except Exception as ex: # pylint: disable=broad-exception-caught
+ # Keep the exception to re-raise on stop(). All model errors must be
+ # captured here, so they surface instead of failing silently.
+ self.error = ex
- # Save number of dimensions
- self.config["dimensions"] = dimensions
+ # Drain the queue so a producer blocked on a full queue can't hang.
+ # Skip draining if stream() already consumed the COMPLETE message,
+ # as there is nothing left to read.
+ if not self.streamcomplete:
+ for _ in self.stream():
+ pass
def stream(self):
"""
@@ -201,6 +229,9 @@ class Sparse(Scoring):
yield from batch
batch = self.queue.get()
+ # Flag that the end of stream message was consumed
+ self.streamcomplete = True
+
def normalize(self, queries, scores):
"""
Normalize query results.
diff --git a/test/python/testscoring/testsparse.py b/test/python/testscoring/testsparse.py
index cc44151e..6b23ab71 100644
--- a/test/python/testscoring/testsparse.py
+++ b/test/python/testscoring/testsparse.py
@@ -7,7 +7,8 @@ import platform
import tempfile
import unittest
-from unittest.mock import patch
+from threading import Event
+from unittest.mock import MagicMock, patch
from txtai.scoring import ScoringFactory
@@ -206,6 +207,64 @@ class TestSparse(unittest.TestCase):
self.assertGreater(len(results), 0)
scoring.close()
+ @patch("txtai.scoring.sparse.SparseVectorsFactory.create")
+ def testEncodingThreadError(self, create):
+ """
+ Test encoding thread errors are re-raised by stop()
+ """
+
+ # Model that fails during encoding, before consuming the stream
+ started = Event()
+
+ def vectors(stream, batch, checkpoint): # pylint: disable=unused-argument
+ started.set()
+ raise RuntimeError("model failed")
+
+ create.return_value = MagicMock(vectors=vectors)
+
+ scoring = ScoringFactory.create({"method": "sparse", "path": "sparse-encoder-testing/splade-bert-tiny-nq"})
+
+ # Start encoding thread and queue a document so the error-path drain
+ # loop has a batch to consume, then wait for the model call and stop
+ scoring.start(None)
+ scoring.insert([("0", "test document", None)])
+ self.assertTrue(started.wait(10))
+
+ # Encoding errors surface instead of failing silently
+ with self.assertRaisesRegex(RuntimeError, "model failed"):
+ scoring.stop()
+
+ scoring.close()
+
+ @patch("txtai.scoring.sparse.SparseVectorsFactory.create")
+ def testEncodingThreadErrorLate(self, create):
+ """
+ Test encoding errors raised after stream() consumed the end-of-stream message
+ """
+
+ # Model that consumes the stream, then fails
+ started = Event()
+
+ def vectors(stream, batch, checkpoint): # pylint: disable=unused-argument
+ started.set()
+ for _ in stream:
+ pass
+ raise RuntimeError("late failure")
+
+ create.return_value = MagicMock(vectors=vectors)
+
+ scoring = ScoringFactory.create({"method": "sparse", "path": "sparse-encoder-testing/splade-bert-tiny-nq"})
+
+ # Start encoding thread, wait until the model call is reached, then stop
+ scoring.start(None)
+ self.assertTrue(started.wait(10))
+
+ # Encoding errors surface instead of failing silently
+ with self.assertRaisesRegex(RuntimeError, "late failure"):
+ scoring.stop()
+
+ scoring.close()
+
def testZvecSparse(self):
"""
Test sparse vectors with a zvec backend