{"action":"created","comment":{"url":"https://api.github.com/repos/aspnet/KestrelHttpServer/pulls/comments/50147515","id":50147515,"diff_hunk":"@@ -49,8 +49,41 @@ public void Write(ArraySegment<byte> buffer, bool immediate, bool chunk)\n \n         public Task WriteAsync(ArraySegment<byte> buffer, bool immediate, bool chunk, CancellationToken cancellationToken)\n         {\n-            // TODO: Use _outputStream.WriteAsync\n-            Write(buffer, immediate, chunk);\n+            lock (_writeLock)\n+            {\n+                if (chunk && buffer.Array != null)\n+                {\n+                    return WriteAsyncChunked(buffer, cancellationToken);\n+                }\n+\n+                return _outputStream.WriteAsync(buffer.Array ?? _nullBuffer, buffer.Offset, buffer.Count, cancellationToken);\n+            }\n+        }\n+\n+        private Task WriteAsyncChunked(ArraySegment<byte> buffer, CancellationToken cancellationToken)\n+        {\n+            var beginChunkBytes = ChunkWriter.BeginChunkBytes(buffer.Count);\n+\n+            // When calls return the tasks they have copied the data to the backing buffer\n+            // so is safe to send one after another and await at end\n+\n+            var preambleTask = _outputStream.WriteAsync(beginChunkBytes.Array, beginChunkBytes.Offset, beginChunkBytes.Count, cancellationToken);\n+            var dataTask = _outputStream.WriteAsync(buffer.Array ?? _nullBuffer, buffer.Offset, buffer.Count, cancellationToken);\n+            var endTask = _outputStream.WriteAsync(_endChunkBytes, 0, _endChunkBytes.Length, cancellationToken);\n+\n+            if (!preambleTask.IsCompleted)\n+            {\n+                return Task.WhenAll(preambleTask, dataTask, endTask);\n+            }\n+            if (!dataTask.IsCompleted)\n+            {\n+                return Task.WhenAll(dataTask, endTask);\n+            }\n+            if (!endTask.IsCompleted)\n+            {\n+                return endTask;","path":"src/Microsoft.AspNet.Server.Kestrel/Filter/StreamSocketOutput.cs","position":38,"original_position":38,"commit_id":"d3a54d67e7cf7303610759db86a6c555a7c80b99","original_commit_id":"d3a54d67e7cf7303610759db86a6c555a7c80b99","user":{"login":"benaadams","id":1142958,"avatar_url":"https://avatars.githubusercontent.com/u/1142958?v=3","gravatar_id":"","url":"https://api.github.com/users/benaadams","html_url":"https://github.com/benaadams","followers_url":"https://api.github.com/users/benaadams/followers","following_url":"https://api.github.com/users/benaadams/following{/other_user}","gists_url":"https://api.github.com/users/benaadams/gists{/gist_id}","starred_url":"https://api.github.com/users/benaadams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benaadams/subscriptions","organizations_url":"https://api.github.com/users/benaadams/orgs","repos_url":"https://api.github.com/users/benaadams/repos","events_url":"https://api.github.com/users/benaadams/events{/privacy}","received_events_url":"https://api.github.com/users/benaadams/received_events","type":"User","site_admin":false},"body":"That's a good point, no","created_at":"2016-01-19T17:52:42Z","updated_at":"2016-01-19T17:52:42Z","html_url":"https://github.com/aspnet/KestrelHttpServer/pull/588#discussion_r50147515","pull_request_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/pulls/588","_links":{"self":{"href":"https://api.github.com/repos/aspnet/KestrelHttpServer/pulls/comments/50147515"},"html":{"href":"https://github.com/aspnet/KestrelHttpServer/pull/588#discussion_r50147515"},"pull_request":{"href":"https://api.github.com/repos/aspnet/KestrelHttpServer/pulls/588"}}},"pull_request":{"url":"https://api.github.com/repos/aspnet/KestrelHttpServer/pulls/588","id":56281622,"html_url":"https://github.com/aspnet/KestrelHttpServer/pull/588","diff_url":"https://github.com/aspnet/KestrelHttpServer/pull/588.diff","patch_url":"https://github.com/aspnet/KestrelHttpServer/pull/588.patch","issue_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/588","number":588,"state":"open","locked":false,"title":"[Blocked] StreamSocketOutput WriteAsync as async","user":{"login":"benaadams","id":1142958,"avatar_url":"https://avatars.githubusercontent.com/u/1142958?v=3","gravatar_id":"","url":"https://api.github.com/users/benaadams","html_url":"https://github.com/benaadams","followers_url":"https://api.github.com/users/benaadams/followers","following_url":"https://api.github.com/users/benaadams/following{/other_user}","gists_url":"https://api.github.com/users/benaadams/gists{/gist_id}","starred_url":"https://api.github.com/users/benaadams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benaadams/subscriptions","organizations_url":"https://api.github.com/users/benaadams/orgs","repos_url":"https://api.github.com/users/benaadams/repos","events_url":"https://api.github.com/users/benaadams/events{/privacy}","received_events_url":"https://api.github.com/users/benaadams/received_events","type":"User","site_admin":false},"body":"Blocked by https://github.com/dotnet/corefx/issues/5488 / https://github.com/dotnet/corefx/issues/5077\r\nResolves #569","created_at":"2016-01-17T16:42:07Z","updated_at":"2016-01-19T17:52:42Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4cc8bf1461d8f8c3d35a1b58499d9de5dfa89cba","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/pulls/588/commits","review_comments_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/pulls/588/comments","review_comment_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/pulls/comments{/number}","comments_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/588/comments","statuses_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/statuses/d3a54d67e7cf7303610759db86a6c555a7c80b99","head":{"label":"benaadams:async-write","ref":"async-write","sha":"d3a54d67e7cf7303610759db86a6c555a7c80b99","user":{"login":"benaadams","id":1142958,"avatar_url":"https://avatars.githubusercontent.com/u/1142958?v=3","gravatar_id":"","url":"https://api.github.com/users/benaadams","html_url":"https://github.com/benaadams","followers_url":"https://api.github.com/users/benaadams/followers","following_url":"https://api.github.com/users/benaadams/following{/other_user}","gists_url":"https://api.github.com/users/benaadams/gists{/gist_id}","starred_url":"https://api.github.com/users/benaadams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benaadams/subscriptions","organizations_url":"https://api.github.com/users/benaadams/orgs","repos_url":"https://api.github.com/users/benaadams/repos","events_url":"https://api.github.com/users/benaadams/events{/privacy}","received_events_url":"https://api.github.com/users/benaadams/received_events","type":"User","site_admin":false},"repo":{"id":38999457,"name":"KestrelHttpServer","full_name":"benaadams/KestrelHttpServer","owner":{"login":"benaadams","id":1142958,"avatar_url":"https://avatars.githubusercontent.com/u/1142958?v=3","gravatar_id":"","url":"https://api.github.com/users/benaadams","html_url":"https://github.com/benaadams","followers_url":"https://api.github.com/users/benaadams/followers","following_url":"https://api.github.com/users/benaadams/following{/other_user}","gists_url":"https://api.github.com/users/benaadams/gists{/gist_id}","starred_url":"https://api.github.com/users/benaadams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benaadams/subscriptions","organizations_url":"https://api.github.com/users/benaadams/orgs","repos_url":"https://api.github.com/users/benaadams/repos","events_url":"https://api.github.com/users/benaadams/events{/privacy}","received_events_url":"https://api.github.com/users/benaadams/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/benaadams/KestrelHttpServer","description":"A development web server for ASP.NET vNext based on libuv.","fork":true,"url":"https://api.github.com/repos/benaadams/KestrelHttpServer","forks_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/forks","keys_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/keys{/key_id}","collaborators_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/teams","hooks_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/hooks","issue_events_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/issues/events{/number}","events_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/events","assignees_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/assignees{/user}","branches_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/branches{/branch}","tags_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/tags","blobs_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/git/refs{/sha}","trees_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/git/trees{/sha}","statuses_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/statuses/{sha}","languages_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/languages","stargazers_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/stargazers","contributors_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/contributors","subscribers_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/subscribers","subscription_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/subscription","commits_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/commits{/sha}","git_commits_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/git/commits{/sha}","comments_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/comments{/number}","issue_comment_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/issues/comments{/number}","contents_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/contents/{+path}","compare_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/compare/{base}...{head}","merges_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/merges","archive_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/downloads","issues_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/issues{/number}","pulls_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/pulls{/number}","milestones_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/milestones{/number}","notifications_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/labels{/name}","releases_url":"https://api.github.com/repos/benaadams/KestrelHttpServer/releases{/id}","created_at":"2015-07-13T08:10:06Z","updated_at":"2015-07-13T08:10:08Z","pushed_at":"2016-01-18T23:24:43Z","git_url":"git://github.com/benaadams/KestrelHttpServer.git","ssh_url":"git@github.com:benaadams/KestrelHttpServer.git","clone_url":"https://github.com/benaadams/KestrelHttpServer.git","svn_url":"https://github.com/benaadams/KestrelHttpServer","homepage":"","size":2203,"stargazers_count":0,"watchers_count":0,"language":"C#","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"dev"}},"base":{"label":"aspnet:dev","ref":"dev","sha":"9885377148e9d50ec45f9c7ca44c8b645a415f6b","user":{"login":"aspnet","id":6476660,"avatar_url":"https://avatars.githubusercontent.com/u/6476660?v=3","gravatar_id":"","url":"https://api.github.com/users/aspnet","html_url":"https://github.com/aspnet","followers_url":"https://api.github.com/users/aspnet/followers","following_url":"https://api.github.com/users/aspnet/following{/other_user}","gists_url":"https://api.github.com/users/aspnet/gists{/gist_id}","starred_url":"https://api.github.com/users/aspnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aspnet/subscriptions","organizations_url":"https://api.github.com/users/aspnet/orgs","repos_url":"https://api.github.com/users/aspnet/repos","events_url":"https://api.github.com/users/aspnet/events{/privacy}","received_events_url":"https://api.github.com/users/aspnet/received_events","type":"Organization","site_admin":false},"repo":{"id":20344662,"name":"KestrelHttpServer","full_name":"aspnet/KestrelHttpServer","owner":{"login":"aspnet","id":6476660,"avatar_url":"https://avatars.githubusercontent.com/u/6476660?v=3","gravatar_id":"","url":"https://api.github.com/users/aspnet","html_url":"https://github.com/aspnet","followers_url":"https://api.github.com/users/aspnet/followers","following_url":"https://api.github.com/users/aspnet/following{/other_user}","gists_url":"https://api.github.com/users/aspnet/gists{/gist_id}","starred_url":"https://api.github.com/users/aspnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aspnet/subscriptions","organizations_url":"https://api.github.com/users/aspnet/orgs","repos_url":"https://api.github.com/users/aspnet/repos","events_url":"https://api.github.com/users/aspnet/events{/privacy}","received_events_url":"https://api.github.com/users/aspnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/aspnet/KestrelHttpServer","description":"A web server for ASP.NET 5 based on libuv.","fork":false,"url":"https://api.github.com/repos/aspnet/KestrelHttpServer","forks_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/forks","keys_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/teams","hooks_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/hooks","issue_events_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/events{/number}","events_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/events","assignees_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/assignees{/user}","branches_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/branches{/branch}","tags_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/tags","blobs_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/git/refs{/sha}","trees_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/statuses/{sha}","languages_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/languages","stargazers_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/stargazers","contributors_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/contributors","subscribers_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/subscribers","subscription_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/subscription","commits_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/commits{/sha}","git_commits_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/git/commits{/sha}","comments_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/comments{/number}","issue_comment_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/comments{/number}","contents_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/contents/{+path}","compare_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/merges","archive_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/downloads","issues_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues{/number}","pulls_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/pulls{/number}","milestones_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/milestones{/number}","notifications_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/labels{/name}","releases_url":"https://api.github.com/repos/aspnet/KestrelHttpServer/releases{/id}","created_at":"2014-05-30T22:46:28Z","updated_at":"2016-01-19T14:30:49Z","pushed_at":"2016-01-19T16:48:23Z","git_url":"git://github.com/aspnet/KestrelHttpServer.git","ssh_url":"git@github.com:aspnet/KestrelHttpServer.git","clone_url":"https://github.com/aspnet/KestrelHttpServer.git","svn_url":"https://github.com/aspnet/KestrelHttpServer","homepage":"","size":4116,"stargazers_count":630,"watchers_count":630,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":133,"mirror_url":null,"open_issues_count":60,"forks":133,"open_issues":60,"watchers":630,"default_branch":"dev"}},"_links":{"self":{"href":"https://api.github.com/repos/aspnet/KestrelHttpServer/pulls/588"},"html":{"href":"https://github.com/aspnet/KestrelHttpServer/pull/588"},"issue":{"href":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/588"},"comments":{"href":"https://api.github.com/repos/aspnet/KestrelHttpServer/issues/588/comments"},"review_comments":{"href":"https://api.github.com/repos/aspnet/KestrelHttpServer/pulls/588/comments"},"review_comment":{"href":"https://api.github.com/repos/aspnet/KestrelHttpServer/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/aspnet/KestrelHttpServer/pulls/588/commits"},"statuses":{"href":"https://api.github.com/repos/aspnet/KestrelHttpServer/statuses/d3a54d67e7cf7303610759db86a6c555a7c80b99"}}}}