{"action":"created","issue":{"url":"https://api.github.com/repos/othiym23/node-continuation-local-storage/issues/71","repository_url":"https://api.github.com/repos/othiym23/node-continuation-local-storage","labels_url":"https://api.github.com/repos/othiym23/node-continuation-local-storage/issues/71/labels{/name}","comments_url":"https://api.github.com/repos/othiym23/node-continuation-local-storage/issues/71/comments","events_url":"https://api.github.com/repos/othiym23/node-continuation-local-storage/issues/71/events","html_url":"https://github.com/othiym23/node-continuation-local-storage/issues/71","id":164385076,"number":71,"title":"CLS breaks when reuse sockets for HTTP fetch","user":{"login":"jchip","id":5876741,"avatar_url":"https://avatars.githubusercontent.com/u/5876741?v=3","gravatar_id":"","url":"https://api.github.com/users/jchip","html_url":"https://github.com/jchip","followers_url":"https://api.github.com/users/jchip/followers","following_url":"https://api.github.com/users/jchip/following{/other_user}","gists_url":"https://api.github.com/users/jchip/gists{/gist_id}","starred_url":"https://api.github.com/users/jchip/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jchip/subscriptions","organizations_url":"https://api.github.com/users/jchip/orgs","repos_url":"https://api.github.com/users/jchip/repos","events_url":"https://api.github.com/users/jchip/events{/privacy}","received_events_url":"https://api.github.com/users/jchip/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2016-07-07T19:10:32Z","updated_at":"2016-07-13T09:05:52Z","closed_at":null,"body":"I have a HTTP fetch that I enable keep alive agent.\r\n\r\nsomething like:\r\n```js\r\nvar httpAgent = new require(\"http\").Agent( { keepAlive: true, maxSockets: 1 });\r\nnamespace.run( function () {\r\n  var xid = Math.random() * 1000;\r\n  namespace.set( \"xid\", xid );\r\n  request( \"http://www.google.com\", { agent: httpAgent }, function (err) {\r\n    assert.equal(namespace.get(\"xid\"), xid);\r\n  });\r\n});\r\n```\r\n\r\nDoing that the first time works and CLS context was intact, but the second time when the socket is reused, the CLS context gets the value from the context in the first fetch.\r\n\r\nhttps://gist.github.com/jchip/73c44046408d0e60b96abc3fc0303eb2\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/othiym23/node-continuation-local-storage/issues/comments/232299458","html_url":"https://github.com/othiym23/node-continuation-local-storage/issues/71#issuecomment-232299458","issue_url":"https://api.github.com/repos/othiym23/node-continuation-local-storage/issues/71","id":232299458,"user":{"login":"jchip","id":5876741,"avatar_url":"https://avatars.githubusercontent.com/u/5876741?v=3","gravatar_id":"","url":"https://api.github.com/users/jchip","html_url":"https://github.com/jchip","followers_url":"https://api.github.com/users/jchip/followers","following_url":"https://api.github.com/users/jchip/following{/other_user}","gists_url":"https://api.github.com/users/jchip/gists{/gist_id}","starred_url":"https://api.github.com/users/jchip/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jchip/subscriptions","organizations_url":"https://api.github.com/users/jchip/orgs","repos_url":"https://api.github.com/users/jchip/repos","events_url":"https://api.github.com/users/jchip/events{/privacy}","received_events_url":"https://api.github.com/users/jchip/received_events","type":"User","site_admin":false},"created_at":"2016-07-13T09:05:52Z","updated_at":"2016-07-13T09:05:52Z","body":"This seems to do the trick:\r\n```js\r\nwrap(net.Socket.prototype, 'unref', function (original) {\r\n  return function() {\r\n    if (this._saveOnRead) {\r\n      this._handle.onread = this._saveOnRead;\r\n      delete this._saveOnRead;\r\n    }\r\n    original.apply(this);\r\n  }\r\n});\r\n\r\n\r\nwrap(net.Socket.prototype, 'ref', function (original) {\r\n  return function() {\r\n    if (this._handle) {\r\n      this._saveOnRead = this._handle.onread;\r\n      this._handle.onread = wrapCallback(this._handle.onread);\r\n    }\r\n    original.apply(this);\r\n  }\r\n});\r\n\r\nwrap(net.Socket.prototype, 'connect', function (original) {\r\n  return function () {\r\n    var args = net._normalizeConnectArgs(arguments);\r\n    if (args[1]) args[1] = wrapCallback(args[1]);\r\n    var result = original.apply(this, args);\r\n    if (this._handle) {\r\n      this._saveOnRead = this._handle.onread;\r\n      this._handle.onread = wrapCallback(this._handle.onread);\r\n    }\r\n    return result;\r\n  };\r\n});\r\n```"}}
{"actor":{"display_login":"jchip"}}