Skip to content

无法请求同一进程的http server #4

Closed
fengmk2 opened this Issue Jul 24, 2012 · 2 comments

2 participants

@fengmk2
Owner
fengmk2 commented Jul 24, 2012

测试代码:

var httpsync = require('../');
var http = require('http');

var app = http.createServer(function (req, res) {
  console.log(req.url, req.headers);
  req.on('end', function () {
    res.end('hello world');
  });
});

app.listen(0, function () {
  var url = 'http://127.0.0.1:' + app.address().port;
  console.log('start to request: ' + url);
  var req = httpsync.get(url);
  var res = req.end();
  console.log(res);
  process.exit(0);
});

进程卡住了。

@zcbenz zcbenz was assigned Jul 24, 2012
@fengmk2 fengmk2 added a commit that referenced this issue Jul 24, 2012
@fengmk2 add test case for #4 fa3e283
@zcbenz
Collaborator
zcbenz commented Jul 25, 2012

httpsync.end()执行时整个线程都会block住直到函数结束,主线程block时将无法进入事件循环,也就无法响应其它请求,于是httpsync.end()就会一直等下去。也就是说httpsync.end()等待进程进入主循环来响应请求,但同时进程又在等待httpsync.end()结束以便进入主循环,有些类似于dead lock。

对于同步阻塞的函数调用来说,这种行为是正确而且可以预料的。

@zcbenz zcbenz closed this Jul 25, 2012
@fengmk2
Owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.