Openziti.monkeypatch causes severe slowdown & timeouts with httpx.AsyncClient in async environment

I am facing a performance and timeout problem when using the OpenZiti Python SDK with openziti.monkeypatch() inside an async application.


Environment

  • Python 3.x

  • httpx (async client)

  • OpenZiti Python SDK latest version

  • Running in an async FastAPI app (but reproducible in plain Python async script)

  • Connection works perfectly when NOT using async


Code Example

async with httpx.AsyncClient(timeout=30.0) as client:
    try:
        with openziti.monkeypatch(context=ztx):
            response = await client.post(url, json=payload)


Observed Behavior

:white_check_mark: When using one request:

  • Works fine

  • No timeout

  • Low latency

:cross_mark: When using async/parallel requests:

  • Requests become extremely slow

  • Many calls timeout at 30s

  • CPU usage goes high

  • The slowdown only happens when monkeypatch is active

Expected Behavior

  • openziti.monkeypatch() should work reliably inside async context

  • Multiple concurrent async HTTP calls should not cause blocking / global lock

  • No request should freeze or stall when running in parallel

Request

  • Could you please investigate:

    • Whether the monkeypatch is compatible with httpx.AsyncClient

    • If the monkeypatch creates blocking I/O inside async event loop

    • If there is a known limitation or workaround for async concurrency

    • Whether the Zitified HTTP adapter needs an async-safe implementation

@ekoby hi sorry to tag you. Will you be able support this? This is kind of blocker for me.

Sorry for the delay, between dealing with some customer issues and the long holiday weekend this was not on top of my list.

the first thing that comes to mind -- I believe this pattern is not well supported:

  • monkeypatch is a fairly involved process, using with(monkeypatch) pattern has to setup and unwind for every call, and multiple calls happening in parallel (this is definitely not well tested)
  • you should be able to call monkeypatch once in your app (assuming you have a single ziti context)

let me know if behavior improves or not if you do it once

With single call it works fine im already using it for long time, but recently i want to enforce SDK to my backend app too but i faced this issue.

can you setup monkeypatch at the start of the program instead of wrapping each HTTP request with it?

We tried that too, it is getting timedout