Repository: microsoft/garnet
File: libs/server/Objects/ItemBroker/CollectionItemBroker.cs (C#)

Code before:
                {
                    if (!isStarted)
                    {
                        isStartedLock.EnterWriteLock();
                        try
                        {
                            _ = Task.Run(Start);
                            isStarted = true;
                        }
                        finally
                        {
                            isStartedLock.ExitWriteLock();
                        }
                    }
                }
                finally
                {
                    isStartedLock.ExitUpgradeableReadLock();
                }
            }

            var timeout = timeoutInSeconds == 0
                ? TimeSpan.FromMilliseconds(-1)
                : TimeSpan.FromSeconds(timeoutInSeconds);

            var isError = false;
            try
            {
                // Wait for either the result found notification or the timeout to expire
                await observer.ResultFoundSemaphore.WaitAsync(timeout, observer.CancellationTokenSource.Token);
            }
            catch (OperationCanceledException) when (observer.CancellationTokenSource.IsCancellationRequested)
            {
                isError = true;
            }

            SessionIdToObserver.TryRemove(observer.Session.ObjectStoreSessionID, out _);

            // Check if observer is still waiting for result
            if (observer.Status == ObserverStatus.WaitingForResult)
            {
                // Try to set the observer result to an empty one
                observer.HandleSetResult(CollectionItemResult.Empty);
            }

            if (isError)
            {
                return CollectionItemResult.Error;
            }

            return observer.Result;

Review comment (suggestion):
Naming: isError is confusing here, since there's no error actually occurring, should be something like isForceUnblocked.

Code after:

            // Check if main loop has started, if not, start the main loop
            if (!isStarted)
            {
                isStartedLock.EnterUpgradeableReadLock();
                try
                {
                    if (!isStarted)
                    {
                        isStartedLock.EnterWriteLock();
                        try
                        {
                            _ = Task.Run(Start);
                            isStarted = true;
                        }
                        finally
                        {
                            isStartedLock.ExitWriteLock();
                        }
                    }
                }
                finally
                {
                    isStartedLock.ExitUpgradeableReadLock();
                }
            }

            var timeout = timeoutInSeconds == 0
                ? TimeSpan.FromMilliseconds(-1)
                : TimeSpan.FromSeconds(timeoutInSeconds);

            try
            {
                // Wait for either the result found notification or the timeout to expire
                await observer.ResultFoundSemaphore.WaitAsync(timeout, observer.CancellationTokenSource.Token);
            }
            catch (OperationCanceledException)
            {
                // Session is disposed
            }

            SessionIdToObserver.TryRemove(observer.Session.ObjectStoreSessionID, out _);

            // Check if observer is still waiting for result
            if (observer.Status == ObserverStatus.WaitingForResult)
            {
                // Try to set the observer result to an empty one
                observer.HandleSetResult(CollectionItemResult.Empty);
            }

            return observer.Result;