Skip to content

Commit 71a7399

Browse files
runningcodeclaude
andcommitted
ref(core): Stop closing the hostname cache
The cache's executor is a single daemon thread with allowCoreThreadTimeOut(true) and a 30 second keep-alive, so it self-terminates once idle and never holds up process exit. close() was switching off something that switches itself off. Scopes.close() already leaves the timer executor running for this reason. Dropping it removes the reason SentryOptions.resetHostnameCache() existed. That method was only there because MainEventProcessor closed a cache the options own, which a re-init with the same options object would then keep handing out shut down. Nothing closes the cache now, so nothing has to undo it. MainEventProcessor no longer implements Closeable, and no longer memoizes the cache either: the options build it on first use, so the field and ensureHostnameCache() were duplicating LazyEvaluator. Both removed methods are on @ApiStatus.Internal types. SentryClientTest's `when client is closed, hostname cache is closed` went with them; it asserted isClosed() on a processor that had never resolved a hostname, which returned true because the cache was still null. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent a4aecfe commit 71a7399

8 files changed

Lines changed: 4 additions & 106 deletions

File tree

‎sentry/api/sentry.api‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,9 +1391,8 @@ public abstract interface class io/sentry/JsonUnknown {
13911391
public abstract fun setUnknown (Ljava/util/Map;)V
13921392
}
13931393

1394-
public final class io/sentry/MainEventProcessor : io/sentry/EventProcessor, java/io/Closeable {
1394+
public final class io/sentry/MainEventProcessor : io/sentry/EventProcessor {
13951395
public fun <init> (Lio/sentry/SentryOptions;)V
1396-
public fun close ()V
13971396
public fun getOrder ()Ljava/lang/Long;
13981397
public fun process (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent;
13991398
public fun process (Lio/sentry/SentryLogEvent;)Lio/sentry/SentryLogEvent;
@@ -3827,7 +3826,6 @@ public class io/sentry/SentryOptions : io/sentry/transport/RateLimiterConfig {
38273826
public fun isTraceSampling ()Z
38283827
public fun isTracingEnabled ()Z
38293828
public fun merge (Lio/sentry/ExternalOptions;)V
3830-
public fun resetHostnameCache ()V
38313829
public fun setAppStartExtender (Lio/sentry/IAppStartExtender;)V
38323830
public fun setAttachServerName (Z)V
38333831
public fun setAttachStacktrace (Z)V

‎sentry/src/main/java/io/sentry/HostnameCache.java‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public final class HostnameCache {
8181
// otherwise.
8282
this.cacheFreshUntil = Deadline.passed(ticker);
8383
// A single thread executor whose worker thread times out while idle, so no thread is kept
84-
// alive between the infrequent cache refreshes.
84+
// alive between the infrequent cache refreshes and nothing has to shut it down.
8585
final @NotNull ThreadPoolExecutor executor =
8686
new ThreadPoolExecutor(
8787
1,
@@ -95,14 +95,6 @@ public final class HostnameCache {
9595
updateCache();
9696
}
9797

98-
void close() {
99-
this.executorService.shutdown();
100-
}
101-
102-
boolean isClosed() {
103-
return this.executorService.isShutdown();
104-
}
105-
10698
/**
10799
* Gets the hostname of the current machine.
108100
*

‎sentry/src/main/java/io/sentry/MainEventProcessor.java‎

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,19 @@
88
import io.sentry.protocol.SentryTransaction;
99
import io.sentry.protocol.User;
1010
import io.sentry.util.HintUtils;
11-
import java.io.Closeable;
12-
import java.io.IOException;
1311
import java.util.ArrayList;
1412
import java.util.List;
1513
import java.util.Map;
1614
import org.jetbrains.annotations.ApiStatus;
1715
import org.jetbrains.annotations.NotNull;
1816
import org.jetbrains.annotations.Nullable;
19-
import org.jetbrains.annotations.VisibleForTesting;
2017

2118
@ApiStatus.Internal
22-
public final class MainEventProcessor implements EventProcessor, Closeable {
19+
public final class MainEventProcessor implements EventProcessor {
2320

2421
private final @NotNull SentryOptions options;
2522
private final @NotNull SentryThreadFactory sentryThreadFactory;
2623
private final @NotNull SentryExceptionFactory sentryExceptionFactory;
27-
private volatile @Nullable HostnameCache hostnameCache = null;
2824

2925
public MainEventProcessor(final @NotNull SentryOptions options) {
3026
this.options = options;
@@ -163,16 +159,7 @@ private void setServerName(final @NotNull SentryBaseEvent event) {
163159
}
164160

165161
if (options.isAttachServerName() && event.getServerName() == null) {
166-
ensureHostnameCache();
167-
if (hostnameCache != null) {
168-
event.setServerName(hostnameCache.getHostname());
169-
}
170-
}
171-
}
172-
173-
private void ensureHostnameCache() {
174-
if (hostnameCache == null) {
175-
hostnameCache = options.getHostnameCache();
162+
event.setServerName(options.getHostnameCache().getHostname());
176163
}
177164
}
178165

@@ -271,31 +258,6 @@ private boolean isCachedHint(final @NotNull Hint hint) {
271258
return HintUtils.hasType(hint, Cached.class);
272259
}
273260

274-
@Override
275-
public void close() throws IOException {
276-
if (hostnameCache != null) {
277-
hostnameCache.close();
278-
// Both this processor and the options outlive a restart, so a closed cache left in either
279-
// place would never refresh the hostname again.
280-
hostnameCache = null;
281-
options.resetHostnameCache();
282-
}
283-
}
284-
285-
boolean isClosed() {
286-
if (hostnameCache != null) {
287-
return hostnameCache.isClosed();
288-
} else {
289-
return true;
290-
}
291-
}
292-
293-
@VisibleForTesting
294-
@Nullable
295-
HostnameCache getHostnameCache() {
296-
return hostnameCache;
297-
}
298-
299261
@Override
300262
public @Nullable Long getOrder() {
301263
return 0L;

‎sentry/src/main/java/io/sentry/SentryOptions.java‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3109,17 +3109,6 @@ public void setDateProvider(final @NotNull SentryDateProvider dateProvider) {
31093109
return hostnameCache.getValue();
31103110
}
31113111

3112-
/**
3113-
* Discards the cached instance, so that the next {@link #getHostnameCache()} builds a new one.
3114-
*
3115-
* <p>Called after the cache has been closed, because these options outlive a restart and a closed
3116-
* cache can no longer resolve anything.
3117-
*/
3118-
@ApiStatus.Internal
3119-
public void resetHostnameCache() {
3120-
hostnameCache.resetValue();
3121-
}
3122-
31233112
/**
31243113
* Adds a ICollector.
31253114
*

‎sentry/src/test/java/io/sentry/HostnameCacheTest.kt‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,4 @@ class HostnameCacheTest {
4848
assertThat(executorService.corePoolSize).isEqualTo(1)
4949
assertThat(executorService.maximumPoolSize).isEqualTo(1)
5050
}
51-
52-
@Test
53-
fun `close shuts the executor down`() {
54-
val cache = getSut()
55-
cache.close()
56-
assertThat(cache.isClosed).isTrue()
57-
}
5851
}

‎sentry/src/test/java/io/sentry/MainEventProcessorTest.kt‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -564,21 +564,6 @@ class MainEventProcessorTest {
564564
}
565565
}
566566

567-
@Test
568-
fun `when processor is closed, closes hostname cache`() {
569-
val sut = fixture.getSut(serverName = null)
570-
571-
sut.process(SentryTransaction(fixture.sentryTracer), Hint())
572-
val hostnameCache = assertNotNull(sut.hostnameCache)
573-
574-
sut.close()
575-
576-
assertTrue(hostnameCache.isClosed)
577-
// Dropped on close so that a restart resolves the hostname again instead of reusing a cache
578-
// whose executor is shut down.
579-
assertNull(sut.hostnameCache)
580-
}
581-
582567
@Test
583568
fun `when event has modules, appends to them`() {
584569
val sut = fixture.getSut(modules = mapOf("group1:artifact1" to "2.0.0"))

‎sentry/src/test/java/io/sentry/SentryClientTest.kt‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,6 @@ class SentryClientTest {
214214
assertFalse(sut.isEnabled)
215215
}
216216

217-
@Test
218-
fun `when client is closed, hostname cache is closed`() {
219-
val sut = fixture.getSut()
220-
assertTrue(sut.isEnabled)
221-
sut.close()
222-
val mainEventProcessor =
223-
fixture.sentryOptions.eventProcessors.filterIsInstance<MainEventProcessor>().first()
224-
assertTrue(mainEventProcessor.isClosed)
225-
}
226-
227217
@Test
228218
fun `when beforeSend is set, callback is invoked`() {
229219
var invoked = false

‎sentry/src/test/java/io/sentry/SentryOptionsTest.kt‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,15 +1196,4 @@ class SentryOptionsTest {
11961196
assertThat(options.hostnameCache).isSameInstanceAs(cache)
11971197
assertThat(cache.getProperty<MonotonicTicker>("ticker")).isSameInstanceAs(ticker)
11981198
}
1199-
1200-
@Test
1201-
fun `resetHostnameCache discards the cached instance`() {
1202-
val options = optionsWithTicker(TestMonotonicTicker())
1203-
val cache = options.hostnameCache
1204-
1205-
options.resetHostnameCache()
1206-
1207-
assertThat(options.peekHostnameCache()).isNull()
1208-
assertThat(options.hostnameCache).isNotSameInstanceAs(cache)
1209-
}
12101199
}

0 commit comments

Comments
 (0)