{"action":"created","issue":{"url":"https://api.github.com/repos/nodejs/node/issues/2986","labels_url":"https://api.github.com/repos/nodejs/node/issues/2986/labels{/name}","comments_url":"https://api.github.com/repos/nodejs/node/issues/2986/comments","events_url":"https://api.github.com/repos/nodejs/node/issues/2986/events","html_url":"https://github.com/nodejs/node/issues/2986","id":107557588,"number":2986,"title":"AsyncWrap: TCPWrap created before MakeCallback","user":{"login":"AndreasMadsen","id":505333,"avatar_url":"https://avatars.githubusercontent.com/u/505333?v=3","gravatar_id":"","url":"https://api.github.com/users/AndreasMadsen","html_url":"https://github.com/AndreasMadsen","followers_url":"https://api.github.com/users/AndreasMadsen/followers","following_url":"https://api.github.com/users/AndreasMadsen/following{/other_user}","gists_url":"https://api.github.com/users/AndreasMadsen/gists{/gist_id}","starred_url":"https://api.github.com/users/AndreasMadsen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AndreasMadsen/subscriptions","organizations_url":"https://api.github.com/users/AndreasMadsen/orgs","repos_url":"https://api.github.com/users/AndreasMadsen/repos","events_url":"https://api.github.com/users/AndreasMadsen/events{/privacy}","received_events_url":"https://api.github.com/users/AndreasMadsen/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/nodejs/node/labels/c++","name":"c++","color":"e11d21"}],"state":"open","locked":false,"assignee":{"login":"trevnorris","id":289174,"avatar_url":"https://avatars.githubusercontent.com/u/289174?v=3","gravatar_id":"","url":"https://api.github.com/users/trevnorris","html_url":"https://github.com/trevnorris","followers_url":"https://api.github.com/users/trevnorris/followers","following_url":"https://api.github.com/users/trevnorris/following{/other_user}","gists_url":"https://api.github.com/users/trevnorris/gists{/gist_id}","starred_url":"https://api.github.com/users/trevnorris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trevnorris/subscriptions","organizations_url":"https://api.github.com/users/trevnorris/orgs","repos_url":"https://api.github.com/users/trevnorris/repos","events_url":"https://api.github.com/users/trevnorris/events{/privacy}","received_events_url":"https://api.github.com/users/trevnorris/received_events","type":"User","site_admin":false},"milestone":null,"comments":4,"created_at":"2015-09-21T17:15:30Z","updated_at":"2015-09-21T18:59:09Z","closed_at":null,"body":"When creating a TCP server, the TCP handles there holds the socket is created in C++ land. The socket handle is then send to the `onconnection` server handle event using MakeCallback.\r\n\r\ncode link: https://github.com/nodejs/node/blob/master/src/tcp_wrap.cc#L259L272\r\n\r\nBecause the socket handle is created before `MakeCallback`, the `async_hooks_init_function` for the socket handle is called before `async_hooks_before_function` for the server handle. This makes it impossible to trace back the origin of the socket handle.\r\n\r\ndprof dump: http://bl.ocks.org/AndreasMadsen/raw/da3adb776bdfcf30f97f/ - Notice the free hanging blue line, which has an origin from nowhere. (The visualization/test was done with the following code).\r\n\r\n```javascript\r\n'use strict';\r\nrequire('../../dprof.js');\r\n\r\nconst net = require('net');\r\n\r\nconst server = net.createServer(function (socket) {\r\n  socket.end('hallo world');\r\n});\r\n\r\nserver.listen(0, 'localhost', function () {\r\n  const addr = server.address();\r\n  const socket = net.connect(addr.port, addr.host, function () {\r\n    socket.once('readable', function () {\r\n      socket.read();\r\n      socket.once('readable', server.close.bind(server));\r\n    });\r\n  });\r\n});\r\n```\r\n\r\nThis issue appears to be caused by https://github.com/nodejs/node/commit/7dde95a8bd. I agree that is is wired to call `async_hooks_before_function` in the `AsyncWrap` constructor, particularly in this case because it suggests that the creation of the `TCPWrap` object and `onconnection` happens in two different async turns.\r\n\r\nHowever I'm not sure what the proper fix would be.\r\n\r\n/cc @trevnorris "},"comment":{"url":"https://api.github.com/repos/nodejs/node/issues/comments/142077203","html_url":"https://github.com/nodejs/node/issues/2986#issuecomment-142077203","issue_url":"https://api.github.com/repos/nodejs/node/issues/2986","id":142077203,"user":{"login":"AndreasMadsen","id":505333,"avatar_url":"https://avatars.githubusercontent.com/u/505333?v=3","gravatar_id":"","url":"https://api.github.com/users/AndreasMadsen","html_url":"https://github.com/AndreasMadsen","followers_url":"https://api.github.com/users/AndreasMadsen/followers","following_url":"https://api.github.com/users/AndreasMadsen/following{/other_user}","gists_url":"https://api.github.com/users/AndreasMadsen/gists{/gist_id}","starred_url":"https://api.github.com/users/AndreasMadsen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AndreasMadsen/subscriptions","organizations_url":"https://api.github.com/users/AndreasMadsen/orgs","repos_url":"https://api.github.com/users/AndreasMadsen/repos","events_url":"https://api.github.com/users/AndreasMadsen/events{/privacy}","received_events_url":"https://api.github.com/users/AndreasMadsen/received_events","type":"User","site_admin":false},"created_at":"2015-09-21T18:59:09Z","updated_at":"2015-09-21T18:59:09Z","body":"> Can't ATM. Page dies w/ a JS error showing in the console.\r\n\r\nYeah, sorry. At this stage it only works in Chrome. Here is a picture:\r\n![skaermbillede 2015-09-21 kl 20 55 19](https://cloud.githubusercontent.com/assets/505333/10001651/1935a792-60a3-11e5-99c0-f8a8f2b1284b.png)\r\n\r\n>  I'm not fully understanding if you're using some sort of global state to keep track of what init callback are called while the before/after callbacks are called. Or what you're using that for.\r\n\r\nThat is exactly what I'm doing. Here is the code If you are interested: https://github.com/AndreasMadsen/dprof/blob/master/dprof.js#L82L94\r\n\r\n> Could you write a basic test case w/ comments on what you'd expect to happen?\r\n\r\nSure, shouldn't be a problem."}}