NodeGuy/channel

Possible heap overflow: Cancelled orders are not removed

LeonardGiglhuber opened this issue · 1 comments

Issue:
The following code generates a heap overflow:

import Channel from "@nodeguy/channel";

async function heapTest() {
    const closedDefault = new Channel<{}>();
    closedDefault.close();
    const randomOtherChannel = new Channel<{}>();

    while (true) {
        switch (await Channel.select([randomOtherChannel.push({}), closedDefault.shift()])) {
            case randomOtherChannel:
                // Will never happen
                throw new Error();
            default:
                // Will always happen
                break;
        }
    }
}

heapTest();

Why:
Every iteration of the while loop appends a new order object to the pushes array of randomOtherChannel. The order immediately get’s cancelled but is never removed from the array. It should normally be skipped inside matchPushesAndShifts() and then sliced off inside processOrders(). This never happens because the while loop condition inside matchPushesAndShifts() is never met (the shifts array of randomOtherChannel is always empty).

Solution: (Pull request submitted)
Filter out all cancelled orders from pushes and shifts at the end of every processOrders() call.

Fixed by #20. Thank you!