node-esl icon indicating copy to clipboard operation
node-esl copied to clipboard

execute() does not detect socket disconnection

Open ssinyagin opened this issue 6 years ago • 4 comments

I'm running synchronous outbound ESL socket toward the node-esl server. If the server starts an conn.execute(xxxx) command, and the socket disconnects during the execution, the execute() never returns and hangs forever.

by looking through the sources of Connection.js, I couldn't find any handling for such event.

Here's my code:

                    function run_phrases() {
                        if( phrases.length > 0 && input === null ) {
                            let file = phrases.shift() + ext;
                            console.log(conn.cs.uuid + ' Playing ' + file);
                            try {
                                conn.execute('play_and_get_digits', '1 1 1 10 # ' + file + ' "" dialed_digits',
                                             function(ev) {
                                                 input = ev.getHeader('variable_dialed_digits');
                                                 if( input !== null ) {
                                                     console.log(conn.cs.uuid + ' User pressed ' + input);
                                                 }
                                                 run_phrases();
                                             });
                            }
                            catch (err) {
                                if( err.code != 'ERR_STREAM_DESTROYED' ) {
                                    console.error(err);
                                }
                                else {
                                    console.log(conn.cs.uuid + ' disconnected');
                                }
                                reject();
                            }
                        }
                        else {
                            resolve();
                        }
                    }
                    
                    run_phrases();

ssinyagin avatar Dec 19 '19 00:12 ssinyagin

net.Socket is not sending "end" event on error. It sends "close" in both error and non-error cases, and that's when you need to emit esl::end

I'll send a PR

ssinyagin avatar Jan 06 '20 21:01 ssinyagin

also a branch to fix it in 1.2.1: https://github.com/voxserv/node-esl/tree/v1.2.1-bugfix

ssinyagin avatar Jan 06 '20 22:01 ssinyagin

Can you open a PR for that branch to the v1.x branch?

englercj avatar Jan 07 '20 02:01 englercj

Until this is merged you can set up the callback on your own code.

    connection.socket.on('close', (hadError: boolean) => {
      if (hadError) logger.warn('esl socket closed with error')
      else logger.debug('esl socket closed')
      endCall()
    })

pablito25sp avatar Jul 12 '22 14:07 pablito25sp