{"action":"created","issue":{"url":"https://api.github.com/repos/polotek/libxmljs/issues/299","labels_url":"https://api.github.com/repos/polotek/libxmljs/issues/299/labels{/name}","comments_url":"https://api.github.com/repos/polotek/libxmljs/issues/299/comments","events_url":"https://api.github.com/repos/polotek/libxmljs/issues/299/events","html_url":"https://github.com/polotek/libxmljs/pull/299","id":66698564,"number":299,"title":"Make NanAdjustExternalMemory thread-safe","user":{"login":"nathan7","id":50854,"avatar_url":"https://avatars.githubusercontent.com/u/50854?v=3","gravatar_id":"","url":"https://api.github.com/users/nathan7","html_url":"https://github.com/nathan7","followers_url":"https://api.github.com/users/nathan7/followers","following_url":"https://api.github.com/users/nathan7/following{/other_user}","gists_url":"https://api.github.com/users/nathan7/gists{/gist_id}","starred_url":"https://api.github.com/users/nathan7/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nathan7/subscriptions","organizations_url":"https://api.github.com/users/nathan7/orgs","repos_url":"https://api.github.com/users/nathan7/repos","events_url":"https://api.github.com/users/nathan7/events{/privacy}","received_events_url":"https://api.github.com/users/nathan7/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":18,"created_at":"2015-04-06T20:33:22Z","updated_at":"2015-04-07T17:58:51Z","closed_at":"2015-04-07T17:58:51Z","pull_request":{"url":"https://api.github.com/repos/polotek/libxmljs/pulls/299","html_url":"https://github.com/polotek/libxmljs/pull/299","diff_url":"https://github.com/polotek/libxmljs/pull/299.diff","patch_url":"https://github.com/polotek/libxmljs/pull/299.patch"},"body":"Fixes #296.\r\nWIP: currently causes the tests to hang on exit."},"comment":{"url":"https://api.github.com/repos/polotek/libxmljs/issues/comments/90676810","html_url":"https://github.com/polotek/libxmljs/pull/299#issuecomment-90676810","issue_url":"https://api.github.com/repos/polotek/libxmljs/issues/299","id":90676810,"user":{"login":"nathan7","id":50854,"avatar_url":"https://avatars.githubusercontent.com/u/50854?v=3","gravatar_id":"","url":"https://api.github.com/users/nathan7","html_url":"https://github.com/nathan7","followers_url":"https://api.github.com/users/nathan7/followers","following_url":"https://api.github.com/users/nathan7/following{/other_user}","gists_url":"https://api.github.com/users/nathan7/gists{/gist_id}","starred_url":"https://api.github.com/users/nathan7/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nathan7/subscriptions","organizations_url":"https://api.github.com/users/nathan7/orgs","repos_url":"https://api.github.com/users/nathan7/repos","events_url":"https://api.github.com/users/nathan7/events{/privacy}","received_events_url":"https://api.github.com/users/nathan7/received_events","type":"User","site_admin":false},"created_at":"2015-04-07T17:58:51Z","updated_at":"2015-04-07T17:58:51Z","body":"I did some further testing to try and falsify my hypothesis: @polotek, you're right, and the original issue is wrong. This PR is useless entirely. The original issue mis-identifies the cause.\r\n\r\nThe following patch does not cause an `abort()`, and all tests run successfully.\r\nMy earlier findings were a mistake in my experiment, due to a misunderstanding about what gets executed when.\r\n```diff\r\ndiff --git a/src/libxmljs.cc b/src/libxmljs.cc\r\nindex dd03395..997a333 100644\r\n--- a/src/libxmljs.cc\r\n+++ b/src/libxmljs.cc\r\n@@ -22,6 +22,19 @@ int xml_memory_used_last = 0;\r\n uv_async_t xml_memory_handle;\r\n uv_mutex_t xml_memory_mutex;\r\n \r\n+pthread_t thread = 0;\r\n+void check_thread() {\r\n+    pthread_t self = pthread_self();\r\n+    if (thread == 0) {\r\n+        thread = self;\r\n+    }\r\n+\r\n+    if (!pthread_equal(self, thread)) {\r\n+        abort();\r\n+    }\r\n+    thread = self;\r\n+}\r\n+\r\n NAUV_WORK_CB(xml_memory_cb) {\r\n     // if v8 is no longer running, don't try to adjust memory\r\n     // this happens when the v8 vm is shutdown and the program is exiting\r\n@@ -42,6 +55,7 @@ NAUV_WORK_CB(xml_memory_cb) {\r\n     uv_mutex_lock(&xml_memory_mutex);\r\n     const int diff = xml_memory_used - xml_memory_used_last;\r\n     xml_memory_used_last = xml_memory_used;\r\n+    check_thread();\r\n     uv_mutex_unlock(&xml_memory_mutex);\r\n \r\n     NanAdjustExternalMemory(diff);\r\n@@ -55,6 +69,8 @@ NAN_INLINE void xml_memory_update() {\r\n         uv_async_send(&xml_memory_handle);\r\n     }\r\n \r\n+    check_thread();\r\n+\r\n     uv_mutex_unlock(&xml_memory_mutex);\r\n }\r\n```"}}