{"action":"created","comment":{"url":"https://api.github.com/repos/netty/netty/pulls/comments/33492709","id":33492709,"diff_hunk":"@@ -317,85 +315,98 @@ private Http2Stream requireStream(int streamId) {\n      * </p>\n      */\n     private final class FlowControlledData extends FlowControlledBase {\n-        private ByteBuf data;\n+        private final ArrayDeque<Object> bufsAndPromises = new ArrayDeque<Object>();\n         private int size;\n+        private int padding;\n \n         private FlowControlledData(ChannelHandlerContext ctx, Http2Stream stream, ByteBuf buf, int padding,\n                                     boolean endOfStream, ChannelPromise promise) {\n             super(ctx, stream, padding, endOfStream, promise);\n-            this.data = buf;\n-            size = data.readableBytes() + padding;\n+            if (!promise.isVoid()) {\n+                bufsAndPromises.add(promise);\n+            }\n+            bufsAndPromises.add(buf);\n+            this.padding = padding;\n+            size = buf.readableBytes();\n         }\n \n         @Override\n         public int size() {\n-            return size;\n+            return size + padding;\n         }\n \n         @Override\n         public void error(Throwable cause) {\n-            ReferenceCountUtil.safeRelease(data);\n-            lifecycleManager.onException(ctx, cause);\n-            data = null;\n+            while (!bufsAndPromises.isEmpty()) {\n+                Object bufOrPromise = bufsAndPromises.remove();\n+                if (bufOrPromise instanceof ByteBuf) {\n+                    ReferenceCountUtil.safeRelease(bufOrPromise);\n+                } else {\n+                    ((ChannelPromise) bufOrPromise).tryFailure(cause);\n+                }\n+            }\n             size = 0;\n-            promise.tryFailure(cause);\n+            padding = 0;\n+            lifecycleManager.onException(ctx, cause);\n         }\n \n         @Override\n         public void write(int allowedBytes) {\n             int bytesWritten = 0;\n-            if (data == null || (allowedBytes == 0 && size != 0)) {\n+            if (bufsAndPromises.isEmpty() || (allowedBytes == 0 && size != 0)) {\n                 // No point writing an empty DATA frame, wait for a bigger allowance.\n                 return;\n             }\n-            try {\n-                int maxFrameSize = frameWriter().configuration().frameSizePolicy().maxFrameSize();\n-                do {\n-                    int allowedFrameSize = Math.min(maxFrameSize, allowedBytes - bytesWritten);\n-                    ByteBuf toWrite;\n-                    // Let data consume the frame before padding.\n-                    int writeableData = data.readableBytes();\n-                    if (writeableData > allowedFrameSize) {\n-                        writeableData = allowedFrameSize;\n-                        toWrite = data.readSlice(writeableData).retain();\n-                    } else {\n-                        // We're going to write the full buffer which will cause it to be released, for subsequent\n-                        // writes just use empty buffer to avoid over-releasing. Have to use an empty buffer\n-                        // as we may continue to write padding in subsequent frames.\n-                        toWrite = data;\n-                        data = Unpooled.EMPTY_BUFFER;\n-                    }\n-                    int writeablePadding = Math.min(allowedFrameSize - writeableData, padding);\n-                    padding -= writeablePadding;\n-                    bytesWritten += writeableData + writeablePadding;\n-                    ChannelPromise writePromise;\n-                    if (size == bytesWritten && !promise.isVoid()) {\n-                        // Can use the original promise if it's the last write\n-                        writePromise = promise;\n+            int maxFrameSize = frameWriter().configuration().frameSizePolicy().maxFrameSize();\n+            do {\n+                int allowedFrameSize = Math.min(maxFrameSize, allowedBytes - bytesWritten);\n+\n+                // Always need a real promise for write as we have to listen to it for failure.\n+                ChannelPromise promise = ctx.newPromise();\n+                promise.addListener(this);\n+                CompositeByteBuf toWrite = ctx.alloc().compositeBuffer(bufsAndPromises.size());\n+                while (!bufsAndPromises.isEmpty()) {\n+                    final Object entry = bufsAndPromises.peek();\n+                    if (entry instanceof ByteBuf) {\n+                        ByteBuf buf = (ByteBuf) entry;\n+                        if (toWrite.readableBytes() + buf.readableBytes() > allowedFrameSize) {\n+                            ByteBuf slice = buf.readSlice(allowedFrameSize - toWrite.readableBytes());\n+                            toWrite.addComponent(slice);\n+                            toWrite.writerIndex(toWrite.writerIndex() + slice.readableBytes());\n+                            // The remaining buffer is kept as a retained slice of the original\n+                            bufsAndPromises.remove();\n+                            bufsAndPromises.addFirst(buf.slice().retain());\n+                            break;\n+                        } else {\n+                            toWrite.addComponent(buf);\n+                            toWrite.writerIndex(toWrite.writerIndex() + buf.readableBytes());\n+                            bufsAndPromises.remove();\n+                        }\n                     } else {\n-                        // Create a new promise and listen to it for failure\n-                        writePromise = ctx.newPromise();\n-                        writePromise.addListener(this);\n-                    }\n-                    if (toWrite instanceof SlicedByteBuf && data instanceof CompositeByteBuf) {\n-                        // If we're writing a subset of a composite buffer then we want to release\n-                        // any underlying buffers that have been consumed. CompositeByteBuf only releases\n-                        // underlying buffers on write if all of its data has been consumed and its refCnt becomes\n-                        // 0.\n-                        final CompositeByteBuf toFree = (CompositeByteBuf) data;\n-                        writePromise.addListener(new ChannelFutureListener() {\n-                            @Override\n-                            public void operationComplete(ChannelFuture future) throws Exception {\n-                                toFree.discardReadComponents();\n-                            }\n-                        });\n+                        bufsAndPromises.remove();\n+                        final ChannelPromise nested = (ChannelPromise) entry;\n+                        if (!nested.isVoid()) {","path":"codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionEncoder.java","position":132,"original_position":132,"commit_id":"201c157b0d9608e0e3b0e2ddb54bccc422a5fb34","original_commit_id":"201c157b0d9608e0e3b0e2ddb54bccc422a5fb34","user":{"login":"louiscryan","id":9046588,"avatar_url":"https://avatars.githubusercontent.com/u/9046588?v=3","gravatar_id":"","url":"https://api.github.com/users/louiscryan","html_url":"https://github.com/louiscryan","followers_url":"https://api.github.com/users/louiscryan/followers","following_url":"https://api.github.com/users/louiscryan/following{/other_user}","gists_url":"https://api.github.com/users/louiscryan/gists{/gist_id}","starred_url":"https://api.github.com/users/louiscryan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/louiscryan/subscriptions","organizations_url":"https://api.github.com/users/louiscryan/orgs","repos_url":"https://api.github.com/users/louiscryan/repos","events_url":"https://api.github.com/users/louiscryan/events{/privacy}","received_events_url":"https://api.github.com/users/louiscryan/received_events","type":"User","site_admin":false},"body":"Good spot. Removed.","created_at":"2015-06-29T18:03:05Z","updated_at":"2015-06-29T18:03:05Z","html_url":"https://github.com/netty/netty/pull/3918#discussion_r33492709","pull_request_url":"https://api.github.com/repos/netty/netty/pulls/3918","_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/comments/33492709"},"html":{"href":"https://github.com/netty/netty/pull/3918#discussion_r33492709"},"pull_request":{"href":"https://api.github.com/repos/netty/netty/pulls/3918"}}},"pull_request":{"url":"https://api.github.com/repos/netty/netty/pulls/3918","id":38697525,"html_url":"https://github.com/netty/netty/pull/3918","diff_url":"https://github.com/netty/netty/pull/3918.diff","patch_url":"https://github.com/netty/netty/pull/3918.patch","issue_url":"https://api.github.com/repos/netty/netty/issues/3918","number":3918,"state":"open","locked":false,"title":"Composite byte buf in data frame coalescing","user":{"login":"louiscryan","id":9046588,"avatar_url":"https://avatars.githubusercontent.com/u/9046588?v=3","gravatar_id":"","url":"https://api.github.com/users/louiscryan","html_url":"https://github.com/louiscryan","followers_url":"https://api.github.com/users/louiscryan/followers","following_url":"https://api.github.com/users/louiscryan/following{/other_user}","gists_url":"https://api.github.com/users/louiscryan/gists{/gist_id}","starred_url":"https://api.github.com/users/louiscryan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/louiscryan/subscriptions","organizations_url":"https://api.github.com/users/louiscryan/orgs","repos_url":"https://api.github.com/users/louiscryan/repos","events_url":"https://api.github.com/users/louiscryan/events{/privacy}","received_events_url":"https://api.github.com/users/louiscryan/received_events","type":"User","site_admin":false},"body":"By releasing the read components of the CompositeByteBuffer on completion of the write future it causes any outstanding slices of the underlying composite buffer to have incorrect indices.\r\n\r\nFixes https://github.com/netty/netty/issues/3914","created_at":"2015-06-26T23:15:55Z","updated_at":"2015-06-29T18:03:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"708b352311547cd11c51d0cf1e011586a560096d","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/netty/netty/pulls/3918/commits","review_comments_url":"https://api.github.com/repos/netty/netty/pulls/3918/comments","review_comment_url":"https://api.github.com/repos/netty/netty/pulls/comments{/number}","comments_url":"https://api.github.com/repos/netty/netty/issues/3918/comments","statuses_url":"https://api.github.com/repos/netty/netty/statuses/201c157b0d9608e0e3b0e2ddb54bccc422a5fb34","head":{"label":"louiscryan:CompositeByteBufInDataFrameCoalescing","ref":"CompositeByteBufInDataFrameCoalescing","sha":"201c157b0d9608e0e3b0e2ddb54bccc422a5fb34","user":{"login":"louiscryan","id":9046588,"avatar_url":"https://avatars.githubusercontent.com/u/9046588?v=3","gravatar_id":"","url":"https://api.github.com/users/louiscryan","html_url":"https://github.com/louiscryan","followers_url":"https://api.github.com/users/louiscryan/followers","following_url":"https://api.github.com/users/louiscryan/following{/other_user}","gists_url":"https://api.github.com/users/louiscryan/gists{/gist_id}","starred_url":"https://api.github.com/users/louiscryan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/louiscryan/subscriptions","organizations_url":"https://api.github.com/users/louiscryan/orgs","repos_url":"https://api.github.com/users/louiscryan/repos","events_url":"https://api.github.com/users/louiscryan/events{/privacy}","received_events_url":"https://api.github.com/users/louiscryan/received_events","type":"User","site_admin":false},"repo":{"id":29745959,"name":"netty","full_name":"louiscryan/netty","owner":{"login":"louiscryan","id":9046588,"avatar_url":"https://avatars.githubusercontent.com/u/9046588?v=3","gravatar_id":"","url":"https://api.github.com/users/louiscryan","html_url":"https://github.com/louiscryan","followers_url":"https://api.github.com/users/louiscryan/followers","following_url":"https://api.github.com/users/louiscryan/following{/other_user}","gists_url":"https://api.github.com/users/louiscryan/gists{/gist_id}","starred_url":"https://api.github.com/users/louiscryan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/louiscryan/subscriptions","organizations_url":"https://api.github.com/users/louiscryan/orgs","repos_url":"https://api.github.com/users/louiscryan/repos","events_url":"https://api.github.com/users/louiscryan/events{/privacy}","received_events_url":"https://api.github.com/users/louiscryan/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/louiscryan/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":true,"url":"https://api.github.com/repos/louiscryan/netty","forks_url":"https://api.github.com/repos/louiscryan/netty/forks","keys_url":"https://api.github.com/repos/louiscryan/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/louiscryan/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/louiscryan/netty/teams","hooks_url":"https://api.github.com/repos/louiscryan/netty/hooks","issue_events_url":"https://api.github.com/repos/louiscryan/netty/issues/events{/number}","events_url":"https://api.github.com/repos/louiscryan/netty/events","assignees_url":"https://api.github.com/repos/louiscryan/netty/assignees{/user}","branches_url":"https://api.github.com/repos/louiscryan/netty/branches{/branch}","tags_url":"https://api.github.com/repos/louiscryan/netty/tags","blobs_url":"https://api.github.com/repos/louiscryan/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/louiscryan/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/louiscryan/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/louiscryan/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/louiscryan/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/louiscryan/netty/languages","stargazers_url":"https://api.github.com/repos/louiscryan/netty/stargazers","contributors_url":"https://api.github.com/repos/louiscryan/netty/contributors","subscribers_url":"https://api.github.com/repos/louiscryan/netty/subscribers","subscription_url":"https://api.github.com/repos/louiscryan/netty/subscription","commits_url":"https://api.github.com/repos/louiscryan/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/louiscryan/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/louiscryan/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/louiscryan/netty/issues/comments{/number}","contents_url":"https://api.github.com/repos/louiscryan/netty/contents/{+path}","compare_url":"https://api.github.com/repos/louiscryan/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/louiscryan/netty/merges","archive_url":"https://api.github.com/repos/louiscryan/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/louiscryan/netty/downloads","issues_url":"https://api.github.com/repos/louiscryan/netty/issues{/number}","pulls_url":"https://api.github.com/repos/louiscryan/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/louiscryan/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/louiscryan/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/louiscryan/netty/labels{/name}","releases_url":"https://api.github.com/repos/louiscryan/netty/releases{/id}","created_at":"2015-01-23T18:13:14Z","updated_at":"2015-05-04T20:10:55Z","pushed_at":"2015-06-26T23:23:18Z","git_url":"git://github.com/louiscryan/netty.git","ssh_url":"git@github.com:louiscryan/netty.git","clone_url":"https://github.com/louiscryan/netty.git","svn_url":"https://github.com/louiscryan/netty","homepage":"http://netty.io/","size":42217,"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":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"netty:master","ref":"master","sha":"b5337abe2aee8e20153900fb7024f989555cf142","user":{"login":"netty","id":473791,"avatar_url":"https://avatars.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"repo":{"id":1064563,"name":"netty","full_name":"netty/netty","owner":{"login":"netty","id":473791,"avatar_url":"https://avatars.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/netty/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":false,"url":"https://api.github.com/repos/netty/netty","forks_url":"https://api.github.com/repos/netty/netty/forks","keys_url":"https://api.github.com/repos/netty/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/netty/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/netty/netty/teams","hooks_url":"https://api.github.com/repos/netty/netty/hooks","issue_events_url":"https://api.github.com/repos/netty/netty/issues/events{/number}","events_url":"https://api.github.com/repos/netty/netty/events","assignees_url":"https://api.github.com/repos/netty/netty/assignees{/user}","branches_url":"https://api.github.com/repos/netty/netty/branches{/branch}","tags_url":"https://api.github.com/repos/netty/netty/tags","blobs_url":"https://api.github.com/repos/netty/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/netty/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/netty/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/netty/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/netty/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/netty/netty/languages","stargazers_url":"https://api.github.com/repos/netty/netty/stargazers","contributors_url":"https://api.github.com/repos/netty/netty/contributors","subscribers_url":"https://api.github.com/repos/netty/netty/subscribers","subscription_url":"https://api.github.com/repos/netty/netty/subscription","commits_url":"https://api.github.com/repos/netty/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/netty/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/netty/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/netty/netty/issues/comments{/number}","contents_url":"https://api.github.com/repos/netty/netty/contents/{+path}","compare_url":"https://api.github.com/repos/netty/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/netty/netty/merges","archive_url":"https://api.github.com/repos/netty/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/netty/netty/downloads","issues_url":"https://api.github.com/repos/netty/netty/issues{/number}","pulls_url":"https://api.github.com/repos/netty/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/netty/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/netty/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/netty/netty/labels{/name}","releases_url":"https://api.github.com/repos/netty/netty/releases{/id}","created_at":"2010-11-09T09:22:21Z","updated_at":"2015-06-29T17:00:45Z","pushed_at":"2015-06-29T14:24:39Z","git_url":"git://github.com/netty/netty.git","ssh_url":"git@github.com:netty/netty.git","clone_url":"https://github.com/netty/netty.git","svn_url":"https://github.com/netty/netty","homepage":"http://netty.io/","size":129191,"stargazers_count":4840,"watchers_count":4840,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2403,"mirror_url":null,"open_issues_count":289,"forks":2403,"open_issues":289,"watchers":4840,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/3918"},"html":{"href":"https://github.com/netty/netty/pull/3918"},"issue":{"href":"https://api.github.com/repos/netty/netty/issues/3918"},"comments":{"href":"https://api.github.com/repos/netty/netty/issues/3918/comments"},"review_comments":{"href":"https://api.github.com/repos/netty/netty/pulls/3918/comments"},"review_comment":{"href":"https://api.github.com/repos/netty/netty/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/netty/netty/pulls/3918/commits"},"statuses":{"href":"https://api.github.com/repos/netty/netty/statuses/201c157b0d9608e0e3b0e2ddb54bccc422a5fb34"}}}}