Skip to content

Commit efed2fe

Browse files
Close HostnameCache#executorService on SentryClient#close. (getsentry#1757)
Fixes getsentry#1756.
1 parent 5cf90ef commit efed2fe

7 files changed

Lines changed: 73 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
* Fix: Close HostnameCache#executorService on SentryClient#close (#1757)
6+
57
## 5.2.1
68

79
* Feat: Add isCrashedLastRun support (#1739)

sentry/api/sentry.api

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,8 @@ public final class io/sentry/IpAddressUtils {
357357
public static fun isDefault (Ljava/lang/String;)Z
358358
}
359359

360-
public final class io/sentry/MainEventProcessor : io/sentry/EventProcessor {
360+
public final class io/sentry/MainEventProcessor : io/sentry/EventProcessor, java/io/Closeable {
361+
public fun close ()V
361362
public fun process (Lio/sentry/SentryEvent;Ljava/lang/Object;)Lio/sentry/SentryEvent;
362363
public fun process (Lio/sentry/protocol/SentryTransaction;Ljava/lang/Object;)Lio/sentry/protocol/SentryTransaction;
363364
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public HostnameCache() {
6262
updateCache();
6363
}
6464

65+
void close() {
66+
this.executorService.shutdown();
67+
}
68+
69+
boolean isClosed() {
70+
return this.executorService.isShutdown();
71+
}
72+
6573
/**
6674
* Gets the hostname of the current machine.
6775
*

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88
import io.sentry.protocol.User;
99
import io.sentry.util.ApplyScopeUtils;
1010
import io.sentry.util.Objects;
11+
import java.io.Closeable;
12+
import java.io.IOException;
1113
import java.util.ArrayList;
1214
import java.util.HashMap;
1315
import java.util.List;
1416
import java.util.Map;
1517
import org.jetbrains.annotations.ApiStatus;
1618
import org.jetbrains.annotations.NotNull;
1719
import org.jetbrains.annotations.Nullable;
20+
import org.jetbrains.annotations.VisibleForTesting;
1821

1922
@ApiStatus.Internal
20-
public final class MainEventProcessor implements EventProcessor {
23+
public final class MainEventProcessor implements EventProcessor, Closeable {
2124

2225
/**
2326
* Default value for {@link SentryEvent#getEnvironment()} set when both event and {@link
@@ -251,4 +254,25 @@ private void setThreads(final @NotNull SentryEvent event, final @Nullable Object
251254
private boolean isCachedHint(final @Nullable Object hint) {
252255
return (hint instanceof Cached);
253256
}
257+
258+
@Override
259+
public void close() throws IOException {
260+
if (hostnameCache != null) {
261+
hostnameCache.close();
262+
}
263+
}
264+
265+
boolean isClosed() {
266+
if (hostnameCache != null) {
267+
return hostnameCache.isClosed();
268+
} else {
269+
return true;
270+
}
271+
}
272+
273+
@VisibleForTesting
274+
@Nullable
275+
HostnameCache getHostnameCache() {
276+
return hostnameCache;
277+
}
254278
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.sentry.transport.ITransport;
88
import io.sentry.util.ApplyScopeUtils;
99
import io.sentry.util.Objects;
10+
import java.io.Closeable;
1011
import java.io.IOException;
1112
import java.util.ArrayList;
1213
import java.util.Collection;
@@ -583,6 +584,21 @@ public void close() {
583584
.getLogger()
584585
.log(SentryLevel.WARNING, "Failed to close the connection to the Sentry Server.", e);
585586
}
587+
for (EventProcessor eventProcessor : options.getEventProcessors()) {
588+
if (eventProcessor instanceof Closeable) {
589+
try {
590+
((Closeable) eventProcessor).close();
591+
} catch (IOException e) {
592+
options
593+
.getLogger()
594+
.log(
595+
SentryLevel.WARNING,
596+
"Failed to close the event processor {}.",
597+
eventProcessor,
598+
e);
599+
}
600+
}
601+
}
586602
enabled = false;
587603
}
588604

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,15 @@ class MainEventProcessorTest {
439439
}
440440
}
441441

442+
@Test
443+
fun `when processor is closed, closes hostname cache`() {
444+
val sut = fixture.getSut()
445+
sut.close()
446+
assertNotNull(sut.hostnameCache) {
447+
assertTrue(it.isClosed())
448+
}
449+
}
450+
442451
private fun generateCrashedEvent(crashedThread: Thread = Thread.currentThread()) = SentryEvent().apply {
443452
val mockThrowable = mock<Throwable>()
444453
val actualThrowable = UncaughtExceptionHandlerIntegration.getUnhandledThrowable(crashedThread, mockThrowable)

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ class SentryClientTest {
133133
assertFalse(sut.isEnabled)
134134
}
135135

136+
@Test
137+
fun `when client is closed, hostname cache is closed`() {
138+
val sut = fixture.getSut()
139+
assertTrue(sut.isEnabled)
140+
sut.close()
141+
val mainEventProcessor = fixture.sentryOptions.eventProcessors
142+
.filterIsInstance<MainEventProcessor>()
143+
.first()
144+
assertTrue(mainEventProcessor.isClosed)
145+
}
146+
136147
@Test
137148
fun `when beforeSend is set, callback is invoked`() {
138149
var invoked = false

0 commit comments

Comments
 (0)