Skip to content

Commit c6e6bbc

Browse files
Check for mixed SDK versions (#4270)
* Check for mixed SDK versions * Format code * format + api --------- Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
1 parent 1090788 commit c6e6bbc

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

sentry/api/sentry.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2867,6 +2867,7 @@ public final class io/sentry/SentryInstantDateProvider : io/sentry/SentryDatePro
28672867
public final class io/sentry/SentryIntegrationPackageStorage {
28682868
public fun addIntegration (Ljava/lang/String;)V
28692869
public fun addPackage (Ljava/lang/String;Ljava/lang/String;)V
2870+
public fun checkForMixedVersions (Lio/sentry/ILogger;)Z
28702871
public fun clearStorage ()V
28712872
public static fun getInstance ()Lio/sentry/SentryIntegrationPackageStorage;
28722873
public fun getIntegrations ()Ljava/util/Set;

sentry/src/main/java/io/sentry/SentryClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,9 @@ public void captureSession(final @NotNull Session session, final @Nullable Hint
767767
.log(SentryLevel.ERROR, "The BeforeEnvelope callback threw an exception.", e);
768768
}
769769
}
770+
771+
SentryIntegrationPackageStorage.getInstance().checkForMixedVersions(options.getLogger());
772+
770773
if (hint == null) {
771774
transport.send(envelope);
772775
} else {

sentry/src/main/java/io/sentry/SentryIntegrationPackageStorage.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public final class SentryIntegrationPackageStorage {
1515
private static volatile @Nullable SentryIntegrationPackageStorage INSTANCE;
1616
private static final @NotNull AutoClosableReentrantLock staticLock =
1717
new AutoClosableReentrantLock();
18+
private static volatile @Nullable Boolean mixedVersionsDetected = null;
19+
private static final @NotNull AutoClosableReentrantLock mixedVersionsLock =
20+
new AutoClosableReentrantLock();
1821

1922
public static @NotNull SentryIntegrationPackageStorage getInstance() {
2023
if (INSTANCE == null) {
@@ -64,12 +67,48 @@ public void addPackage(final @NotNull String name, final @NotNull String version
6467

6568
SentryPackage newPackage = new SentryPackage(name, version);
6669
packages.add(newPackage);
70+
try (final @NotNull ISentryLifecycleToken ignored = mixedVersionsLock.acquire()) {
71+
mixedVersionsDetected = null;
72+
}
6773
}
6874

6975
public @NotNull Set<SentryPackage> getPackages() {
7076
return packages;
7177
}
7278

79+
public boolean checkForMixedVersions(final @NotNull ILogger logger) {
80+
final @Nullable Boolean mixedVersionsDetectedBefore = mixedVersionsDetected;
81+
if (mixedVersionsDetectedBefore != null) {
82+
return mixedVersionsDetectedBefore;
83+
}
84+
try (final @NotNull ISentryLifecycleToken ignored = mixedVersionsLock.acquire()) {
85+
final @NotNull String sdkVersion = BuildConfig.VERSION_NAME;
86+
boolean mixedVersionsDetectedThisCheck = false;
87+
88+
for (SentryPackage pkg : packages) {
89+
if (pkg.getName().startsWith("maven:io.sentry:")
90+
&& !sdkVersion.equalsIgnoreCase(pkg.getVersion())) {
91+
logger.log(
92+
SentryLevel.ERROR,
93+
"The Sentry SDK has been configured with mixed versions. Expected %s to match core SDK version %s but was %s",
94+
pkg.getName(),
95+
sdkVersion,
96+
pkg.getVersion());
97+
mixedVersionsDetectedThisCheck = true;
98+
}
99+
}
100+
101+
if (mixedVersionsDetectedThisCheck) {
102+
logger.log(SentryLevel.ERROR, "^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
103+
logger.log(SentryLevel.ERROR, "^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
104+
logger.log(SentryLevel.ERROR, "^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
105+
logger.log(SentryLevel.ERROR, "^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
106+
}
107+
mixedVersionsDetected = mixedVersionsDetectedThisCheck;
108+
return mixedVersionsDetectedThisCheck;
109+
}
110+
}
111+
73112
@TestOnly
74113
public void clearStorage() {
75114
integrations.clear();

0 commit comments

Comments
 (0)