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

D-Bus Proxy: addListener mulitple times (3) to the same event, emits the D-Bus signal mulitple times (3 * 3) #64

Closed
ManuW opened this issue Feb 22, 2021 · 0 comments · Fixed by #66

Comments

@ManuW
Copy link

ManuW commented Feb 22, 2021

I have a simple D-Bus service. The service has only one signal called test and the service emits every ten seconds the current date.

import * as dbus from 'dbus-next';

class MyInterface extends dbus.interface.Interface {

    /* https://github.com/dbusjs/node-dbus-next/blob/master/lib/service/interface.js#L266 */
    static configureDBusIntf() {
        MyInterface.configureMembers({
            signals: {
                test: {
                    signature: 's'
                }
            }
        });
    }

    constructor() {
        super('org.test.iface');
    }

    test(msg: string) {
        return msg;
    }
}

async function main() {
    MyInterface.configureDBusIntf();
    const example = new MyInterface();

    const bus = dbus.sessionBus();
    await bus.requestName('org.test.test', 0);
    bus.export('/org/test/test', example);

    setInterval(
        () => {
            const date = new Date();
            console.log(date);
            example.test(date.toUTCString());
        },
        10000
    );
}

main().catch((err) => {
    console.log('Error:' + err);
});

/*
$ node dist/dbus_service.js 
2021-02-16T00:17:52.457Z
2021-02-16T00:18:02.469Z
2021-02-16T00:18:12.480Z
*/

The client adds a listener to the test D-Bus signal three times => Instead of receiving the signal three times the client receives the signal nine times!

import * as dbus from 'dbus-next';

async function main() {

    const log1 = (str: string) => { console.log('1  ', str) };
    const log2 = (str: string) => { console.log(' 2 ', str) };
    const log3 = (str: string) => { console.log('  3', str) };

    const bus = dbus.sessionBus();
    const obj = await bus.getProxyObject('org.test.test', '/org/test/test');
    const testIface = obj.getInterface('org.test.iface');
    testIface.on('test', log1);
    testIface.on('test', log2);
    testIface.on('test', log3);
}

main().catch((err) => {
    console.log('Error:' + err);
});

/*
$ node dist/dbus_client.js
1   Tue, 16 Feb 2021 00:17:52 GMT
 2  Tue, 16 Feb 2021 00:17:52 GMT
  3 Tue, 16 Feb 2021 00:17:52 GMT
1   Tue, 16 Feb 2021 00:17:52 GMT
 2  Tue, 16 Feb 2021 00:17:52 GMT
  3 Tue, 16 Feb 2021 00:17:52 GMT
1   Tue, 16 Feb 2021 00:17:52 GMT
 2  Tue, 16 Feb 2021 00:17:52 GMT
  3 Tue, 16 Feb 2021 00:17:52 GMT

1   Tue, 16 Feb 2021 00:18:02 GMT
 2  Tue, 16 Feb 2021 00:18:02 GMT
  3 Tue, 16 Feb 2021 00:18:02 GMT
1   Tue, 16 Feb 2021 00:18:02 GMT
 2  Tue, 16 Feb 2021 00:18:02 GMT
  3 Tue, 16 Feb 2021 00:18:02 GMT
1   Tue, 16 Feb 2021 00:18:02 GMT
 2  Tue, 16 Feb 2021 00:18:02 GMT
  3 Tue, 16 Feb 2021 00:18:02 GMT

1   Tue, 16 Feb 2021 00:18:12 GMT
 2  Tue, 16 Feb 2021 00:18:12 GMT
  3 Tue, 16 Feb 2021 00:18:12 GMT
1   Tue, 16 Feb 2021 00:18:12 GMT
 2  Tue, 16 Feb 2021 00:18:12 GMT
  3 Tue, 16 Feb 2021 00:18:12 GMT
1   Tue, 16 Feb 2021 00:18:12 GMT
 2  Tue, 16 Feb 2021 00:18:12 GMT
  3 Tue, 16 Feb 2021 00:18:12 GMT
*/

I found a fix for this issue but maybe not the best: Simply check the amount of listeners

lib/client/proxy-interface.js

...
    this.on('removeListener', (eventName, listener) => {
      // FIX
      if (this.listeners(eventName).length > 0) {
        return;
      }

      const [signal, detailedEvent] = getEventDetails(eventName);

      if (!signal) {
        return;
      }

      if (this.$object.bus._connection.stream.writable) {
        this.$object.bus._removeMatch(this._signalMatchRuleString(eventName))
          .catch(error => {
            this.$object.bus.emit('error', error);
          });
      }
      this.$object.bus._signals.removeListener(detailedEvent, this._getEventListener(signal));
    });

    this.on('newListener', (eventName, listener) => {
      // FIX
      if (this.listeners(eventName).length > 0) {
        return;
      }

      const [signal, detailedEvent] = getEventDetails(eventName);

      if (!signal) {
        return;
      }

      this.$object.bus._addMatch(this._signalMatchRuleString(eventName))
        .catch(error => {
          this.$object.bus.emit('error', error);
        });
      this.$object.bus._signals.on(detailedEvent, this._getEventListener(signal));
    });
  }
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant