Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue] After a few ours running, Dbus throws the org.freedesktop.DBus.Error.LimitsExceeded exception #57

Open
uriel-kluk opened this issue Jan 3, 2025 · 2 comments
Labels
not-reviewed Issue that needs reviewed

Comments

@uriel-kluk
Copy link

Description

The application is designed for a headless device that scans BLE advertisements and pushes them to the cloud via MQTT (message pump). However, after running for a few hours, it crashes with the following exception:

Unhandled exception. Tmds.DBus.DBusException: org.freedesktop.DBus.Error.LimitsExceeded: Connection ":1.290" is not allowed to add more match rules (increase limits in configuration file if required; max_match_rules_per_connection=2048)
at Tmds.DBus.DBusConnection.CallMethodAsync(Message msg, Boolean checkConnected, Boolean checkReplyType)
at Tmds.DBus.DBusConnection.WatchSignalAsync(ObjectPath path, String interface, String signalName, SignalHandler handler)
at Tmds.DBus.Connection.WatchSignalAsync(ObjectPath path, String interface, String signalName, SignalHandler handler)
at Tmds.DBus.CodeGen.DBusObjectProxy.WatchNonVoidSignalAsync[T](String iface, String member, Action'1 error, Action'1 action, ReadMethodDelegate'1 readValue, Boolean isPropertiesChanged)
at Linux.Bluetooth.Device.CreateAsync(IDevice1 proxy)
at Linux.Bluetooth.Adapter.OnDeviceAddedAsync(ValueTuple'2 args)
at System.Threading.Tasks.Task.<>c.b__128_1(Object state)
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
...

Here is the Initialization Code:

private async Task InitializeBleScannerAsync()
{
    if (_adapter != null)
    {
        Logger.LogWarning("Bluetooth adapter already initialized");
        return;
    }

    // Get the list of available Bluetooth adapters
    var adapters = await BlueZManager.GetAdaptersAsync();
    if (adapters.Count == 0)
    {
        throw new InvalidOperationException("No Bluetooth adapters found");
    }

    // Use the first available adapter
    _adapter = adapters[0];

    // Extract the adapter name from the adapter path
    var adapterPath = _adapter.ObjectPath.ToString();
    var adapterName = adapterPath.Substring(adapterPath.LastIndexOf("/", StringComparison.OrdinalIgnoreCase) + 1);
    Logger.LogDebug("Using Bluetooth adapter {AdapterName}", adapterName);


    // Register event handlers for the adapter
    _adapter.PoweredOn += adapter_OnPowerOnCompletedAsync;
    _adapter.PoweredOff += adapter_PoweredOffAsync;
    _adapter.DeviceFound += Adapter_DeviceFound;

    // Power on the adapter
    await _adapter.SetPoweredAsync(true);
}

Here is the adapter_OnPowerOnCompletedAsync event function

    private async Task adapter_OnPowerOnCompletedAsync(Adapter adapter, BlueZEventArgs e)
    {
        try
        {
            if (e.IsStateChange)
            {
                Logger.LogInformation("Bluetooth adapter {Adapter} powered on.", adapter.Name);
            }
            else
            {
                Logger.LogInformation("Bluetooth adapter {Adapter} already powered on.", adapter.Name);
            }

            // do an active registration for all devices
            using var watcher = await adapter.WatchDevicesAddedAsync(dev => _activeScanBlock.Post(new DeviceFoundEventArgs(dev, false)));

            // Set discovery filter, Setting DuplicateData to true ensures that the DeviceFound event is triggered for every advertisement, even for devices already detected.
            Logger.LogDebug("Setting BLE Filter...");
            await adapter.SetDiscoveryFilterAsync(new Dictionary<string, object>
                {
                    { "Transport", "le" }, // Use BLE
                    { "DuplicateData", true }
                });

            Logger.LogDebug("Scanning for devices...");
            await adapter.StartDiscoveryAsync();

        }
        catch (Exception ex)
        {
            Logger.LogError(ex, "Exception occurred in powerOn event.");
        }
    }

And here is the Adapter_DeviceFound event function

    private Task Adapter_DeviceFound(Adapter sender, DeviceFoundEventArgs eventArgs)
    {

        // Instrument and trace
        IncrementMeter(MtrAdvCount);

        // Post the event to the first block
        _activeScanBlock.Post(eventArgs);

        return Task.CompletedTask;
    }

Environment

  • OS Distro: _Ubuntu v22.04
  • Linux.Bluetooth Version: 5.67.1

Severity (1-5)

4=Error: Need to reset the adapter

Steps To Reproduce

keep the adapter scanning for a few hours.

Expected Behavior

No crash

@uriel-kluk uriel-kluk added the not-reviewed Issue that needs reviewed label Jan 3, 2025
@DamianSuess
Copy link
Contributor

Did a search and found there are a few things that may resolve your issue. The nature of the error your running into with DBus is that the connections to the scanned devices are never closed.

You could attempt to increase the DBus connection limit of active connections on your distribution. Or, attempt to close the connection after obtaining the information of the scanned device in WatchDevicesAddedAsync(...).

The increasing the DBus connection limit can be found in numerous posts. This was obtained searching on , "Dbus throws the org.freedesktop.DBus.Error.LimitsExceeded".

For the closing of connections, possibly attempt the following inside of your WatchDevicesAddedAsync(...). Perform an await dev.DisconnectAsync() after obtaining the desired information from your scanned device. These are the connections which are not being closed.

@uriel-kluk
Copy link
Author

uriel-kluk commented Jan 3, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
not-reviewed Issue that needs reviewed
Projects
None yet
Development

No branches or pull requests

2 participants