{"action":"created","comment":{"url":"https://api.github.com/repos/apache/kafka/pulls/comments/67625334","id":67625334,"diff_hunk":"@@ -1426,8 +1425,9 @@ private void ensureNotClosed() {\n     private void acquire() {\n         ensureNotClosed();\n         long threadId = Thread.currentThread().getId();\n-        if (threadId != currentThread.get() && !currentThread.compareAndSet(NO_CURRENT_THREAD, threadId))\n-            throw new ConcurrentModificationException(\"KafkaConsumer is not safe for multi-threaded access\");\n+\n+        if (!currentThread.compareAndSet(NO_CURRENT_THREAD, Thread.currentThread()) && currentThread.get().getId() != Thread.currentThread().getId())","path":"clients/src/main/java/org/apache/kafka/clients/consumer/KafkaConsumer.java","position":36,"original_position":36,"commit_id":"026bc6b03a40b33b7e9c9f244a8370a395564c81","original_commit_id":"026bc6b03a40b33b7e9c9f244a8370a395564c81","user":{"login":"ewencp","id":47366,"avatar_url":"https://avatars.githubusercontent.com/u/47366?v=3","gravatar_id":"","url":"https://api.github.com/users/ewencp","html_url":"https://github.com/ewencp","followers_url":"https://api.github.com/users/ewencp/followers","following_url":"https://api.github.com/users/ewencp/following{/other_user}","gists_url":"https://api.github.com/users/ewencp/gists{/gist_id}","starred_url":"https://api.github.com/users/ewencp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ewencp/subscriptions","organizations_url":"https://api.github.com/users/ewencp/orgs","repos_url":"https://api.github.com/users/ewencp/repos","events_url":"https://api.github.com/users/ewencp/events{/privacy}","received_events_url":"https://api.github.com/users/ewencp/received_events","type":"User","site_admin":false},"body":"You need to be careful about ordering here and how you check this. The old code first validates the IDs aren't the same and then tries to set the value (because the only way the IDs shouldn't match is if no thread is currently in a Consumer method call). This new code tries to set it first, and if it fails, it assumes that it is still set when comparing the IDs. However, if the initial call fails because another thread is accessing the Consumer, but then it finishes and calls release(), then calling `currentThread.get().getID()` will fail because it will have been reset back to `null`/`NO_CURRENT_THREAD` and you'll get a `NullPointerException`. Same goes for the subsequent call to `currentThread.get().getName()` in the error message.\r\n\r\nI think you want to call `currentThread.get()` *once*, and hold onto that value. No matter what happens, if at some point during this method call the value was a different thread, then we should trigger the `ConcurrentModificationException`. The problem is that you can't be guaranteed you'll perfectly capture the thread that was in conflict because `compareAndSet` doesn't let you know what the value was if it wasn't the expected value. So I think the error message creation just needs to be careful about this -- it's possible we see a conflict, but we cannot actually get the `Thread` object that caused the conflict (we couldn't do this with IDs either -- calling `currentThread.get()` when creating the error message could, by that point in time, return -1).\r\n\r\nI think the JDK8 version of AtomicReference may have methods that let you accomplish this, but atm we're stuck with JDK7.","created_at":"2016-06-19T22:52:06Z","updated_at":"2016-06-19T22:52:06Z","html_url":"https://github.com/apache/kafka/pull/1516#discussion_r67625334","pull_request_url":"https://api.github.com/repos/apache/kafka/pulls/1516","_links":{"self":{"href":"https://api.github.com/repos/apache/kafka/pulls/comments/67625334"},"html":{"href":"https://github.com/apache/kafka/pull/1516#discussion_r67625334"},"pull_request":{"href":"https://api.github.com/repos/apache/kafka/pulls/1516"}}},"pull_request":{"url":"https://api.github.com/repos/apache/kafka/pulls/1516","id":74159046,"html_url":"https://github.com/apache/kafka/pull/1516","diff_url":"https://github.com/apache/kafka/pull/1516.diff","patch_url":"https://github.com/apache/kafka/pull/1516.patch","issue_url":"https://api.github.com/repos/apache/kafka/issues/1516","number":1516,"state":"open","locked":false,"title":"KAFKA-3837: Report the thread name of the blocking thread when throwing ConcurrentModificationException","user":{"login":"bharatviswa504","id":8586345,"avatar_url":"https://avatars.githubusercontent.com/u/8586345?v=3","gravatar_id":"","url":"https://api.github.com/users/bharatviswa504","html_url":"https://github.com/bharatviswa504","followers_url":"https://api.github.com/users/bharatviswa504/followers","following_url":"https://api.github.com/users/bharatviswa504/following{/other_user}","gists_url":"https://api.github.com/users/bharatviswa504/gists{/gist_id}","starred_url":"https://api.github.com/users/bharatviswa504/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bharatviswa504/subscriptions","organizations_url":"https://api.github.com/users/bharatviswa504/orgs","repos_url":"https://api.github.com/users/bharatviswa504/repos","events_url":"https://api.github.com/users/bharatviswa504/events{/privacy}","received_events_url":"https://api.github.com/users/bharatviswa504/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-06-16T21:54:56Z","updated_at":"2016-06-19T22:52:06Z","closed_at":null,"merged_at":null,"merge_commit_sha":"97f773315a808e6c4548fd2aa97c95448ba77f6e","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/kafka/pulls/1516/commits","review_comments_url":"https://api.github.com/repos/apache/kafka/pulls/1516/comments","review_comment_url":"https://api.github.com/repos/apache/kafka/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/kafka/issues/1516/comments","statuses_url":"https://api.github.com/repos/apache/kafka/statuses/026bc6b03a40b33b7e9c9f244a8370a395564c81","head":{"label":"bharatviswa504:KAFKA-3837-1","ref":"KAFKA-3837-1","sha":"026bc6b03a40b33b7e9c9f244a8370a395564c81","user":{"login":"bharatviswa504","id":8586345,"avatar_url":"https://avatars.githubusercontent.com/u/8586345?v=3","gravatar_id":"","url":"https://api.github.com/users/bharatviswa504","html_url":"https://github.com/bharatviswa504","followers_url":"https://api.github.com/users/bharatviswa504/followers","following_url":"https://api.github.com/users/bharatviswa504/following{/other_user}","gists_url":"https://api.github.com/users/bharatviswa504/gists{/gist_id}","starred_url":"https://api.github.com/users/bharatviswa504/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bharatviswa504/subscriptions","organizations_url":"https://api.github.com/users/bharatviswa504/orgs","repos_url":"https://api.github.com/users/bharatviswa504/repos","events_url":"https://api.github.com/users/bharatviswa504/events{/privacy}","received_events_url":"https://api.github.com/users/bharatviswa504/received_events","type":"User","site_admin":false},"repo":{"id":57098397,"name":"kafka","full_name":"bharatviswa504/kafka","owner":{"login":"bharatviswa504","id":8586345,"avatar_url":"https://avatars.githubusercontent.com/u/8586345?v=3","gravatar_id":"","url":"https://api.github.com/users/bharatviswa504","html_url":"https://github.com/bharatviswa504","followers_url":"https://api.github.com/users/bharatviswa504/followers","following_url":"https://api.github.com/users/bharatviswa504/following{/other_user}","gists_url":"https://api.github.com/users/bharatviswa504/gists{/gist_id}","starred_url":"https://api.github.com/users/bharatviswa504/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bharatviswa504/subscriptions","organizations_url":"https://api.github.com/users/bharatviswa504/orgs","repos_url":"https://api.github.com/users/bharatviswa504/repos","events_url":"https://api.github.com/users/bharatviswa504/events{/privacy}","received_events_url":"https://api.github.com/users/bharatviswa504/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bharatviswa504/kafka","description":"Mirror of Apache Kafka","fork":true,"url":"https://api.github.com/repos/bharatviswa504/kafka","forks_url":"https://api.github.com/repos/bharatviswa504/kafka/forks","keys_url":"https://api.github.com/repos/bharatviswa504/kafka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bharatviswa504/kafka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bharatviswa504/kafka/teams","hooks_url":"https://api.github.com/repos/bharatviswa504/kafka/hooks","issue_events_url":"https://api.github.com/repos/bharatviswa504/kafka/issues/events{/number}","events_url":"https://api.github.com/repos/bharatviswa504/kafka/events","assignees_url":"https://api.github.com/repos/bharatviswa504/kafka/assignees{/user}","branches_url":"https://api.github.com/repos/bharatviswa504/kafka/branches{/branch}","tags_url":"https://api.github.com/repos/bharatviswa504/kafka/tags","blobs_url":"https://api.github.com/repos/bharatviswa504/kafka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bharatviswa504/kafka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bharatviswa504/kafka/git/refs{/sha}","trees_url":"https://api.github.com/repos/bharatviswa504/kafka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bharatviswa504/kafka/statuses/{sha}","languages_url":"https://api.github.com/repos/bharatviswa504/kafka/languages","stargazers_url":"https://api.github.com/repos/bharatviswa504/kafka/stargazers","contributors_url":"https://api.github.com/repos/bharatviswa504/kafka/contributors","subscribers_url":"https://api.github.com/repos/bharatviswa504/kafka/subscribers","subscription_url":"https://api.github.com/repos/bharatviswa504/kafka/subscription","commits_url":"https://api.github.com/repos/bharatviswa504/kafka/commits{/sha}","git_commits_url":"https://api.github.com/repos/bharatviswa504/kafka/git/commits{/sha}","comments_url":"https://api.github.com/repos/bharatviswa504/kafka/comments{/number}","issue_comment_url":"https://api.github.com/repos/bharatviswa504/kafka/issues/comments{/number}","contents_url":"https://api.github.com/repos/bharatviswa504/kafka/contents/{+path}","compare_url":"https://api.github.com/repos/bharatviswa504/kafka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bharatviswa504/kafka/merges","archive_url":"https://api.github.com/repos/bharatviswa504/kafka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bharatviswa504/kafka/downloads","issues_url":"https://api.github.com/repos/bharatviswa504/kafka/issues{/number}","pulls_url":"https://api.github.com/repos/bharatviswa504/kafka/pulls{/number}","milestones_url":"https://api.github.com/repos/bharatviswa504/kafka/milestones{/number}","notifications_url":"https://api.github.com/repos/bharatviswa504/kafka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bharatviswa504/kafka/labels{/name}","releases_url":"https://api.github.com/repos/bharatviswa504/kafka/releases{/id}","deployments_url":"https://api.github.com/repos/bharatviswa504/kafka/deployments","created_at":"2016-04-26T04:43:45Z","updated_at":"2016-04-26T04:43:49Z","pushed_at":"2016-06-17T00:00:00Z","git_url":"git://github.com/bharatviswa504/kafka.git","ssh_url":"git@github.com:bharatviswa504/kafka.git","clone_url":"https://github.com/bharatviswa504/kafka.git","svn_url":"https://github.com/bharatviswa504/kafka","homepage":null,"size":32156,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"trunk"}},"base":{"label":"apache:trunk","ref":"trunk","sha":"7d9d1cb2355e33270703280ed6bb712033b03d26","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":2211243,"name":"kafka","full_name":"apache/kafka","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/kafka","description":"Mirror of Apache Kafka","fork":false,"url":"https://api.github.com/repos/apache/kafka","forks_url":"https://api.github.com/repos/apache/kafka/forks","keys_url":"https://api.github.com/repos/apache/kafka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/kafka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/kafka/teams","hooks_url":"https://api.github.com/repos/apache/kafka/hooks","issue_events_url":"https://api.github.com/repos/apache/kafka/issues/events{/number}","events_url":"https://api.github.com/repos/apache/kafka/events","assignees_url":"https://api.github.com/repos/apache/kafka/assignees{/user}","branches_url":"https://api.github.com/repos/apache/kafka/branches{/branch}","tags_url":"https://api.github.com/repos/apache/kafka/tags","blobs_url":"https://api.github.com/repos/apache/kafka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/kafka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/kafka/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/kafka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/kafka/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/kafka/languages","stargazers_url":"https://api.github.com/repos/apache/kafka/stargazers","contributors_url":"https://api.github.com/repos/apache/kafka/contributors","subscribers_url":"https://api.github.com/repos/apache/kafka/subscribers","subscription_url":"https://api.github.com/repos/apache/kafka/subscription","commits_url":"https://api.github.com/repos/apache/kafka/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/kafka/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/kafka/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/kafka/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/kafka/contents/{+path}","compare_url":"https://api.github.com/repos/apache/kafka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/kafka/merges","archive_url":"https://api.github.com/repos/apache/kafka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/kafka/downloads","issues_url":"https://api.github.com/repos/apache/kafka/issues{/number}","pulls_url":"https://api.github.com/repos/apache/kafka/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/kafka/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/kafka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/kafka/labels{/name}","releases_url":"https://api.github.com/repos/apache/kafka/releases{/id}","deployments_url":"https://api.github.com/repos/apache/kafka/deployments","created_at":"2011-08-15T18:06:16Z","updated_at":"2016-06-19T13:25:54Z","pushed_at":"2016-06-19T22:37:51Z","git_url":"git://github.com/apache/kafka.git","ssh_url":"git@github.com:apache/kafka.git","clone_url":"https://github.com/apache/kafka.git","svn_url":"https://github.com/apache/kafka","homepage":null,"size":44110,"stargazers_count":3272,"watchers_count":3272,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":2020,"mirror_url":"git://git.apache.org/kafka.git","open_issues_count":192,"forks":2020,"open_issues":192,"watchers":3272,"default_branch":"trunk"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/kafka/pulls/1516"},"html":{"href":"https://github.com/apache/kafka/pull/1516"},"issue":{"href":"https://api.github.com/repos/apache/kafka/issues/1516"},"comments":{"href":"https://api.github.com/repos/apache/kafka/issues/1516/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/kafka/pulls/1516/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/kafka/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/kafka/pulls/1516/commits"},"statuses":{"href":"https://api.github.com/repos/apache/kafka/statuses/026bc6b03a40b33b7e9c9f244a8370a395564c81"}}}}
{"actor":{"display_login":"ewencp"}}