nats-io / nats.ts

TypeScript Node.js client for NATS, the cloud native messaging system
https://www.nats.io
Apache License 2.0
178 stars 13 forks source link

Maximum call stack size exceeded when use a recursive function inside subscribe #98

Closed cocodrino closed 4 years ago

cocodrino commented 4 years ago

Hi, I'm trying to transform some code from amqp to nats, I start with the basic Fibonacci request/reply

server

import {connect} from 'ts-nats';

function fibonacci(number) {
  // eslint-disable-next-line max-statements-per-line
  if (number === 0 || number === 1) { return number; } else { return fibonacci(number - 1) + fibonacci(number - 2); }
}

// ...
(async function() {
  try {
    const nc = await connect();
    console.info('server running.');
    await nc.subscribe("fibo", (err, msg) => {
      console.info("receiving message");
      if (err || !msg.reply) {
        // handle the error
      } else {
        // eslint-disable-next-line radix
        const reqNumber = parseInt(msg.reply);

        console.log(msg.data);
        const response = fibonacci(reqNumber);
        console.info(`response is ${response}`);
        nc.publish(msg.reply, response.toString());
      }
    }, {max: 100});
    // Do something with the connection
  } catch (ex) {
    // handle the error
  }
})();

client

import {connect} from 'ts-nats';

// ...
(async function() {
  try {
    const nc = await connect();

    console.info("msg 2");
    const msg = await nc.request('fibo', 0, "6");
    console.info(`msg: ${msg.data}`);

  } catch (ex) {
    // handle the error
  }
})();

the Fibonacci code is not tail recursive but I've tested this even with big numbers and works ok, I never get an error related to the call stack when use it...

when I run this code I get this

Exception in PromiseRejectCallback:
/Users/Admin/Downloads/proj/js/typescript/vertex_rpc/src/prueba.ts:4
    return new (P || (P = Promise))(function (resolve, reject) {
           ^

RangeError: Maximum call stack size exceeded
Exception in PromiseRejectCallback:
/Users/Admin/Downloads/proj/js/typescript/vertex_rpc/src/prueba.ts:7
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
                                                                                          ^

RangeError: Maximum call stack size exceeded
(node:39229) UnhandledPromiseRejectionWarning: RangeError: Maximum call stack size exceeded
    at /Users/Admin/Downloads/proj/js/typescript/vertex_rpc/src/prueba.ts:1:1
    at /Users/Admin/Downloads/proj/js/typescript/vertex_rpc/src/prueba.ts:8:37
    at new Promise (<anonymous>)
    at __awaiter (/Users/Admin/Downloads/proj/js/typescript/vertex_rpc/src/prueba.ts:4:12)
    at fibonacci (/Users/Admin/Downloads/proj/js/typescript/vertex_rpc/src/prueba.ts:14:12)
    at /Users/Admin/Downloads/proj/js/typescript/vertex_rpc/src/prueba.ts:5:76
    at Generator.next (<anonymous>)
    at /Users/Admin/Downloads/proj/js/typescript/vertex_rpc/src/prueba.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/Admin/Downloads/proj/js/typescript/vertex_rpc/src/prueba.ts:4:12)
(node:39229) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:39229) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

do you know what could be the issue here?...thank you so much

aricart commented 4 years ago

try implementing without recursion.