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

8268361: Fix the infinite loop in next_line #4378

Closed
wants to merge 3 commits into from
Closed

8268361: Fix the infinite loop in next_line #4378

wants to merge 3 commits into from

Conversation

UncleNine
Copy link
Contributor

@UncleNine UncleNine commented Jun 6, 2021

If the /proc/stat mount point is changed in container environment, the while loop may lead to 100% cpu usage.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/4378/head:pull/4378
$ git checkout pull/4378

Update a local copy of the PR:
$ git checkout pull/4378
$ git pull https://git.openjdk.java.net/jdk pull/4378/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 4378

View PR using the GUI difftool:
$ git pr show -t 4378

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/4378.diff

… in container environment, the while loop may lead to 100% cpu usage.
@bridgekeeper
Copy link

bridgekeeper bot commented Jun 6, 2021

👋 Welcome back UncleNine! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Jun 6, 2021

@UncleNine The following labels will be automatically applied to this pull request:

  • jmx
  • serviceability

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added serviceability serviceability-dev@openjdk.org jmx jmx-dev@openjdk.org labels Jun 6, 2021
@y1yang0
Copy link
Member

y1yang0 commented Jun 8, 2021

I have encountered a similar problem before, I've filed a new issue https://bugs.openjdk.java.net/browse/JDK-8268361 for this. You can change your title to "8268361: Fix the infinite loop in next_line", then OpenJDK bot would associate your PR with the corresponding JBS issue.

Best regards,
Yang

@UncleNine UncleNine changed the title Fix the dead loop 8268361: Fix the infinite loop in next_line Jun 8, 2021
@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 8, 2021
@mlbridge
Copy link

mlbridge bot commented Jun 8, 2021

Webrevs

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

Hi,

This fixes the potential infinite loop, but is it sufficient?

Thanks,
David

int c;
do {
c = fgetc(f);
} while(c != '\n' && c != EOF);
Copy link
Member

Choose a reason for hiding this comment

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

Style nit: please add space before (

Copy link
Member

Choose a reason for hiding this comment

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

It is not obvious to me that the caller of next_line will handle the fact that we have hit EOF?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In my case, it happened in the container environment.
the /proc filesystem of the container is provided by lxcfs, but a lxcfs bug may make the /proc/stat mount point change, then the file descriptor is different and fgetc function returns an EOF on error, But c != '\n' is true and it leads to the infinit loop.
Below are our flamegraph in the production , it happends on serveral frameworks(micrometer, elasticsearch..)which use the api "sun/management/OperatingSystemImpl.getSystemCpuLoad"
107140246-bb490f80-695b-11eb-9f62-0116079f4f4a

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I use top -Hp $PID command and the thread show 100% cpu usage
top
jtack

Copy link
Contributor

Choose a reason for hiding this comment

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

@UncleNine We should handle the case in the get_totalticks() function - which seems to be the only user of next_line() - when next_line() returns EOF as David said. One way would be to return the 'c' character read in next_line and if it's EOF, return -2 in get_totalticks().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

all right, i have updated the code

@jerboaa
Copy link
Contributor

jerboaa commented Jun 8, 2021

If the /proc/stat mount point is changed in container environment, the while loop may lead to 100% cpu usage.

In what way have you observed /proc/stat being changed which manifests in 100% cpu usage?

@UncleNine
Copy link
Contributor Author

If the /proc/stat mount point is changed in container environment, the while loop may lead to 100% cpu usage.

In what way have you observed /proc/stat being changed which manifests in 100% cpu usage?

In my case, it happened in the container environment.
the /proc filesystem of the container is provided by lxcfs, but a lxcfs bug may make the /proc/stat mount point change.
It was caused by a lxcfs-3.1.2 segment fault's exit

@openjdk openjdk bot removed the rfr Pull request is ready for review label Jun 9, 2021
Copy link
Contributor

@jerboaa jerboaa left a comment

Choose a reason for hiding this comment

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

Looks fine to me. Please fix the jcheck (whitespace) issue.

Copy link
Contributor

@sspitsyn sspitsyn left a comment

Choose a reason for hiding this comment

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

It looks okay to me.
Thanks,
Serguei

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

This seems better to me now.

Thanks,
David

@openjdk
Copy link

openjdk bot commented Jun 10, 2021

@UncleNine This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8268361: Fix the infinite loop in next_line

Reviewed-by: dholmes, sgehwolf, sspitsyn

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 113 new commits pushed to the master branch:

  • b018c45: 8267630: Start of release updates for JDK 18
  • 7400789: 8268542: serviceability/logging/TestFullNames.java tests only 1st test case
  • a95e64c: 8268443: ParallelGC Full GC should use parallel WeakProcessor
  • 0a6c7d8: 8267424: CTW: C1 fails with "State must not be null"
  • e11f70a: 8003216: Add JFR event indicating explicit System.gc() call
  • 2b41459: 8267073: Race between Card Redirtying and Freeing Collection Set regions results in missing remembered set entries with G1
  • 7cd5a6e: 8268537: (Temporary) Disable ParallelRefProcEnabled for Parallel GC
  • f716711: 8265271: JFR: Allow use of .jfc options when starting JFR
  • f677163: 8266130: convert Thread-SMR stress tests from counter based to time based
  • 6c552a7: 8268544: some runtime/sealedClasses tests should be run in driver mode
  • ... and 103 more: https://git.openjdk.java.net/jdk/compare/52d88ee1d1e0f6b9927db03a2b0bff75e4be03a2...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@dholmes-ora, @jerboaa, @sspitsyn) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jun 10, 2021
Copy link
Contributor

@jerboaa jerboaa left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for your patience!

@UncleNine
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Jun 10, 2021
@openjdk
Copy link

openjdk bot commented Jun 10, 2021

@UncleNine
Your change (at version f568406) is now ready to be sponsored by a Committer.

@jerboaa
Copy link
Contributor

jerboaa commented Jun 10, 2021

/sponsor

@openjdk
Copy link

openjdk bot commented Jun 10, 2021

Going to push as commit 7267227.
Since your change was applied there have been 113 commits pushed to the master branch:

  • b018c45: 8267630: Start of release updates for JDK 18
  • 7400789: 8268542: serviceability/logging/TestFullNames.java tests only 1st test case
  • a95e64c: 8268443: ParallelGC Full GC should use parallel WeakProcessor
  • 0a6c7d8: 8267424: CTW: C1 fails with "State must not be null"
  • e11f70a: 8003216: Add JFR event indicating explicit System.gc() call
  • 2b41459: 8267073: Race between Card Redirtying and Freeing Collection Set regions results in missing remembered set entries with G1
  • 7cd5a6e: 8268537: (Temporary) Disable ParallelRefProcEnabled for Parallel GC
  • f716711: 8265271: JFR: Allow use of .jfc options when starting JFR
  • f677163: 8266130: convert Thread-SMR stress tests from counter based to time based
  • 6c552a7: 8268544: some runtime/sealedClasses tests should be run in driver mode
  • ... and 103 more: https://git.openjdk.java.net/jdk/compare/52d88ee1d1e0f6b9927db03a2b0bff75e4be03a2...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot closed this Jun 10, 2021
@openjdk openjdk bot added the integrated Pull request has been integrated label Jun 10, 2021
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Jun 10, 2021
@openjdk
Copy link

openjdk bot commented Jun 10, 2021

@jerboaa @UncleNine Pushed as commit 7267227.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@UncleNine UncleNine deleted the myjdk branch June 11, 2021 01:31
@dholmes-ora
Copy link
Member

@UncleNine please do not force push commits to an open PR as it makes it difficult for reviewers to track the changes. The PR can contain as many commits as you like as it will all be squashed to a single clean commit when integrating. Thanks.

@tstuefe
Copy link
Member

tstuefe commented Jun 11, 2021

Would it not be better to read the whole content of /proc/stat with a single read() call instead of line by line? I don't think proc fs guarantees any kind of consistency with separate reads.

@UncleNine
Copy link
Contributor Author

@UncleNine please do not force push commits to an open PR as it makes it difficult for reviewers to track the changes. The PR can contain as many commits as you like as it will all be squashed to a single clean commit when integrating. Thanks.

Sorry,

1 similar comment
@UncleNine
Copy link
Contributor Author

@UncleNine please do not force push commits to an open PR as it makes it difficult for reviewers to track the changes. The PR can contain as many commits as you like as it will all be squashed to a single clean commit when integrating. Thanks.

Sorry,

@UncleNine
Copy link
Contributor Author

Would it not be better to read the whole content of /proc/stat with a single read() call instead of line by line? I don't think proc fs guarantees any kind of consistency with separate reads.

yes, so fgetc's return value should need to be checked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated jmx jmx-dev@openjdk.org serviceability serviceability-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

6 participants