Skip to content

Memory leak when using concatMap with asynchronous function #2342

@janmarthedal

Description

@janmarthedal

RxJS version:

Version 5.1.0.

Code to reproduce:

const Rx = require('rxjs/Rx');

const buffer_size = 1024;
const dummy_string = Array(10001).join('x');

const generator = (function() {
    const subscribers = [];
    let counter = 0;

    const observable = Rx.Observable.create(observer => {
        subscribers.push(observer);
    });

    observable.request = count => {
        setTimeout(() => {
            for (let req=count; req > 0; req--) {
                counter++;
                subscribers.forEach(s => s.next(counter));
            }
        }, 0);
    };

    return observable;
})();

function mapper_async(recs) {
    return Rx.Observable.create(observer => {
        setTimeout(() => {
            observer.next(recs.length);
            observer.complete();
        }, 1);
    });
}

generator
    .map(v => dummy_string + v)
    .bufferCount(buffer_size)
    .concatMap(mapper_async)
    .subscribe(v => {
        console.log(v);
        generator.request(buffer_size);
    });

generator.request(buffer_size);

Expected behavior:

Memory usage stays bounded.

Actual behavior:

Out of memory (after some time).

Additional information:

If using

function mapper(recs) {
    return Rx.Observable.create(observer => {
        observer.next(recs.length);
        observer.complete();
    });
}

as the mapping function instead of mapper_async, the problem does not occur.

If running the equivalent code using https://github.com/Reactive-Extensions/RxJS (v4.1.0) the memory does not blow up.

Run on node.js version 6.9.4.

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