Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions application_sdk/decorators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from application_sdk.inputs import Input

logger = get_logger(__name__)


executor = ThreadPoolExecutor()


Expand Down
2 changes: 2 additions & 0 deletions application_sdk/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import threading
from typing import Any, List, Sequence

import uvloop
from temporalio.types import CallableType
Comment on lines +11 to 12
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix potential thread safety issues with uvloop initialization.

Setting the event loop policy at module level can lead to race conditions when using threading. The worker creates daemon threads that run their own event loops, which could conflict with the global policy setting.

Move the uvloop initialization into the worker thread:

-asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

 class Worker:
     async def start(self, daemon: bool = False, *args: Any, **kwargs: Any) -> None:
         if daemon:
             worker_thread = threading.Thread(
-                target=lambda: asyncio.run(self.start(daemon=False)), daemon=True
+                target=lambda: self._run_worker_with_uvloop(), daemon=True
             )
             worker_thread.start()
             return

+    def _run_worker_with_uvloop(self):
+        asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
+        asyncio.run(self.start(daemon=False))

Also applies to: 18-19


from application_sdk.clients.temporal import TemporalClient
from application_sdk.common.logger_adaptors import get_logger

logger = get_logger(__name__)
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the event loop policy sets the event loop at all the asyncio instances in the application sdk and in the apps, hence decided to keep it here as worker is used in all the apps



class Worker:
Expand Down
Loading