Skip to content

[BUG] race condition in ToolWatcher #351

Open
@panditkbgh

Description

@panditkbgh

Checks

  • I have updated to the lastest minor and patch version of Strands
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Strands Version

0.1.9

Python Version

3.12

Operating System

macOS 15.5

Installation Method

other

Steps to Reproduce

create multiple instances of Agent() each on separate thread. You will see exception thrown as below:

  File ".../agents.py", line 67, in create_agent
    agent = Agent(
            ^^^^^^
  File ".../python3.12/site-packages/strands/agent/agent.py", line 260, in __init__
    self.tool_watcher = ToolWatcher(tool_registry=self.tool_registry)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../python3.12/site-packages/strands/tools/watcher.py", line 39, in __init__
    self.start()
  File ".../python3.12/site-packages/strands/tools/watcher.py", line 134, in start
    ToolWatcher._shared_observer.start()
  File ".../python3.12/site-packages/watchdog/observers/api.py", line 268, in start
    super().start()
  File ".../python3.12/site-packages/watchdog/utils/__init__.py", line 76, in start
    threading.Thread.start(self)
  File ".../python/3.12.8/lib/python3.12/threading.py", line 989, in start
    raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once

Expected Behavior

no exception should be thrown

Actual Behavior

exception thrown:

  File ".../agents.py", line 67, in create_agent
    agent = Agent(
            ^^^^^^
  File ".../python3.12/site-packages/strands/agent/agent.py", line 260, in __init__
    self.tool_watcher = ToolWatcher(tool_registry=self.tool_registry)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../python3.12/site-packages/strands/tools/watcher.py", line 39, in __init__
    self.start()
  File ".../python3.12/site-packages/strands/tools/watcher.py", line 134, in start
    ToolWatcher._shared_observer.start()
  File ".../python3.12/site-packages/watchdog/observers/api.py", line 268, in start
    super().start()
  File ".../python3.12/site-packages/watchdog/utils/__init__.py", line 76, in start
    threading.Thread.start(self)
  File ".../python/3.12.8/lib/python3.12/threading.py", line 989, in start
    raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once

Additional Context

No response

Possible Solution

add a lock and change shared state after locking

Related Issues

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions