public async Task RemoveMethodCallbackAsync()

in DeviceBridge/Services/ConnectionManager.cs [661:694]


        public async Task RemoveMethodCallbackAsync(string deviceId)
        {
            _logger.Info("Attempting remove method handler for device {deviceId}", deviceId);

            // We need to synchronize this with client creation/close so a race condition doesn't cause us to add the
            // callback to a client that is being currently created but not yet in the clients list.
            var mutex = _clientSemaphores.GetOrAdd(deviceId, new SemaphoreSlim(1, 1));
            await mutex.WaitAsync();

            try
            {
                _logger.Info("Acquired connection lock for device {deviceId}", deviceId);

                // Remove the callback so it's not registered in new clients
                if (_methodCallbacks.TryRemove(deviceId, out _))
                {
                    if (!_clients.TryGetValue(deviceId, out DeviceClient client))
                    {
                        _logger.Info("Connection for device {deviceId} not found while trying to remove method callback", deviceId);
                        return;
                    }

                    await client.SetMethodDefaultHandlerAsync(null, null);
                }
                else
                {
                    _logger.Info("Tried to remove method handler for device {deviceId}, but a handler was not registered", deviceId);
                }
            }
            finally
            {
                mutex.Release();
            }
        }