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

How to deactivate listener? #23

Closed
7h0mas-R opened this issue Mar 16, 2021 · 3 comments
Closed

How to deactivate listener? #23

7h0mas-R opened this issue Mar 16, 2021 · 3 comments

Comments

@7h0mas-R
Copy link

7h0mas-R commented Mar 16, 2021

Hi,
thanks for this little project. I use it to drive multiple rotary encoders and it is really working very nicely.
I noticed one thing:
When a listner is registered with on (like in the example you provide), I would like to also unregister it again. Normally, I often use off for that purpose, but that seems not to work. So I used removeAllListeners() instead.
But I noticed, that the program does still not finish, which seems to indicate, that it still waits for input. Even if I call close on the objects and even set them to null, it does not stop executing until I press Ctrl+C.

I basically add this code to the end of the example you provide:

const InputEvent = require('../');
var input = new InputEvent('/dev/input/event0');
var rotary = new InputEvent.Rotary(input);
rotary.on('left'  , ev => console.log('left',  ev));
rotary.on('right' , ev => console.log('right', ev));

setTimeout(()=>{
  console.log('Removing input ')
  rotary.removeAllListeners();
  rotary.close();
  rotary = null;
  input.close(); 
  input=null;               
},2000)
@7h0mas-R
Copy link
Author

checking with .listenerCount shows that the listeners are gone... so probably no big deal

@7h0mas-R 7h0mas-R reopened this Mar 26, 2021
@7h0mas-R
Copy link
Author

Hi,

I reopen this, since I still have issues. What I am trying to do, is have a program that allows the user to configure an DToverlay via a GUI that is then installed and the program uses input-event to register events to read from the DTOverlay.
The user can also dynamically change the settings, which eventually removes the events and deletes the overlay, replacing it with something new.
The error I observe is a Kernel warning about a potential memory leak:
ERROR: memory leak, expected refcount 1 instead of 2, of_node_get()/of_node_put() unbalanced

It looks, like there is still a reference to the particular device and thus dtoverlay generates the error.
I have made a very simple script trying to track down the error. It seems, as if the fs.readStream is not released and also not garbage collected.
I use the npm why-is-node-running package to check for unreleased handles and it finds FSREQWRAP for each input or rotary object I create.
Here is the test-script:

const log = require('why-is-node-running');
const InputEvent = require('input-event');
var input = new InputEvent('/dev/input/event0');
setTimeout(()=>{
  console.log('Removing input ')
  input.close(()=>{
    input=null;
  });
},2000);

setTimeout(()=>{
  log();
},10000)

This gives me the following output (the timer handler is from the 10s timer calling log()):

Removing input 
There are 2 handle(s) keeping the process running

# Timeout
/home/volumio/input-event_test.js:20 - setTimeout(()=>{

# FSREQWRAP
(unknown stack trace)
^C

Any ideas?

@7h0mas-R
Copy link
Author

7h0mas-R commented Mar 27, 2021

I dug a bit deeper: This is the line in lib/index.js causing the creation of the handle:

this.input.on('data', data => {...});

So it seems to be the listener attached to the fs.ReadStream object.
I also posted my finding here:

nodejs/help#3289

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

No branches or pull requests

1 participant