Skip to content

Python: Bug: TypeError: cannot pickle '_thread.RLock' object when cloning Kernel with OpenTelemetry enabled #12802

@MMoshtaghi

Description

@MMoshtaghi

Describe the bug
When using features that internally clone a semantic_kernel.Kernel instance (such as agent orchestrations like HandoffOrchestration), a TypeError is raised if OpenTelemetry metrics are enabled. The error, TypeError: cannot pickle '_thread.RLock' object, originates from a deepcopy operation on the kernel's plugins.

The root cause appears to be the invocation_duration_histogram and streaming_duration_histogram fields on the KernelFunction model. These fields are OpenTelemetry Histogram objects which contain thread locks and are therefore not serializable or deep-copyable.

While there have been previous attempts to fix a similar error, such as in PR #9292, by marking the fields with exclude=True and providing a default_factory, this does not prevent copy.deepcopy() from attempting to copy them, leading to the crash.

To Reproduce
Add observability ( you can use the sample in MS Learn ) to an agent orchestration that clones the kernel for its members, for example HandoffOrchestration

Expected behavior
The kernel should be cloned successfully without raising a TypeError, allowing orchestrations and other features to work correctly with observability enabled.

Screenshots
Not applicable

Stack Trace

Traceback (most recent call last):
  File "...", in <module>
    asyncio.run(main())
  File "...", in main
    await handoff.invoke("test")
  File "/.../semantic_kernel/agents/orchestration/orchestrator_base.py", line 125, in invoke
    ...
  File "/.../semantic_kernel/agents/orchestration/handoffs.py", line 174, in __init__
    self._kernel = agent.kernel.clone()
  File "/.../semantic_kernel/kernel.py", line 541, in clone
    plugins=deepcopy(self.plugins),
  File "/.../copy.py", line 136, in deepcopy
    y = copier(x, memo)
  ...
  File "/.../copy.py", line 151, in deepcopy
    rv = reductor(4)
TypeError: cannot pickle '_thread.RLock' object

Platform

  • Python version: 3.12
  • Semantic-Kernel version: >=1.32.2
  • OS: Mac

Metadata

Metadata

Assignees

Labels

agentsbugSomething isn't workingpythonPull requests for the Python Semantic Kernel

Type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions