sqlanywhere / node-sqlanywhere

SAP SQL Anywhere Database Client for Node
Apache License 2.0
39 stars 36 forks source link

sqlanywhere connection via express locks node process #8

Closed Albertik closed 8 years ago

Albertik commented 8 years ago

I have this simple express app:

var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var request = require('request');
var sqlanywhere = require('sqlanywhere');
var connParams = "uid=uid;pwd=pwd;eng=eng;dbn=dbn;links=tcpip{host=host};con=con";
app.use('/api/sqlany', function (req, res) {
    var conn = sqlanywhere.createConnection();
    conn.connect(connParams);
    console.log('Connected');
    conn.disconnect();
    console.log('Disconnect');
    res.send('ok');
});

request('http://localhost:3000/api/sqlany', function (err) {
    if (err) throw err;
});

server.listen(3000);

After connection to database it logs 'Connected' and 'Disconnected' in console and after that node process locks, I can't terminate it with Ctrl+C. Node version v5.0.0, tried with sql anywhere 11 and 16

gperrow-SAP commented 8 years ago

I'm not familiar with express but I installed it on my Windows machine. I tried this test and it worked fine. After connect/disconnect, Ctrl-C stopped the app. What platform are you running on?

Do you see the same problem when the SQL Anywhere part is taken out of the app?

Albertik commented 8 years ago

I run on the OS X El Capitan Version 10.11.2, I changed it that not to use express, same result:

var http = require('http');
var connParams = "uid=uid;pwd=pwd;eng=eng;dbn=dbn;links=tcpip{host=host};con=con";
var server = http.createServer(function (req, res) {
    var conn = sqlanywhere.createConnection();
    conn.connect(connParams);
    console.log('Connected');
    conn.disconnect();
    console.log('Disconnect');
    res.end('ok');
});
var request = require('request');
var sqlanywhere = require('sqlanywhere');

request('http://localhost:3000/', function (err) {
    if (err) throw err;
});

server.listen(3000);

When taken out of request handling it works just fine

alevinru commented 8 years ago

@gperrow-SAP Just have tried it on a freshly set up Ubuntu 14.04.2 LTS with:

then:

ubuntu@ip-172-31-15-54:/var/www/sa$ npm install request express sqlanywhere
ubuntu@ip-172-31-15-54:/var/www/sa$ vi test.js
ubuntu@ip-172-31-15-54:/var/www/sa$ node test.js 
Error: Code: -100 Msg: Database server not found
    at Error (native)
    at /var/www/sa/test.js:10:10
    at Layer.handle [as handle_request] (/var/www/sa/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/var/www/sa/node_modules/express/lib/router/index.js:312:13)
    at /var/www/sa/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/var/www/sa/node_modules/express/lib/router/index.js:330:12)
    at next (/var/www/sa/node_modules/express/lib/router/index.js:271:10)
    at expressInit (/var/www/sa/node_modules/express/lib/middleware/init.js:33:5)
    at Layer.handle [as handle_request] (/var/www/sa/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/var/www/sa/node_modules/express/lib/router/index.js:312:13)
^C^C^C^C^C^C^C^C^C^CAborted (core dumped)
ubuntu@ip-172-31-15-54:/var/www/sa$

I used connParams = "uid=uid;pwd=pwd;eng=eng;dbn=dbn;" and it's ok that "Database server not found". When i set up correct connParams it's the same thing - i can stop the process only doing kill -HUP from a separate console connection. Seems like conn.connect locks something that http/express waits to be released.

gperrow-SAP commented 8 years ago

Thanks @alevinru. I have reproduced the problem on both Linux and Mac and I am investigating what's causing it and how we can fix it. It has something to do with our signal handling which is why it works fine on Windows.

gperrow-SAP commented 8 years ago

I tried this with node.js 0.10.29 and it worked fine, but it does not work in node.js v4 or v5, so something to do with signal handling has changed in that time. Someone reported a very similar bug in node.js: https://github.com/nodejs/node/issues/4182

There is a workaround in that bug description. Adding the following code to the test.js file solved the problem for me:

process.on('SIGINT', function() {
    process.exit();
});
alevinru commented 8 years ago

@gperrow-SAP many thanks