node-midi icon indicating copy to clipboard operation
node-midi copied to clipboard

Creating midi.input() instances can cause ALSA to run out of resources

Open simonbates opened this issue 9 years ago • 0 comments

If I create enough midi.input() instances, I eventually run out of ALSA resources on Linux and my program terminates.

For example, running the following program:

var midi = require("midi");

for (var i = 0; i < 100; i++) {
    new midi.input();
}

process.exit();

Results in:

ALSA lib seq_hw.c:457:(snd_seq_hw_open) open /dev/snd/seq failed: Cannot allocate memory

MidiInAlsa::initialize: error creating ALSA sequencer client object.

terminate called after throwing an instance of 'RtMidiError'
  what():  MidiInAlsa::initialize: error creating ALSA sequencer client object.
Aborted

I think what is happening is that while the ~NodeMidiInput() destructor deletes the RtMidiIn instance that it creates, I believe that there is no guarantee of when the destructor will get called by the garbage collector.

To test this theory, I've created a fork of node-midi that adds a function to explicitly delete the RtMidiIn instance:

https://github.com/justinlatimer/node-midi/compare/master...simonbates:release-rtmidi

With this version, I'm able to create lots of midi.input() instances as long as I explicitly delete the associated RtMidiIn object when it's no longer needed:

var midi = require("midi");

for (var i = 0; i < 10000; i++) {
    var midiInput = new midi.input();
    midiInput.release();
}

process.exit();

simonbates avatar Nov 14 '16 20:11 simonbates