node-mole-rpc icon indicating copy to clipboard operation
node-mole-rpc copied to clipboard

Cancel timer when server responds

Open yoursunny opened this issue 5 years ago • 8 comments

The code has a bug: the program below does not terminate right away after all requests have been processed, but has to wait for the 300-second timeout. This patch fixes the bug by canceling the timer when server responds.

const MoleClient = require('../MoleClient');
const MoleServer = require('../MoleServer');

const TransportClient = require('./TransportClient');
const TransportServer = require('./TransportServer');

const EventEmitter = require('events');

(async () => {
    const emitter = new EventEmitter();
    const server = new MoleServer({
      transports: [new TransportServer({ emitter, inTopic: 'c', outTopic: 's' })],
    });
    server.expose({ f: () => 1 });
    await server.run();

    const client = new MoleClient({
      requestTimeout: 300000,
      transport: new TransportClient({ emitter, inTopic: 's', outTopic: 'c' }),
    });
    await client.callMethod('f', []);
    await client.runBatch([
      ['f', [1]],
    ]);
    console.log('program should exit now');
})();

yoursunny avatar Apr 16 '20 20:04 yoursunny

@yoursunny, thank you for the pull request! Will release the update during the week.

koorchik avatar Jun 07 '20 12:06 koorchik

I am not sure. That it should work as you have described. If we will stop the server when there is no active request then it should stop right after line await server.run(); which does not make a lot of sense. Moreover, if I run a server, I do not want it to stop if it is waiting for external calls. Now server behave the same with any "requestTimeout" values. It just waits for incoming calls.

koorchik avatar Jun 12 '20 14:06 koorchik

The change is on the client. No change has been made to the server. When the client receives a response from the server, it should cancel the timer and allow itself to exit.

yoursunny avatar Jun 12 '20 14:06 yoursunny

Oh, clear. Then it should be separate processes for client and server. I will recheck. Thanks

koorchik avatar Jun 12 '20 14:06 koorchik

For this test case, the server cannot prevent the process from exiting, because it uses an EventEmitter that does not have a socket.

Your socket based servers, such as the WebSocket transport, lack a close() method. That’s why you have to use process.exit() in test suites. But that’s a different topic.

yoursunny avatar Jun 12 '20 14:06 yoursunny

Yes, you are right. No IO in event emitter. Will try your example once more)

koorchik avatar Jun 12 '20 14:06 koorchik

Hey @koorchik can you review and merge?

yoursunny avatar Jul 18 '20 01:07 yoursunny

After a long time without action from the repository owner, I have decided to publish my patches in a fork: @yoursunny/mole-rpc. If anyone else is affected from this bug, you can try my fork.

yoursunny avatar Sep 06 '20 14:09 yoursunny