GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix asprof incorrectly existing with non zero exit code by Baraa-Hasheesh · Pull Request #1614 · async-profiler/async-profiler

Conversation

@Baraa-Hasheesh

@Baraa-Hasheesh Baraa-Hasheesh commented Dec 3, 2025

Copy link
Copy Markdown
Contributor

Description

In collect mode, the asprof will do the following:

  • run jattach to start the profiler
  • sleep/wait until the full profiling duration
  • run jattach to stop the profiler

During some testing, I noticed that it's possible for the Java process to exit during the call of the last sleep call that the asprof does to wait for session time to be exhausted

If that happens the follow up jattach call will fail, as the process is already dead,

This change doesn't not fully solve the issue as the race condition still exists but will make much less likely to happen as it checks the process is still alive before calling jattach to stop the profiler

Related issues

N/A

Motivation and context

Fix exist code for the asprof in case Java process exiting before stop is called

How has this been tested?

manual testing


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Baraa-Hasheesh added 2 commits December 3, 2025 12:07
@Baraa-Hasheesh
Check that process is still alive before running jattach to stop the …
62a9891
…profiler
@Baraa-Hasheesh
print tmp file if used
283c756
@Baraa-Hasheesh Baraa-Hasheesh changed the title Fix jattach incorrectly existing with non zero exit code Fix asprof incorrectly existing with non zero exit code Dec 3, 2025
fandreuz
fandreuz reviewed Dec 3, 2025
View reviewed changes
Comment thread src/main/main.cpp Outdated
// It's possible for java process to die during the last `sleep` call in the waiting loop
// Running jattach in that case will cause the asprof to incorrectly conclude with non 0 exit code
if (kill(pid, 0) == 0) {
run_jattach(pid, String("stop,file=") << file << "," << output << format << ",log=" << logfile);

@fandreuz fandreuz Dec 3, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Technically the race condition is still there. Can we do one of the following?

  • Fix the problem in jattach
  • Be resilient to this situation in Async-Profiler, without relying on the output of kill(pid, 0) at some arbitrary point in time.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Baraa-Hasheesh Baraa-Hasheesh Dec 3, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'm aware that race conditions still exists

This change doesn't not fully solve the issue as the race condition still exists but will make much less likely to happen as it checks the process is still alive before calling jattach to stop the profiler

Now regarding the suggestions

Fix the problem in jattach

That would need to be done on the correct package here which is out of scope for this change here

And strictly speaking jattach is behaving correctly, so changes to avoid this won't exactly look the best & I would advice against it, but I would ask @apangin about his opinion

Be resilient to this situation in Async-Profiler, without relying on the output of kill(pid, 0) at some arbitrary point in time.

I can't really see how to do that without hard coding checks for acceptable error messages that may be reported back by the jattach command, which won't look the best & overriding the exit code for those cases

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

apangin reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@apangin apangin Dec 3, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

To keep the termination check logic in one place, will it be possible to re-shape the while loop above? E.g., what about just swapping sleep(1) with if (kill(pid, 0) != 0)?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Baraa-Hasheesh Baraa-Hasheesh Dec 4, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@apangin that should achieve the same goal of making the race condition less likely, 35478e2

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Baraa-Hasheesh Baraa-Hasheesh Dec 4, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Also I created a PR here for other race conditions on jattach

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Baraa-Hasheesh
flip wait & kill
35478e2
@Baraa-Hasheesh Baraa-Hasheesh mentioned this pull request Dec 5, 2025
Avoid jattach waiting for an already dead process jattach/jattach#86
Merged
@apangin
apangin merged commit 5bf0e31 into async-profiler:master Dec 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@apangin apangin apangin left review comments
+1 more reviewer
@fandreuz fandreuz fandreuz left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@Baraa-Hasheesh @apangin @fandreuz
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.