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

node process blocks if you use fiber in proces.once('exit', ...) callback #453

Closed
mitar opened this issue Sep 7, 2021 · 0 comments
Closed

Comments

@mitar
Copy link

mitar commented Sep 7, 2021

I know the project is not maintained anymore, but I just want to document this for anyone else who might spend time debugging this. I use fibers through Meteor and in Meteor I had the following code:

const connectionIds = new Set();

Meteor.onConnection((connection) => {
  connectionIds.add(connection.id);

  connection.onClose(() => {
    SomeDocuments.remove({
      connectionId: connection.id,
    });
    connectionIds.delete(connection.id);
  });
});

const connectionsCleanup = Meteor.bindEnvironment(() => {
  for (const connectionId of connectionIds) {
    SomeDocuments.remove({
      connectionId,
    });
    connectionIds.delete(connectionId);
  }
});

process.once('exit', connectionsCleanup);
process.once('SIGTERM', connectionsCleanup);
process.once('SIGINT', connectionsCleanup);

SomeDocuments.remove does some database requests (inside a fiber).

With Meteor 1.11.1 (which uses node v12.18.4) this makes the whole process block and never exit. It is documented that:

There is no way to prevent the exiting of the event loop at this point, and once all 'exit' listeners have finished running the Node.js process will terminate.

And:

Listener functions must only perform synchronous operations. The Node.js process will exit immediately after calling the 'exit' event listeners causing any additional work still queued in the event loop to be abandoned.

It seems running fibers in there messes up with things. So do not do it.

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