Async notifier does not handle errors
還沒有人認領這個 Issue。
評估
研究方向
從 notifier.py 中的 Notifier._on_message_available 和 AsyncBufferedReader 開始。追蹤 bus.recv() 的錯誤如何到達 _on_error(),以及正在等待的 coroutine 如何消費訊息佇列。當非同步接收錯誤能夠到達 listener,且等待中的 coroutine 能夠處理這些錯誤而不會一直處於阻塞狀態,同時保持訊息順序時,即完成。
由索引模型根據 Issue 內容生成。
描述
Greetings! I'm using python-can to talk to my inverter's battery.
Describe the bug
In class Notifier:
def _on_message_available(self, bus: BusABC) -> None:
if msg := bus.recv(0):
self._on_message_received(msg)
When an exception occurs, for example the CAN interface going down or being unplugged, bus.recv() raises but the exception is not handled. The threaded version does call _on_error(), so I guess the async version should too. I fixed it by inheriting the class, but these 3 lines of code would be better in notifier.py.
class Notifier_e (can.Notifier):
def _on_message_available(self, bus):
try:
super()._on_message_available(bus)
except Exception as exc:
if not self._on_error(exc):
# If it was not handled, raise the exception here
raise
Then _on_error() passes the exception to the listeners, for example AsyncBufferedReader, but it doesn't handle exceptions either: instead the coroutine waiting for messages simply stays stuck forever. So I extended it in the same way:
class AsyncBufferedReader_e( can.AsyncBufferedReader ):
def on_error( self, exc ):
self.buffer.put_nowait( exc )
async def __anext__(self):
m = await self.buffer.get()
if isinstance( m, BaseException ):
raise m
return m
It puts the exception into the queue with messages, so when the coroutine waiting on these messages gets to the exception, it is raised, and the coroutine can process it. This preserves order between messages and exceptions, so messages received and pushed into the queue before the exception occurs will be processed.
I use it to cleanly disconnect the CAN interface, then connect again when it is plugged back into the USB port.
I'm not sure about Exception vs BaseException. I guess it would be useful to pass KeyboardInterrupt to the waiting coroutine, so it would be terminated in the same way as a Ctrl-C occurring during a blocking recv().
Have a nice day!
- 主要語言
- Python
- 星號
- 1.6k
- 分支
- 701
- PR 合併指標
- 30 天內沒有已合併 PR
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
hardbyte/python-can 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 88/100
hardbyte/python-can#2103 ·
-
Unhashable type for Systec interface errors可能已有人在做 @giri256 於 78 天前認領。 未關閉bug
難度 1/5 1 小時以內 新手友好度 78/100
hardbyte/python-can#2077 · 1 則留言 · 1 個 reaction ·
-
bug
難度 1/5 1 小時以內 新手友好度 68/100
hardbyte/python-can#1922 · 1 個 reaction ·
-
Make socket binding exceptions more debugable可能已有人在做 @vonpanda 於 6 天前認領。 未關閉enhancement
難度 3/5 1-2 天 新手友好度 75/100
hardbyte/python-can#2104 ·
-
enhancement
難度 5/5 一週以上 新手友好度 30/100
hardbyte/python-can#2102 ·
查看 hardbyte/python-can 的全部 Issue
相似的 Issue
-
enhancement from-review
難度 2/5 1-3 小時 新手友好度 78/100
khuisman/mcp-gee-sweet#1010 ·
維護者通常 1 天內回覆
-
bug confirmed issue
難度 2/5 1-3 小時 新手友好度 72/100
open-webui/open-webui#32333 · 2 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 68/100
ll7/robot_sf_ll7#10368 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 62/100
-
難度 2/5 1-3 小時 新手友好度 68/100
flop-labs/technocore-chat#982 ·
維護者通常 1 天內回覆