Possible issue: ThreadsafeProxy silently returns None when proxied event loop is closed, causing 'NoneType' object can't be awaited downstream

Đang mở
#722 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
python
Lĩnh vực
backend, networking

Hướng nghiên cứu

Start by reading bellows/thread.py, especially ThreadsafeProxy.__getattr__ and its func_wrapper; compare the closed-loop path with the dispatch to asyncio.run_coroutine_threadsafe. The issue describes the expected failure and points to PR #720 for the proposed behavior. Confirm the proxy raises a useful exception when dispatch cannot proceed, including the loop-closes-during-dispatch race, and add focused coverage for those cases.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

⚠️ This is an investigation, not a fresh confirmed reproduction. I read the code path and matched it against an existing in-the-wild traceback. Filing as a tracking issue since the fix was only proposed inside the bundled zigpy/bellows#720 (since closed) and there is no single issue covering it.

HA core home-assistant/core#168432 (April 2026, SLZB-06M over the network, Python 3.14, bellows 0.49.0) includes:

File "/usr/local/lib/python3.14/site-packages/bellows/ezsp/__init__.py", line 224, in disconnect
    await self._gw.disconnect()
TypeError: 'NoneType' object can't be awaited

bellows/ezsp/__init__.py:221-225 already guards if self._gw: before the await:

async def disconnect(self):
    self.stop_ezsp()
    if self._gw:
        await self._gw.disconnect()
        self._gw = None

So self._gw itself is not the None. The None is the return value of self._gw.disconnect(). _gw is a ThreadsafeProxy, so the call routes through ThreadsafeProxy.__getattr__ (bellows/thread.py:81-110):

def func_wrapper(*args, **kwargs):
    loop = self._obj_loop
    curr_loop = asyncio.get_running_loop()
    call = functools.partial(func, *args, **kwargs)
    if loop == curr_loop:
        return call()
    if loop.is_closed():
        # Disconnected
        LOGGER.warning("Attempted to use a closed event loop")
        return                              # ← silent None
    if asyncio.iscoroutinefunction(func):
        future = asyncio.run_coroutine_threadsafe(call(), loop)
        return asyncio.wrap_future(future, loop=curr_loop)
    else:
        ...

If the proxied EventLoopThread's loop is closed (i.e. the worker thread has already exited or its loop has been closed) at the moment of the call, the proxy logs a warning and returns plain None. An async caller doing await proxy.some_async_method() then awaits None and gets TypeError: 'NoneType' object can't be awaited. The error propagates up _async_initialize → shutdown → disconnect and leaves ZHA in an indefinite retry loop, since the cleanup itself fails.

There is also a small race between the loop.is_closed() check and run_coroutine_threadsafe(...) (the loop can become closed in between), which would today raise from run_coroutine_threadsafe and is otherwise survivable, but worth addressing in the same place.

Status of the fix

The original in-flight PR, zigpy/bellows#720 (a larger bundle by silenthooligan), was closed without merging on 2026-08-08; its author dropped the ThreadsafeProxy part because it overlaps zigpy/bellows#730 and zigpy/bellows#741. The silent-None branch is still on dev today. Open work on the same path:

  • zigpy/bellows#730: handle async proxy calls after the event loop closes
  • zigpy/bellows#741: handle the loop closing between the is_closed() check and dispatch
  • zigpy/bellows#745 (issue): connection_lost() tears down the worker loop before notifying the application, so disconnect() races the teardown
  • zigpy/bellows#751: stop using the EZSP gateway once its transport has closed
  • zigpy/bellows#753: RFC to replace ThreadsafeProxy with ThreadedGateway
Field evidence

A second in-the-wild report with the same signature, from HA 2026.10 on Python 3.14 (SLZB bridge, home-assistant/core#185517, comment of 2026-11-04): bellows.thread logs Attempted to use a closed event loop (7 occurrences), then TypeError: 'NoneType' object can't be awaited is raised both from _gw.send_data(...) in protocol.py command (reached via the watchdog's read_counters) and from _gw.disconnect() in ezsp/__init__.py disconnect, logged as Failed to disconnect from radio. That matches the closed-loop branch described above and shows the None return is not limited to disconnect(): every proxied async call (send_data included) can hit it. The surrounding thread is mostly about SLZB bridge stalls and GC pauses, so the closed-loop error appears there as a consequence of the connection dropping, not as the root cause of the stall.

What I have not verified
  • I have not produced a fresh reproducer log. The tracebacks are from the home-assistant/core#168432 and home-assistant/core#185517 reports.
  • On a current Python 3.14.4 install I tested, asyncio.iscoroutinefunction works (it emits a DeprecationWarning but still returns the right boolean), so the proxy's coroutine-detection path itself is functioning. The closed-loop branch is the path most likely firing in the reported case, triggered by an ESPHome serial-bridge connection blip on the SLZB-06M.
Ngôn ngữ chính
Python
Star
221
Fork
104
Merge trung bình
5 ngày 15 giờ
Pull request đã merge (30 ngày)
4

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của zigpy/bellows

Tất cả issue của zigpy/bellows

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.