Skip to content

Commit d2f2e35

Browse files
committed
TS-45056 Rework
1 parent c0d9835 commit d2f2e35

2 files changed

Lines changed: 14 additions & 15 deletions

File tree

agent/src/main/java/com/teamscale/jacoco/agent/PreMain.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class PreMain {
5454
* System property that we use to prevent this agent from being attached to the same VM twice. This can happen if
5555
* the agent is registered via multiple JVM environment variables and/or the command line at the same time.
5656
*/
57-
private static final String LOCKING_SYSTEM_PROPERTY = "TEAMSCALE_JAVA_PROFILER_ATTACHED";
57+
static final String LOCKING_SYSTEM_PROPERTY = "TEAMSCALE_JAVA_PROFILER_ATTACHED";
5858

5959
/**
6060
* Environment variable from which to read the config ID to use. This is an ID for a profiler configuration that is
@@ -79,8 +79,6 @@ public static void premain(String options, Instrumentation instrumentation) {
7979
try {
8080
startProfiler(options, instrumentation);
8181
} catch (Throwable t) {
82-
// Top-level safety net: an exception escaping premain would be re-thrown by the JVM as
83-
// IllegalAgentException and abort the profiled application. See class-level Javadoc.
8482
logStartupFailure(t);
8583
}
8684
}
@@ -125,8 +123,7 @@ private static void startProfiler(String options, Instrumentation instrumentatio
125123
agentOptions.configurationViaTeamscale.unregisterProfiler();
126124
}
127125

128-
// Previously rethrown. We now swallow the exception so a misconfigured agent cannot abort the profiled
129-
// application. The error has been logged above.
126+
// Swallow to avoid aborting startup on misconfiguration (see class Javadoc).
130127
return;
131128
} catch (AgentOptionReceiveException e) {
132129
// When Teamscale is not available, we don't want to fail hard to still allow for testing even if no
@@ -158,16 +155,9 @@ private static void logStartupFailure(Throwable t) {
158155
"Teamscale Java Profiler failed to start up. The profiled application will continue to run "
159156
+ "without coverage collection.", t);
160157
} catch (Throwable loggingFailure) {
161-
// The logger may not be initialized yet or may itself be broken. Fall back to stderr so the failure is
162-
// at least visible somewhere, then give up silently.
163-
try {
164-
System.err.println(
165-
"[Teamscale Java Profiler] Failed to start up and will not collect coverage: " + t);
166-
loggingFailure.addSuppressed(t);
167-
loggingFailure.printStackTrace();
168-
} catch (Throwable ignored) {
169-
// Nothing we can do; the alternative is to crash the profiled application, which we must not do.
170-
}
158+
// Logger may not be initialized yet or may itself be broken. Fall back to stderr.
159+
System.err.println("[Teamscale Java Profiler] Failed to start up and will not collect coverage: " + t);
160+
System.err.println("[Teamscale Java Profiler] Logging itself failed: " + loggingFailure);
171161
}
172162
}
173163

agent/src/test/java/com/teamscale/jacoco/agent/PreMainTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.teamscale.jacoco.agent;
22

3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.BeforeEach;
35
import org.junit.jupiter.api.Test;
46

57
import java.lang.instrument.Instrumentation;
@@ -14,6 +16,13 @@
1416
*/
1517
class PreMainTest {
1618

19+
@BeforeEach
20+
@AfterEach
21+
void clearLockingProperty() {
22+
// premain sets this property to prevent double-attach. Clear it so each test runs against a fresh state.
23+
System.clearProperty(PreMain.LOCKING_SYSTEM_PROPERTY);
24+
}
25+
1726
/**
1827
* Invalid agent options produce an {@code AgentOptionParseException} internally. The agent must swallow it so the
1928
* profiled application keeps running.

0 commit comments

Comments
 (0)