Skip to content

The agent engineering platform.

fix(prompty): add thread-safety to InvokerFactory singleton (#34983)

Sourab Singh Bora631ca011f41e
diff --git a/libs/partners/prompty/langchain_prompty/core.py b/libs/partners/prompty/langchain_prompty/core.py
index 90d6579564..b939255938 100644
--- a/libs/partners/prompty/langchain_prompty/core.py
+++ b/libs/partners/prompty/langchain_prompty/core.py
@@ -4,6 +4,7 @@ import abc
import json
import os
import re
+import threading
from pathlib import Path
from typing import Any, Generic, Literal, TypeVar
@@ -200,22 +201,31 @@ class NoOpParser(Invoker):
class InvokerFactory:
- """Factory for creating invokers."""
+ """Factory for creating invokers.
+
+ This class implements a thread-safe singleton pattern using double-checked
+ locking to ensure only one instance is created even under concurrent access.
+ """
_instance = None
+ _lock = threading.Lock()
_renderers: dict[str, type[Invoker]] = {}
_parsers: dict[str, type[Invoker]] = {}
_executors: dict[str, type[Invoker]] = {}
_processors: dict[str, type[Invoker]] = {}
def __new__(cls) -> InvokerFactory:
+ # First check (fast path, no lock needed)
if cls._instance is None:
- cls._instance = super().__new__(cls)
- # Add NOOP invokers
- cls._renderers["NOOP"] = NoOpParser
- cls._parsers["NOOP"] = NoOpParser
- cls._executors["NOOP"] = NoOpParser
- cls._processors["NOOP"] = NoOpParser
+ with cls._lock:
+ # Double-check after acquiring lock
+ if cls._instance is None:
+ cls._instance = super().__new__(cls)
+ # Add NOOP invokers
+ cls._renderers["NOOP"] = NoOpParser
+ cls._parsers["NOOP"] = NoOpParser
+ cls._executors["NOOP"] = NoOpParser
+ cls._processors["NOOP"] = NoOpParser
return cls._instance
def register(
diff --git a/libs/partners/prompty/tests/unit_tests/test_invoker_thread_safety.py b/libs/partners/prompty/tests/unit_tests/test_invoker_thread_safety.py
new file mode 100644
index 0000000000..1a0057df82
--- /dev/null
+++ b/libs/partners/prompty/tests/unit_tests/test_invoker_thread_safety.py
@@ -0,0 +1,62 @@
+"""Test thread-safety of InvokerFactory singleton pattern."""
+
+import threading
+
+from langchain_prompty.core import InvokerFactory
+
+
+def test_invoker_factory_singleton_thread_safety() -> None:
+ """Test that InvokerFactory maintains singleton pattern under concurrent access.
+
+ This test verifies the fix for issue #34981 by ensuring that multiple threads
+ accessing InvokerFactory simultaneously all receive the same instance.
+ """
+ # Reset singleton for testing
+ InvokerFactory._instance = None
+
+ results: list[int] = []
+
+ def thread_worker() -> None:
+ """Worker function that gets InvokerFactory instance."""
+ factory = InvokerFactory()
+ results.append(id(factory))
+
+ # Create 10 threads to access the singleton concurrently
+ threads = [threading.Thread(target=thread_worker) for _ in range(10)]
+
+ # Start all threads
+ for thread in threads:
+ thread.start()
+
+ # Wait for all threads to complete
+ for thread in threads:
+ thread.join()
+
+ # Verify all threads received the same instance
+ unique_instances = set(results)
+ assert len(unique_instances) == 1, (
+ f"Expected 1 unique instance, but got {len(unique_instances)}. "
+ f"Race condition detected!"
+ )
+
+
+def test_invoker_factory_singleton_consistency() -> None:
+ """Test that multiple sequential calls return the same instance."""
+ factory1 = InvokerFactory()
+ factory2 = InvokerFactory()
+ factory3 = InvokerFactory()
+
+ assert factory1 is factory2
+ assert factory2 is factory3
+ assert id(factory1) == id(factory2) == id(factory3)
+
+
+def test_invoker_factory_noop_registrations() -> None:
+ """Test that NOOP invokers are properly registered."""
+ factory = InvokerFactory()
+
+ # Verify NOOP invokers are registered
+ assert "NOOP" in factory._renderers
+ assert "NOOP" in factory._parsers
+ assert "NOOP" in factory._executors
+ assert "NOOP" in factory._processors