Async notifier does not handle errors
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 38/100
Hướng nghiên cứu
Bắt đầu trong notifier.py với Notifier._on_message_available và AsyncBufferedReader. Theo dõi cách các lỗi từ bus.recv() đến được _on_error() và cách coroutine đang chờ tiêu thụ hàng đợi tin nhắn. Hoàn thành khi các lỗi nhận bất đồng bộ đến được các listener và các coroutine đang chờ có thể xử lý chúng mà không tiếp tục bị chặn, đồng thời vẫn giữ nguyên thứ tự tin nhắn.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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!
- Ngôn ngữ chính
- Python
- Star
- 1.6k
- Fork
- 697
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của hardbyte/python-can
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
hardbyte/python-can#2103 ·
-
bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
hardbyte/python-can#2077 · 1 bình luận · 1 reaction ·
-
bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 68/100
hardbyte/python-can#1922 · 1 reaction ·
-
enhancement
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 30/100
hardbyte/python-can#2102 ·
-
bug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
hardbyte/python-can#2092 ·
Tất cả issue của hardbyte/python-can
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
stephrobert/dsoxlab#238 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
sublimehq/package_control#1780 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
nwg-piotr/nwg-displays#145 ·