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 the runtime attachment from launching multiple times #409

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public final class CoreRuntimeAttach {

private final String agentJarResourceName;

private boolean runtimeAttachmentRequested;

/**
* Creates a new {@code DistroRuntimeAttach} from the resource name of the agent jar.
*
Expand All @@ -37,6 +39,11 @@ public void attachJavaagentToCurrentJVM() {
return;
}

if (runtimeAttachmentRequested) {
return;
}
runtimeAttachmentRequested = true;
Comment on lines +42 to +45
Copy link
Member

Choose a reason for hiding this comment

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

I'm just wondering, what kind of situations are we trying to prevent here? A simple duplicated line of code? Or runtime attach being called from completely different places in the application? Would it make sense to print out a warning with a stack trace to help pinpoint the unnecessary invocations?

Copy link
Member Author

Choose a reason for hiding this comment

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

This PR aims to prevent the runtime attachment from being invoked several times from the main method. If a runtime attachment is invoked while another runtime attachment is in progress and not finished, I am not sure about the result with JVM attachment mechanism. The instrumentation might not be correctly installed.

Would it make sense to print out a warning with a stack trace to help pinpoint the unnecessary invocations?

Nothing is displayed to be consistent with the current display policy. First, this type of message was reported with a debug log. After, the warning log messages were replaced with System.err.println and the debug messages removed. Perhaps it may be worth rediscussing this display policy.

Copy link
Member

Choose a reason for hiding this comment

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

If a runtime attachment is invoked while another runtime attachment is in progress and not finished

Again, is that a valid scenario? Don't we already have a check that verifies that only the main thread can call this method?
Otherwise, if a concurrent scenario is possible, then this code needs to be thread safe (use AtomicBoolean instead).

Copy link
Member Author

Choose a reason for hiding this comment

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

Don't we already have a check that verifies that only the main thread can call this method?
Otherwise, if a concurrent scenario is possible, then this code needs to be thread safe (use AtomicBoolean instead).

The scenario is about two runtime attachments requested from the main thread since the new check is after the main thread check.

Again, is that a valid scenario?

I have not today enough knowledge about the Attach Listener JVM thread implementation. I would prefer to stop the things ealier because it is not the kind of issue I would like to debug. We can close this PR on OTel if you prefer.

Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to stop the things ealier because it is not the kind of issue I would like to debug.

👍

We can close this PR on OTel if you prefer.

No, I approve of this PR, I think this kind of check definitely makes sense. I was wondering whether this was motivated by a real use case, and if so, what kind of issue it was.


AgentFileProvider agentFileProvider = new AgentFileProvider(agentJarResourceName);

File javaagentFile = agentFileProvider.getAgentFile();
Expand Down