platformplatform / PlatformPlatform

Alpha state. A platform designed for building enterprise-grade, multi-tenant products using Azure, .NET, React, TypeScript, Infrastructure as Code, etc.
https://platformplatform.github.io
MIT License
257 stars 26 forks source link

Make ConcurrentCommandCounter thread safe #575

Closed PavlovicDzFilip closed 2 months ago

PavlovicDzFilip commented 2 months ago

Summary & Motivation

In rare conditions, the code allows the unit of work and/or the events to not be published at the end of the request.

Multiple threads can decrement the concurrent counter at the same time using Interlocked to ensure atomicity of the operation. However, when reading the data, Interlocked was not used, leading to potential race conditions.

Note: I am aware there is no concurrency involved in here. But the counter itself has partial support for thread safety, and this is addressed in the PR.

Checklist

PavlovicDzFilip commented 2 months ago

This is a simple fix to ensure the methods of ConcurrentCommandCounter class are thread safe.

However, there is a smell about the counter itself. There should never be any concurrency when handling the requests.