Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behavior with multiple notifiers #1888

Open
bessman opened this issue Nov 5, 2024 · 3 comments · May be fixed by #1890
Open

Unexpected behavior with multiple notifiers #1888

bessman opened this issue Nov 5, 2024 · 3 comments · May be fixed by #1890
Labels

Comments

@bessman
Copy link
Contributor

bessman commented Nov 5, 2024

Describe the bug

When there is more than one active Notifier, only one instance receives an incoming message. Which notifier gets the message seems random.

To Reproduce

# Do this first to set up two virtual can buses with bidirectional gating:
# sudo modprobe vcan
# sudo ip link add dev vcan0 type vcan
# sudo ip link add dev vcan1 type vcan
# sudo ip link set vcan0 up
# sudo ip link set vcan1 up
# sudo modprobe can-gw
# sudo cangw -A -s vcan0 -d vcan1 -e
# sudo cangw -A -s vcan1 -d vcan0 -e

import can

class NamedPrinter(can.Printer):
    def __init__(self, name: str, *args, **kwargs) -> None:
        self.name = name
        super().__init__(*args, **kwargs)

    def on_message_received(self, msg: can.Message) -> None:
        print(f"Printer {self.name}")
        super().on_message_received(msg)

vbus0 = can.Bus(interface="socketcan", channel="vcan0")
vbus1 = can.Bus(interface="socketcan", channel="vcan1")

not1 = can.Notifier(vbus1, [NamedPrinter("A")])
not2 = can.Notifier(vbus1, [NamedPrinter("B")])

msg = can.Message(arbitration_id=0x11, data=b"hello")

for i in range(4):
    vbus0.send(msg)

# Output:
# Printer A
# Timestamp: 1730815787.280445    ID: 00000011    X Rx                DL:  5    68 65 6c 6c 6f              'hello'    Channel: vcan1
# Printer B
# Timestamp: 1730815787.280519    ID: 00000011    X Rx                DL:  5    68 65 6c 6c 6f              'hello'    Channel: vcan1
# Printer A
# Timestamp: 1730815787.280466    ID: 00000011    X Rx                DL:  5    68 65 6c 6c 6f              'hello'    Channel: vcan1
# Printer B
# Timestamp: 1730815787.280531    ID: 00000011    X Rx                DL:  5    68 65 6c 6c 6f              'hello'    Channel: vcan1

Expected behavior

I expected all notifiers to receive all incoming messages.

If multiple simultaneous notifiers on the same bus is not supported, the documentation should mention this. Ideally, a warning or error should be raised if an additional notifier is created on a bus with an already active notifier.

Additional context

OS and version: Ubuntu 20.04
Python version: 3.13.0
python-can version: 4.4.2
python-can interface/s (if applicable): socketcan

@bessman bessman added the bug label Nov 5, 2024
@zariiii9003
Copy link
Collaborator

A single notifier can handle multiple bus instances and multiple listeners:

notifier = can.Notifier([vbus1, vbus2], [NamedPrinter("A"), NamedPrinter("B")])

@bessman
Copy link
Contributor Author

bessman commented Nov 5, 2024

Yes, I know, but I still find this behavior surprising. Wouldn't it make sense to make note of it in the documentation, at least?

On a related note, is there any way to clean up orphaned notifiers? For example:

notifier = can.Notifier(vbus1, [NamedPrinter("A")])
notifier = can.Notifier(vbus1, [NamedPrinter("B")])
notifier.stop()
vbus0.send(msg)
# Output:
# Printer A
# Timestamp: 1730841762.033267    ID: 00000011    X Rx                DL:  5    68 65 6c 6c 6f              'hello'    Channel: vcan1

@zariiii9003
Copy link
Collaborator

Take a look at #1890

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants