{"action":"created","comment":{"url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/comments/26580633","id":26580633,"diff_hunk":"@@ -203,61 +186,84 @@ public ResponseBuffers receiveMessage(final int responseTo) {\n         if (isClosed()) {\n             throw new MongoSocketClosedException(\"Cannot read from a closed stream\", getServerAddress());\n         } else {\n-            while (!messages.containsKey(responseTo)) {\n+            try {\n+                ResponseBuffers responseBuffers = receiveResponseBuffers();\n                 try {\n-                    reading.acquire();\n-                    if (messages.containsKey(responseTo)) {\n-                        break;\n-                    }\n-                    ResponseBuffers responseBuffers = receiveResponseBuffers();\n-                    try {\n-                        connectionListener.messageReceived(new ConnectionMessageReceivedEvent(getId(),\n-                                                                                              responseBuffers.getReplyHeader()\n-                                                                                                             .getResponseTo(),\n-                                                                                              responseBuffers.getReplyHeader()\n-                                                                                                             .getMessageLength()));\n-                    } catch (Throwable t) {\n-                        LOGGER.warn(\"Exception when trying to signal messageReceived to the connectionListener\", t);\n-                    }\n-                    messages.put(responseBuffers.getReplyHeader().getResponseTo(), new Response(responseBuffers, null));\n-                } catch (Exception e) {\n-                    close();\n-                    messages.put(responseTo, new Response(null, translateReadException(e)));\n-                } finally {\n-                    reading.release();\n-                }\n-            }\n-            Response response = messages.remove(responseTo);\n-            if (response.hasError()) {\n-                Throwable t = response.getError();\n-                if (t instanceof MongoException) {\n-                    throw (MongoException) t;\n-                } else {\n-                    throw MongoException.fromThrowable(t);\n+                    connectionListener.messageReceived(new ConnectionMessageReceivedEvent(getId(),\n+                                                       responseBuffers.getReplyHeader().getResponseTo(),\n+                                                       responseBuffers.getReplyHeader().getMessageLength()));\n+                } catch (Throwable t) {\n+                    LOGGER.warn(\"Exception when trying to signal messageReceived to the connectionListener\", t);\n                 }\n+                return responseBuffers;\n+            } catch (Exception e) {\n+                close();\n+                throw translateReadException(e);\n             }\n-            return response.getResult();\n         }\n     }\n \n     @Override\n     public void sendMessageAsync(final List<ByteBuf> byteBuffers, final int lastRequestId, final SingleResultCallback<Void> callback) {\n         notNull(\"open\", stream);\n+        notNull(\"callback\", callback);\n         if (LOGGER.isTraceEnabled()) {\n-            LOGGER.trace(format(\"Queuing send message: %s\", lastRequestId));\n+            LOGGER.trace(format(\"Send message async: %s\", lastRequestId));\n+        }\n+        final SingleResultCallback<Void> safeCallback = errorHandlingCallback(callback, LOGGER);\n+        if (isClosed()) {\n+            try {\n+                safeCallback.onResult(null, new MongoSocketClosedException(\"Cannot write to a closed stream\", getServerAddress()));\n+            } catch (Throwable t) {\n+                LOGGER.warn(\"Exception calling callback\", t);\n+            }\n+        } else {\n+            stream.writeAsync(byteBuffers, new AsyncCompletionHandler<Void>() {\n+                @Override\n+                public void completed(final Void v) {\n+                    try {\n+                        connectionListener.messagesSent(new ConnectionMessagesSentEvent(getId(), lastRequestId,\n+                                getTotalRemaining(byteBuffers)));\n+                    } catch (Throwable t) {\n+                        LOGGER.warn(\"Exception when trying to signal messagesSent to the connectionListener\", t);\n+                    }\n+                    safeCallback.onResult(null, null);\n+                }\n+\n+                @Override\n+                public void failed(final Throwable t) {\n+                    close();\n+                    safeCallback.onResult(null, translateWriteException(t));\n+                }\n+            });\n         }\n-        writeQueue.add(new SendMessageAsync(byteBuffers, lastRequestId, errorHandlingCallback(callback, LOGGER)));\n-        processPendingWrites();\n     }\n \n     @Override\n     public void receiveMessageAsync(final int responseTo, final SingleResultCallback<ResponseBuffers> callback) {\n         notNull(\"open\", stream);\n+        notNull(\"callback\", callback);\n         if (LOGGER.isTraceEnabled()) {\n-            LOGGER.trace(format(\"Queuing read message: %s\", responseTo));\n+            LOGGER.trace(format(\"Receive message async: %s\", responseTo));\n+        }\n+        if (isClosed()) {\n+            if (LOGGER.isTraceEnabled()) {\n+                LOGGER.trace(format(\"Stream closed: %s\", responseTo));\n+            }\n+            errorHandlingCallback(callback, LOGGER).onResult(null,\n+                    new MongoSocketClosedException(\"Cannot read from a closed stream\", getServerAddress()));\n+        } else {\n+            fillAndFlipBuffer(REPLY_HEADER_LENGTH,\n+                    errorHandlingCallback(new ResponseHeaderCallback(new SingleResultCallback<ResponseBuffers>() {\n+                        @Override\n+                        public void onResult(final ResponseBuffers result, final Throwable t) {\n+                            if (LOGGER.isTraceEnabled()) {\n+                                LOGGER.trace(format(\"Received message: %s\", responseTo));\n+                            }\n+                            callback.onResult(result, t);","path":"driver-core/src/main/com/mongodb/connection/InternalStreamConnection.java","position":171,"original_position":171,"commit_id":"84829a23f9c05d21d5efb98b64dfaacfc73ec98d","original_commit_id":"84829a23f9c05d21d5efb98b64dfaacfc73ec98d","user":{"login":"jyemin","id":1111546,"avatar_url":"https://avatars.githubusercontent.com/u/1111546?v=3","gravatar_id":"","url":"https://api.github.com/users/jyemin","html_url":"https://github.com/jyemin","followers_url":"https://api.github.com/users/jyemin/followers","following_url":"https://api.github.com/users/jyemin/following{/other_user}","gists_url":"https://api.github.com/users/jyemin/gists{/gist_id}","starred_url":"https://api.github.com/users/jyemin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jyemin/subscriptions","organizations_url":"https://api.github.com/users/jyemin/orgs","repos_url":"https://api.github.com/users/jyemin/repos","events_url":"https://api.github.com/users/jyemin/events{/privacy}","received_events_url":"https://api.github.com/users/jyemin/received_events","type":"User","site_admin":false},"body":"Did we lose the errorHandlingCallback here?","created_at":"2015-03-17T15:07:42Z","updated_at":"2015-03-17T15:07:42Z","html_url":"https://github.com/rozza/mongo-java-driver/pull/72#discussion_r26580633","pull_request_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/72","_links":{"self":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/comments/26580633"},"html":{"href":"https://github.com/rozza/mongo-java-driver/pull/72#discussion_r26580633"},"pull_request":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/72"}}},"pull_request":{"url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/72","id":31342795,"html_url":"https://github.com/rozza/mongo-java-driver/pull/72","diff_url":"https://github.com/rozza/mongo-java-driver/pull/72.diff","patch_url":"https://github.com/rozza/mongo-java-driver/pull/72.patch","issue_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/72","number":72,"state":"open","locked":false,"title":"Simplified InternalConnectionStream","user":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"body":"Now assumes exclusive use of the stream. The plan is for multithread use again in the future.\r\nJAVA-1674\n\n<!-- Reviewable:start -->\n[<img src=\"https://reviewable.io/review_button.png\" height=40 alt=\"Review on Reviewable\"/>](https://reviewable.io/reviews/rozza/mongo-java-driver/72)\n<!-- Reviewable:end -->\n","created_at":"2015-03-17T15:00:34Z","updated_at":"2015-03-17T15:07:42Z","closed_at":null,"merged_at":null,"merge_commit_sha":"29e7940133a5339ad60da1241fa35f6b90006d96","assignee":{"login":"jyemin","id":1111546,"avatar_url":"https://avatars.githubusercontent.com/u/1111546?v=3","gravatar_id":"","url":"https://api.github.com/users/jyemin","html_url":"https://github.com/jyemin","followers_url":"https://api.github.com/users/jyemin/followers","following_url":"https://api.github.com/users/jyemin/following{/other_user}","gists_url":"https://api.github.com/users/jyemin/gists{/gist_id}","starred_url":"https://api.github.com/users/jyemin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jyemin/subscriptions","organizations_url":"https://api.github.com/users/jyemin/orgs","repos_url":"https://api.github.com/users/jyemin/repos","events_url":"https://api.github.com/users/jyemin/events{/privacy}","received_events_url":"https://api.github.com/users/jyemin/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/72/commits","review_comments_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/72/comments","review_comment_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/72/comments","statuses_url":"https://api.github.com/repos/rozza/mongo-java-driver/statuses/84829a23f9c05d21d5efb98b64dfaacfc73ec98d","head":{"label":"rozza:j1674","ref":"j1674","sha":"84829a23f9c05d21d5efb98b64dfaacfc73ec98d","user":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"repo":{"id":10246360,"name":"mongo-java-driver","full_name":"rozza/mongo-java-driver","owner":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/rozza/mongo-java-driver","description":"java driver for mongo","fork":true,"url":"https://api.github.com/repos/rozza/mongo-java-driver","forks_url":"https://api.github.com/repos/rozza/mongo-java-driver/forks","keys_url":"https://api.github.com/repos/rozza/mongo-java-driver/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rozza/mongo-java-driver/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rozza/mongo-java-driver/teams","hooks_url":"https://api.github.com/repos/rozza/mongo-java-driver/hooks","issue_events_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/events{/number}","events_url":"https://api.github.com/repos/rozza/mongo-java-driver/events","assignees_url":"https://api.github.com/repos/rozza/mongo-java-driver/assignees{/user}","branches_url":"https://api.github.com/repos/rozza/mongo-java-driver/branches{/branch}","tags_url":"https://api.github.com/repos/rozza/mongo-java-driver/tags","blobs_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/refs{/sha}","trees_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rozza/mongo-java-driver/statuses/{sha}","languages_url":"https://api.github.com/repos/rozza/mongo-java-driver/languages","stargazers_url":"https://api.github.com/repos/rozza/mongo-java-driver/stargazers","contributors_url":"https://api.github.com/repos/rozza/mongo-java-driver/contributors","subscribers_url":"https://api.github.com/repos/rozza/mongo-java-driver/subscribers","subscription_url":"https://api.github.com/repos/rozza/mongo-java-driver/subscription","commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/commits{/sha}","git_commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/commits{/sha}","comments_url":"https://api.github.com/repos/rozza/mongo-java-driver/comments{/number}","issue_comment_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/comments{/number}","contents_url":"https://api.github.com/repos/rozza/mongo-java-driver/contents/{+path}","compare_url":"https://api.github.com/repos/rozza/mongo-java-driver/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rozza/mongo-java-driver/merges","archive_url":"https://api.github.com/repos/rozza/mongo-java-driver/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rozza/mongo-java-driver/downloads","issues_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues{/number}","pulls_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls{/number}","milestones_url":"https://api.github.com/repos/rozza/mongo-java-driver/milestones{/number}","notifications_url":"https://api.github.com/repos/rozza/mongo-java-driver/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rozza/mongo-java-driver/labels{/name}","releases_url":"https://api.github.com/repos/rozza/mongo-java-driver/releases{/id}","created_at":"2013-05-23T15:06:03Z","updated_at":"2015-03-17T09:26:53Z","pushed_at":"2015-03-17T15:01:02Z","git_url":"git://github.com/rozza/mongo-java-driver.git","ssh_url":"git@github.com:rozza/mongo-java-driver.git","clone_url":"https://github.com/rozza/mongo-java-driver.git","svn_url":"https://github.com/rozza/mongo-java-driver","homepage":"","size":35391,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"rozza:master","ref":"master","sha":"a0d02f4dfc6f0bd03347c04a880a1021e17bc18f","user":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"repo":{"id":10246360,"name":"mongo-java-driver","full_name":"rozza/mongo-java-driver","owner":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/rozza/mongo-java-driver","description":"java driver for mongo","fork":true,"url":"https://api.github.com/repos/rozza/mongo-java-driver","forks_url":"https://api.github.com/repos/rozza/mongo-java-driver/forks","keys_url":"https://api.github.com/repos/rozza/mongo-java-driver/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rozza/mongo-java-driver/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rozza/mongo-java-driver/teams","hooks_url":"https://api.github.com/repos/rozza/mongo-java-driver/hooks","issue_events_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/events{/number}","events_url":"https://api.github.com/repos/rozza/mongo-java-driver/events","assignees_url":"https://api.github.com/repos/rozza/mongo-java-driver/assignees{/user}","branches_url":"https://api.github.com/repos/rozza/mongo-java-driver/branches{/branch}","tags_url":"https://api.github.com/repos/rozza/mongo-java-driver/tags","blobs_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/refs{/sha}","trees_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rozza/mongo-java-driver/statuses/{sha}","languages_url":"https://api.github.com/repos/rozza/mongo-java-driver/languages","stargazers_url":"https://api.github.com/repos/rozza/mongo-java-driver/stargazers","contributors_url":"https://api.github.com/repos/rozza/mongo-java-driver/contributors","subscribers_url":"https://api.github.com/repos/rozza/mongo-java-driver/subscribers","subscription_url":"https://api.github.com/repos/rozza/mongo-java-driver/subscription","commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/commits{/sha}","git_commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/commits{/sha}","comments_url":"https://api.github.com/repos/rozza/mongo-java-driver/comments{/number}","issue_comment_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/comments{/number}","contents_url":"https://api.github.com/repos/rozza/mongo-java-driver/contents/{+path}","compare_url":"https://api.github.com/repos/rozza/mongo-java-driver/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rozza/mongo-java-driver/merges","archive_url":"https://api.github.com/repos/rozza/mongo-java-driver/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rozza/mongo-java-driver/downloads","issues_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues{/number}","pulls_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls{/number}","milestones_url":"https://api.github.com/repos/rozza/mongo-java-driver/milestones{/number}","notifications_url":"https://api.github.com/repos/rozza/mongo-java-driver/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rozza/mongo-java-driver/labels{/name}","releases_url":"https://api.github.com/repos/rozza/mongo-java-driver/releases{/id}","created_at":"2013-05-23T15:06:03Z","updated_at":"2015-03-17T09:26:53Z","pushed_at":"2015-03-17T15:01:02Z","git_url":"git://github.com/rozza/mongo-java-driver.git","ssh_url":"git@github.com:rozza/mongo-java-driver.git","clone_url":"https://github.com/rozza/mongo-java-driver.git","svn_url":"https://github.com/rozza/mongo-java-driver","homepage":"","size":35391,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/72"},"html":{"href":"https://github.com/rozza/mongo-java-driver/pull/72"},"issue":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/issues/72"},"comments":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/issues/72/comments"},"review_comments":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/72/comments"},"review_comment":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/72/commits"},"statuses":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/statuses/84829a23f9c05d21d5efb98b64dfaacfc73ec98d"}}}}