rwlock icon indicating copy to clipboard operation
rwlock copied to clipboard

Possible memory leak

Open kieransimkin opened this issue 9 years ago • 3 comments

Looks like a memory leak? It's only happened once so I'm not 100% sure it's rwlock that's actually leaking, but it would make sense that it is - I'm creating lots of one-time use locks with unique identifiers

<--- Last few GCs --->

127398132 ms: Scavenge 1534.7 (1587.0) -> 1534.7 (1587.0) MB, 0.2 / 0 ms (+ 2.5 ms in 1 steps since last GC) [allocation failure] [incremental marking delaying mark-sweep]. 127401398 ms: Mark-sweep 1534.7 (1587.0) -> 1352.4 (1587.0) MB, 3266.6 / 0 ms (+ 3.4 ms in 2 steps since start of marking, biggest step 2.5 ms) [last resort gc]. 127404780 ms: Mark-sweep 1352.4 (1587.0) -> 1527.7 (1582.0) MB, 3381.7 / 0 ms [last resort gc].

<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0x9088c1b4629 <JS Object> 1: writeLock(aka c) [/home/kieran/btc/broadcaster/node_modules/rwlock/lib/lock.js:~5] [pc=0x11e6eff43898](this=0x197eb8f2c61 <JS Object>,b=0x111e36fb7e21 <String[10]: 25-2141721>,c=0x111e36fb7dd9 <JS Function %28SharedFunctionInfo 0x13c646a98ed9%29>,f=0x9088c1041b9 <undefined) 2: arguments adaptor frame: 2->3 3: /* anonymous */ [/home/kieran/btc/broadcaster/index.js:285] [pc=0x11e6ee63...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory Aborted (core dumped)

kieransimkin avatar Dec 27 '15 22:12 kieransimkin

Here's an example of one of the functions that's using rwlock (it's a socket.io event hander)

socket.on('openOrder', function (data) {
     orderbook_lock.writeLock(data.exchange_id+'-'+data.remote_id, function(release) {
             io.to('orders_'+data.exchange_id).emit('open_order', data);
              pool.getConnection(function(err,connection){
                if (err) {
                  connection.release();
                  release();
                  console.log('failed to get ocnnection'+err.stack);
                  return;
                }

                connection.query("insert into orders set exchange_id=?, opentime=UNIX_TIMESTAMP(), type=?, quantity=?, price=?, remote_id=?",[data.exchange_id,data.type,data.quantity,data.price,data.remote_id], function(err,rows){
                    connection.release();
                    release();
                    if(err) {
                        console.log(err);
                    }
                });

              });
     });

  });

kieransimkin avatar Dec 27 '15 22:12 kieransimkin

I believe the fix would be to remove the entry table[key] when all locks have been released on that key?

kieransimkin avatar Dec 29 '15 04:12 kieransimkin

This has the potential to cause allot of GC, maybe a max number of locks could be defined, at this point you could go and free all locks by taking the writelock and then destroying the lock.

An option to release in writelock that kills table[key] when release is called rather than releasing would help.

You would have to keep your own list of keys to track the number of locks you have made and take care of this in your program. Other than providing a way to destroy a lock everything is out of scope for rwlock.

gordon-to avatar Aug 10 '16 11:08 gordon-to