Sitecore Pipelines – Thread Safety

This might not be a widely common knowledge, but when creating your own Sitecore pipeline processors you need to make sure that your code is thread safe as by default the pipeline processors will be created as a singleton.

I’ve encountered it before a few years ago and recently stumbled upon it again. Thus this blog post serve as a reminder for my future self.

Take a look at the following example

When testing locally with a single user the issue won’t become apparent as there’s usually only a single request that will trigger the code block but given enough volume of incoming requests then you would start seeing a weird behavior where the result is being overridden by a different thread.

The reason why by default Sitecore choose to create the pipeline processor to be created as a Singleton most likely is due to performance, though I wish this was officially documented.

Another way to re-mediate the issue is by changing the object life cycle from Singleton to Transient, by doing this Sitecore will create a new instance of the processor class whenever the pipeline processor is executed.  This can be done by setting the reusable property to false.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.