Skip to content
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

Prevent memory leak if HTTP/1.1 encoding fails #1610

Merged
merged 2 commits into from
Jun 8, 2021

Conversation

idelpivnitskiy
Copy link
Member

Motivation:

When HttpObjectEncoder encodes meta-data or trailers an error may
happen, which will cause a leak of internal ByteBuf. It may happen if
any of the encoding object throw or if it tries to expand the size of
that internal ByteBuf without success.

Modifications:

  • try-catch meta-data and trailers encoding and release the tmp
    ByteBuf in case of any failures;

Result:

Internal ByteBuf does not leak in case of an error in
HttpObjectEncoder.

Motivation:

When `HttpObjectEncoder` encodes meta-data or trailers an error may
happen, which will cause a leak of internal `ByteBuf`. It may happen if
any of the encoding object throw or if it tries to expand the size of
that internal `ByteBuf` without success.

Modifications:

- try-catch meta-data and trailers encoding and release the tmp
`ByteBuf` in case of any failures;

Result:

Internal `ByteBuf` does not leak in case of an error in
`HttpObjectEncoder`.
try {
buf.writeCharSequence(lengthHex, US_ASCII);
writeShortBE(buf, CRLF_SHORT);
} catch (Exception e) {
Copy link
Member Author

@idelpivnitskiy idelpivnitskiy Jun 4, 2021

Choose a reason for hiding this comment

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

This code is unlikely to fail. Added try-catch here for consistency, more safety, and better sleep :)

Copy link
Collaborator

@NiteshKant NiteshKant left a comment

Choose a reason for hiding this comment

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

One question but LGTM

@idelpivnitskiy idelpivnitskiy merged commit fbcfc7f into apple:main Jun 8, 2021
@idelpivnitskiy idelpivnitskiy deleted the prevent-leak branch June 8, 2021 04:41
bondolo pushed a commit to bondolo/servicetalk that referenced this pull request Jun 8, 2021
Motivation:

When `HttpObjectEncoder` encodes meta-data or trailers an error may
happen, which will cause a leak of internal `ByteBuf`. It may happen if
any of the encoding object throw or if it tries to expand the size of
that internal `ByteBuf` without success.

Modifications:

- try-catch meta-data and trailers encoding and release the tmp
`ByteBuf` in case of any failures;

Result:

Internal `ByteBuf` does not leak in case of an error in
`HttpObjectEncoder`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants