Skip to content

Commit bd0c3b4

Browse files
committed
Check for mixed SDK versions
1 parent 64d302b commit bd0c3b4

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.sentry.util.Objects;
66
import java.util.Set;
77
import java.util.concurrent.CopyOnWriteArraySet;
8+
89
import org.jetbrains.annotations.ApiStatus;
910
import org.jetbrains.annotations.NotNull;
1011
import org.jetbrains.annotations.Nullable;
@@ -15,6 +16,8 @@ public final class SentryIntegrationPackageStorage {
1516
private static volatile @Nullable SentryIntegrationPackageStorage INSTANCE;
1617
private static final @NotNull AutoClosableReentrantLock staticLock =
1718
new AutoClosableReentrantLock();
19+
private static volatile @Nullable Boolean mixedVersionsDetected = null;
20+
private static final @NotNull AutoClosableReentrantLock mixedVersionsLock = new AutoClosableReentrantLock();
1821

1922
public static @NotNull SentryIntegrationPackageStorage getInstance() {
2023
if (INSTANCE == null) {
@@ -64,12 +67,42 @@ 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:") && !sdkVersion.equalsIgnoreCase(pkg.getVersion())) {
90+
logger.log(SentryLevel.ERROR, "The Sentry SDK has been configured with mixed versions. Expected %s to match core SDK version %s but was %s", pkg.getName(), sdkVersion, pkg.getVersion());
91+
mixedVersionsDetectedThisCheck = true;
92+
}
93+
}
94+
95+
if (mixedVersionsDetectedThisCheck) {
96+
logger.log(SentryLevel.ERROR, "^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
97+
logger.log(SentryLevel.ERROR, "^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
98+
logger.log(SentryLevel.ERROR, "^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
99+
logger.log(SentryLevel.ERROR, "^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
100+
}
101+
mixedVersionsDetected = mixedVersionsDetectedThisCheck;
102+
return mixedVersionsDetectedThisCheck;
103+
}
104+
}
105+
73106
@TestOnly
74107
public void clearStorage() {
75108
integrations.clear();

0 commit comments

Comments
 (0)