Skip to content

SYNC PUB-SUB dropping messages? #358

@ocaballeror

Description

@ocaballeror

Continuing the discussion from #357

Consider the following code:

import time
from osbrain import run_nameserver, run_agent, Agent

def log_msg(agent, msg):
    agent.log_info('Received: %s' % msg)

class ListAgent(Agent):
    def on_init(self):
        self.received = []
    
    def append(self, msg):
        self.log_info('Received %s' % msg)
        self.received.append(msg)

    def idle(self):
        self.log_info('Nothing to process')

class MasterAgent(Agent):
    def on_init(self):
        self.both_received = False
        self.addr = self.bind("SYNC_PUB", alias='main', handler=log_msg)

    def start(self):
        agent1 = run_agent('Agent1', base=ListAgent)
        agent2 = run_agent('Agent2', base=ListAgent)
        agent1.connect(self.addr, handler="append")
        agent2.connect(self.addr, handler="append")
        self.send('main', 'Hello world')
        time.sleep(2)
        recv1 = bool(agent1.get_attr('received'))
        recv2 = bool(agent2.get_attr('received'))
        self.both_received = recv1 and recv2

if __name__ == "__main__":
    ns = run_nameserver()
    master = run_agent('Master', base=MasterAgent)
    master.start()
    try:
        assert master.get_attr('both_received')
    finally:
        ns.shutdown()

I tried extending the sleep time up to 10 seconds to give the message a little bit more time to be sent, but it didn't work, sometimes the subscriber doesn't ever get the message. Here are some weird things I noticed that I can't find an explanation for:

  • If the publisher sends a second message, the subscribers receive it just fine.
  • Removing the master agent class, and putting all that code directly in the "main" block solves the issue.
  • The subscriber missing the publication is always the second one to be created. Changing the order in which they connect doesn't seem to make a difference.
  • I've never run into a situation where none of the agents get the message. It is always one or both.

I am still confused 😕

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions