Skip to content

removeListeners doesn't work as expected for newly created orders, causing callback leak #25

Description

@ngutman

Summary

There's an issue causing removeListeners to not remove all the order callbacks that were registered in registerListeners causing a leak, as new orders accumulate this greatly affects performance eventually killing he process

Issue Breakdown

  • Upon initial creation of an order with cid the cbGID will be undefined.{cid} since gid was never provided
  • Calling registerListeners will register the mentioned cbGID which later is used with removeListeners
  • Submitting the order will invoke an update message updating the gid to null (we listen to update to verify the order was successfully submitted
  • cbGID will now be null.{cid} mismatching the initial cbGID, rendering removeListeners useless which will increase callbacks size infinitely - this specifically affects systems automatically creating orders.

Sample code

(I took the sample code from https://github.com/bitfinexcom/bfx-api-node-models/#order marking where the issue is prominent)

const { Order } = require('bfx-api-node-models')
const ws = ... // setup WSv2 instance for order updates/submission

// Build new order
const o = new Order({
  cid: Date.now(),
  symbol: 'tBTCUSD',
  price: 7000.0,
  amount: -0.02,
  type: Order.type.EXCHANGE_LIMIT
}, ws) // note WSv2 client passed in here

let closed = false

// Enable automatic updates
o.registerListeners()

o.on('update', () => {
  debug('order updated: %j', o.serialize())
  o.removeListeners() // ISSUE - `cbGID` is not the same as before the `update`
})

o.on('close', () => {
  debug('order closed: %s', o.status)
  closed = true
  o.removeListeners() // Probably also problematic
})

debug('submitting order %d', o.cid)

o.submit().then(() => {
  debug('got submit confirmation for order %d [%d]', o.cid, o.id)
}).catch((err) => {
  debug('failed to submit order: %s', err.message)
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions