Skip to content

ensure the XContentBuilder is always closed in RestBuilderListener #21124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 1, 2016

Conversation

jaymode
Copy link
Member

@jaymode jaymode commented Oct 26, 2016

There may be cases where the XContentBuilder is not used and therefore it never gets closed, which can cause a leak of bytes. This change moves the creation of the builder into a try with resources block.

There may be cases where the XContentBuilder is not used and therefore it never gets
closed, which can cause a leak of bytes. This change moves the creation of the builder
into a try with resources block.
@dadoonet
Copy link
Member

I think this should go as well in 5.0 branch, no?

@bleskes
Copy link
Contributor

bleskes commented Oct 26, 2016

I'm not sure we want to automatically close. The current api is set up so that we expect to use the builder we give them (there is no way to not have a builder) - if they don't it's probably a bug we're hiding (or we should give an official way to not have a builder created at all). I think we should add an assertion that verifies the builder is closed when buildResponse returns

@s1monw
Copy link
Contributor

s1monw commented Oct 26, 2016

@bleskes maybe both, assertion and auto close? I mean some plugins might not test this (3rd party ) and then we are leaking bytes since assertions are off?

@bleskes
Copy link
Contributor

bleskes commented Oct 26, 2016

@s1monw yeah , that's what I meant ++ . Keep code as is but make sure builder is closed when buildResponse returns

@jaymode jaymode added the v5.0.1 label Oct 26, 2016
@jaymode
Copy link
Member Author

jaymode commented Oct 26, 2016

I think this should go as well in 5.0 branch, no?

I think that makes sense, I added the 5.0.1 label.

maybe both, assertion and auto close?

I pushed 5a17d14, which adds the assertion while keeping auto close.

@jaymode jaymode added the review label Oct 26, 2016
Copy link
Contributor

@s1monw s1monw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left two comments

return buildResponse(response, builder);
try (XContentBuilder builder = channel.newBuilder()) {
final RestResponse restResponse = buildResponse(response, builder);
assert assertionsEnabled == false || builder.isClosed() : "callers should ensure the XContentBuilder is closed themselves";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a protected method assert assertBuilderClosed(builder) we can override in tests instead. this is very confusing and I hate static mutable vars

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ that's a much better approach

public boolean isClosed() {
return generator.isClosed();
}

public XContentGenerator generator() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be enough to only add isClosed to XContentGenerator? and maybe add a javadoc?


// pkg private method that we can override for testing
boolean assertBuilderClosed(XContentBuilder xContentBuilder) {
return xContentBuilder.generator().isClosed();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually do this:

assert  xContentBuilder.generator().isClosed();
return true;

@s1monw
Copy link
Contributor

s1monw commented Oct 26, 2016

left a minor LGTM otherwise

@s1monw
Copy link
Contributor

s1monw commented Nov 1, 2016

@jaymode can we get this in please?

@jaymode jaymode merged commit 6e7e891 into elastic:master Nov 1, 2016
@jaymode jaymode deleted the release_bytes_master branch November 1, 2016 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants