2

Closed

Message Publish within a Subscribe Error

description

When Publishing a new Message on the Bus within a Subscribe, a "Collection was modified; enumeration operation may not execute." error occurs.
Example:
Messages.Subscribe<Widget1>(w =>
{
    Messages.Publish(new Widget2());
});
Closed Mar 26, 2011 at 12:03 AM by tathamoddie

comments

tathamoddie wrote Mar 14, 2011 at 12:30 PM

Hi Craig,

I can't reproduce this in the source. Tested against cbdf64bacd4e.

I've added a new test in 0318aadcf8fb to cover this scenario anyway:

[TestMethod]
public void MessageCoordinator_Publish_ShouldAcceptNewMessagesWithinACallback()
{
// Arrange
var coordinator = new MessageCoordinator();

// Act
var intReceived = false;
coordinator.Subscribe<int>(m => { intReceived = m == 123; });
coordinator.Subscribe<string>(m => coordinator.Publish(123));
coordinator.Publish("message");

// Assert
Assert.IsTrue(intReceived);
}

-- Tatham

craigathon wrote Mar 15, 2011 at 4:39 AM

Hi Tatham,
I was able to reproduce by executing the publish before the subscribe. So basically just flip the lines around.

coordinator.Publish("message");
coordinator.Subscribe<string>(m => coordinator.Publish(123));

-Craig

tathamoddie wrote Mar 15, 2011 at 7:35 AM

Ah - it happens when we're playing back old messages (aka, the subscribe happens after the published).

Fixed + test added in f22fc40cd33e.

Thanks for your diagnosis. :)

tathamoddie wrote Mar 26, 2011 at 12:01 AM

Fix released in v1.1.

-- Tatham