Skip to content

Conversation

Copy link

Copilot AI commented Oct 30, 2025

UUID.randomUUID() was called during field initialization of AgentConfiguration, causing ANRs on app startup. The method uses SecureRandom internally, which can block for several seconds on some Android devices, particularly when invoked during static initialization.

Changes

  • Deferred initialization: Changed sessionID field from eager (provideSessionId() call) to lazy (null) initialization
  • Thread-safe lazy loading: Implemented double-checked locking in getSessionID() to initialize on first access
  • Synchronized regeneration: Made provideSessionId() synchronized to prevent race conditions during explicit session ID renewal

Before

private String sessionID = provideSessionId();  // Blocks during class init

public String getSessionID() {
    return sessionID;
}

After

private String sessionID = null;  // No blocking during class init

public String getSessionID() {
    if (sessionID == null) {
        synchronized (this) {
            if (sessionID == null) {
                sessionID = UUID.randomUUID().toString();
            }
        }
    }
    return sessionID;
}

public synchronized String provideSessionId() {
    sessionID = UUID.randomUUID().toString();
    return sessionID;
}

The blocking UUID generation now occurs only when the session ID is actually needed, avoiding ANRs during app startup while maintaining thread safety and backward compatibility.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • dl.google.com
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java -XX:+HeapDumpOnOutOfMemoryError --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED -Xms1024m -Xmx8192m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant -cp /home/REDACTED/.gradle/wrapper/dists/gradle-7.6-bin/9l9tetv7ltxvx3i8an4pb86ye/gradle-7.6/lib/gradle-launcher-7.6.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 7.6 (dns block)
    • Triggering command: /usr/lib/jvm/temurin-11-jdk-amd64/bin/java -XX:+HeapDumpOnOutOfMemoryError --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED -Xms1024m -Xmx8192m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant -cp /home/REDACTED/.gradle/wrapper/dists/gradle-7.6-bin/9l9tetv7ltxvx3i8an4pb86ye/gradle-7.6/lib/gradle-launcher-7.6.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 7.6 (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>com.newrelic.agent.android.AgentConfiguration.provideSessionId (AgentConfiguration.java:225)</issue_title>
<issue_description>NOTE: # ( ^^ ^^ )

Description

App faces ANR in some cases.

I have dug into the issue, it appears on this method:

public String provideSessionId() {
sessionID = UUID.randomUUID().toString(); --this line specifically
return sessionID;
}

Basically, this internally calls:
SecureRandom secureRandom = new SecureRandom();

This is cryptographically secure random number generation, which on some Android devices and OS versions can block for several seconds during initialization, especially if called very early during app startup (like in a ContentProvider static initializer). (As suggested by AI agent)

Steps to Reproduce

Occurs randomly during app startup, detected via firebase.

Expected Behavior

App should not throw ANR.

Relevant Logs / Console output

com.newrelic.agent.android.AgentConfiguration.provideSessionId (AgentConfiguration.java:225)
com.newrelic.agent.android.AgentConfiguration. (AgentConfiguration.java:63)
com.newrelic.agent.android.AgentConfiguration. (AgentConfiguration.java:54)
com.newrelic.agent.android.rum.AppApplicationLifeCycle. (AppApplicationLifeCycle.java:38)
com.newrelic.agent.android.rum.contentprovider.NewRelicAppContentProvider. (NewRelicAppContentProvider.java:20)
java.lang.Class.newInstance (Native method)
android.app.AppComponentFactory.instantiateProvider (AppComponentFactory.java:147)
androidx.core.app.CoreComponentFactory.instantiateProvider (CoreComponentFactory.java:66)
android.app.ActivityThread.installProvider (ActivityThread.java:7909)
android.app.ActivityThread.installContentProviders (ActivityThread.java:7430)
android.app.ActivityThread.handleBindApplication (ActivityThread.java:7124)
android.app.ActivityThread.-$$Nest$mhandleBindApplication (unavailable)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:2328)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loopOnce (Looper.java:205)
android.os.Looper.loop (Looper.java:294)
android.app.ActivityThread.main (ActivityThread.java:8394)
java.lang.reflect.Method.invoke (Native method)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:640)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:995)

Your Environment

Android 14
Brand:Oppo
Model:Realme C51
TIP: # ( Include as many relevant details about your environment as possible. )

  • ex: Browser name and version:
  • ex: Operating System and version:

Additional context

</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix ANR in provideSessionId method due to secure random initialization Fix ANR caused by blocking SecureRandom initialization in AgentConfiguration Oct 30, 2025
Copilot finished work on behalf of ndesai-newrelic October 30, 2025 14:06
Copilot AI requested a review from ndesai-newrelic October 30, 2025 14:06
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