-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not working on http.ServerResponse closing since Node 0.6.7 #9
Comments
A workaround is to monkey patch Node: var ServerResponse = require('http').ServerResponse;
ServerResponse.prototype.assignSocket = function(socket) {
var that = socket._httpMessage = this;
this.onServerResponseClose = function onServerResponseClose() {
that.emit('close');
};
socket.on('close', this.onServerResponseClose);
this.socket = socket;
this.connection = socket;
this._flush();
};
ServerResponse.prototype.detachSocket = function(socket) {
socket.removeListener('close', this.onServerResponseClose);
socket._httpMessage = null;
this.socket = this.connection = null;
}; |
I manage the trycatch module (async try/catch), and I use Tom's long-stack-traces technique. I came across this issue as well, but it wasn't an option to turn off in production b/c we use trycatch for stability. It turns out the issue is related to how long-stack-traces' (LST) shims functions,
Unfortunately this breaks That's why this patch works, b/c you're effectively preventing the _httpMessage from being GC'd, but the problem is you're emitting a https://github.com/CrabDude/trycatch/blob/master/lib/hook.js#L40-60 // Wrap EventEmitters
var EventEmitter = require('events').EventEmitter
var onEvent = EventEmitter.prototype.on
EventEmitter.prototype.on = EventEmitter.prototype.addListener = function (type, callback) {
var newCallback
callback._wrappedCallback = callback._wrappedCallback || []
newCallback = wrap(callback, 'EventEmitter.on')
if (newCallback !== callback) {
callback._wrappedCallback.push(newCallback)
}
arguments[1] = newCallback
return onEvent.apply(this, arguments)
}
var removeEvent = EventEmitter.prototype.removeListener
EventEmitter.prototype.removeListener = function (type, callback) {
if (callback && Array.isArray(callback._wrappedCallback)) {
arguments[1] = callback._wrappedCallback.pop()
}
return removeEvent.apply(this, arguments)
} Basically, what I'm doing is still wrapping each callback, but I store the array on the original function so when it comes time to remove I can pop them off the array. This fixes the issue in http.js::onServerResponseClose. |
Due to a change in Node http module, there is a loss of scope in a wrapped callback, resulting in an error.
The text was updated successfully, but these errors were encountered: