neonious / lowjs

A port of Node.JS with far lower system requirements. Community version for POSIX systems such as Linux, uClinux or Mac OS X.
http://www.lowjs.org/
Other
1.27k stars 72 forks source link

Fix order of nextTick processing to match nodejs #163

Open ChristophKaser opened 1 year ago

ChristophKaser commented 1 year ago

Nodejs processes callbacks added by process.nextTick in a queue (=> LIFO order). Lowjs uses a stack (=>FIFO order). This causes issues, e.g in readable-streams, where I had 'readable' events after the 'end' event, which in turn caused a deadlock in code unprepared for this event order.

Demo code to check lowjs and nodejs behavior:

function a () {
    console.log('a');
    process.nextTick(b);
    process.nextTick(b);
}

function b () {
    console.log('b');
    process.nextTick(c, '1');
    process.nextTick(c, '2');
}

function c (arg) {
    console.log('c', arg);
}

a();

This PR fixes the order used by lowjs by inserting new callbacks at the bottom of the stack. While this is suboptimal for a large number of callbacks, in real world code the number of callbacks usually is not that large, so the O(n²) behaviour of the insertion method should not matter.