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

Resource leak when calling shutdown before finish writing #126

Open
rluvaton opened this issue Feb 16, 2023 · 1 comment
Open

Resource leak when calling shutdown before finish writing #126

rluvaton opened this issue Feb 16, 2023 · 1 comment
Labels
bounty:$50 Bounty applies for fixing this issue (Parse Bounty Program) type:bug

Comments

@rluvaton
Copy link

This code causes resource leak and don't close the TLS socket

const apn = require('../');

let i = 0;

const pushToken = ''

function send(provider, token) {

  const note = new apn.Notification();
  note.alert = `Hey hello, I just sent my first Push Notification`;

  // The topic is usually the bundle identifier of your application.
  note.topic = '<my-topic>';

  console.log(`Sending: ${note.compile()} to`);

  return provider.send(note, [token]).then(result => {
    console.log('sent:', result.sent.length);
    console.log('failed:', result.failed.length);
    console.log(result.failed);
  });
}

async function run() {
  const users = [
    { name: 'Wendy', devices: [pushToken] },
  ];
  const service = new apn.Provider({
    // ...
  });

  Promise.all(
    users.map(user => {
      try {
        return send(service, pushToken);
      } finally {
        return service.shutdown();
      }
    }),
  )
    .finally(() => {
      printActiveHandles();

      if (i++ < 10) {
        run();
      } else {
        setInterval(() => {
          printActiveHandles()
        }, 1000)
      }
    });
}


function printActiveHandles() {
  const data = process._getActiveHandles();

  const formatted = data.map(item => item.constructor.name);

  console.log(`Got ${formatted.length} active handles`, formatted);
}

run();

this will log in the end:

Got 13 active handles [
  'WriteStream', 'WriteStream',
  'TLSSocket',   'TLSSocket',
  'TLSSocket',   'TLSSocket',
  'TLSSocket',   'TLSSocket',
  'TLSSocket',   'TLSSocket',
  'TLSSocket',   'TLSSocket',
  'TLSSocket'
]
@parse-github-assistant
Copy link

Thanks for opening this issue!

  • ❌ Please edit your post and use the provided template when creating a new issue. This helps everyone to understand your post better and asks for essential information to quicker review the issue.

@mtrezza mtrezza added type:bug bounty:$50 Bounty applies for fixing this issue (Parse Bounty Program) labels Jul 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bounty:$50 Bounty applies for fixing this issue (Parse Bounty Program) type:bug
Projects
None yet
Development

No branches or pull requests

2 participants