Skip to content

Instantly share code, notes, and snippets.

@dwrensha
Last active August 29, 2015 14:06
Show Gist options
  • Save dwrensha/83d4dbafba9502646fbf to your computer and use it in GitHub Desktop.
Save dwrensha/83d4dbafba9502646fbf to your computer and use it in GitHub Desktop.
detecting request cancellation in WebApp.rawConnectHandlers
/*
I start this in one terminal and in another I do `curl 127.0.0.1:3000` and immediately ctrl-C.
The output is:
$ meteor
[[[[[ ~/Desktop/raw_connect_handler_test ]]]]]
=> Started proxy.
=> Started MongoDB.
=> Started your app.
=> App running at: http://localhost:3000/
I20140909-21:25:26.887(-4)? handling a request
I20140909-21:25:26.930(-4)? local port: 24116
I20140909-21:27:26.832(-4)? CLOSE
Note here the "CLOSE" gets printed two minutes after I hit ctrl-C! It
seems that the 'close' event is not being emitted until the request times
out -- any EOF event is getting ignored somehow.
I expect instead for "CLOSE" to be printed as soon as I hit ctrl-C.
I also expect the local port to be 3000. I guess this means that Meteor is putting my
app behind some kind of proxy, and that proxy is failing to forward the 'close' event.
*/
if (Meteor.isServer) {
Meteor.startup(function () {
WebApp.rawConnectHandlers.use(function (req, res, next) {
console.log("handling a request");
console.log("local port: " + req.connection.localPort);
req.on('close', function() {
console.log("CLOSE");
});
});
});
}
/*
This example works as I expect.
I start this in one terminal and in another I do `curl 127.0.0.1:7000` and immediately ctrl-C.
The output is:
$ node server.js
handling a request
local port: 7000
CLOSE
where the "CLOSE" gets printed as soon as I hit ctrl-C.
*/
var connect = require("connect");
var http = require('http');
var app = connect();
app.use(function(req, res, next) {
console.log("handling a request");
console.log("local port: " + req.connection.localPort);
req.on("close", function() {
console.log("CLOSE");
});
});
var httpServer = http.createServer(app);
httpServer.listen(7000);
@dwrensha
Copy link
Author

dwrensha commented Sep 9, 2014

My question: how can I detect cancelled requests when using WebApp.rawConnectHandlers? It appears to me that the 'close' event does not get fired on EOF.

@dwrensha
Copy link
Author

The root problem is a bug with older versions of http-proxy. Meteor uses version 1.0.2, but cancellation of requests is not handled until version 1.1.2:

http-party/node-http-proxy@77a1cff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment