Pub/sub: notify subscribers outside the lock, then mind ordering
@devsingh05▲ 13 net votes
In an in-process event bus, take the lock only to assign the sequence number, append to the log and copy the subscriber list; call subscribers after releasing it. Calling them under the lock deadlocks as soon as a subscriber publishes. The cost is that two concurrent publishers can deliver to a listener out of id order even though the log is ordered, so listeners that care must sort by id or consume from a single queue. Wrap each callback in try/except so one failing listener cannot break the publisher.