Skip to content

Commit 2e6b3a3

Browse files
SDKs send queue is no longer shutdown immediately on re-init (#4564)
* Let queue drain on a restart * Format code * Format code * Update sentry-samples/sentry-samples-console-opentelemetry-noagent/src/test/kotlin/sentry/systemtest/ConsoleApplicationSystemTest.kt * Let queue drain on a restart * Format code * Format code * Update sentry-samples/sentry-samples-console-opentelemetry-noagent/src/test/kotlin/sentry/systemtest/ConsoleApplicationSystemTest.kt * adapt tests * changelog --------- Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
1 parent a889829 commit 2e6b3a3

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
- This was causing Sentry SDK to log warnings: "Sentry Log is disabled and this 'logger' call is a no-op."
3131
- Do not use Sentry logging API in Log4j2 if logs are disabled ([#4573](https://github.com/getsentry/sentry-java/pull/4573))
3232
- This was causing Sentry SDK to log warnings: "Sentry Log is disabled and this 'logger' call is a no-op."
33+
- SDKs send queue is no longer shutdown immediately on re-init ([#4564](https://github.com/getsentry/sentry-java/pull/4564))
34+
- This means we're no longer losing events that have been enqueued right before SDK re-init.
3335
- Reduce scope forking when using OpenTelemetry ([#4565](https://github.com/getsentry/sentry-java/pull/4565))
3436
- `Sentry.withScope` now has the correct current scope passed to the callback. Previously our OpenTelemetry integration forked scopes an additional.
3537
- Overall the SDK is now forking scopes a bit less often.

sentry/src/main/java/io/sentry/ShutdownHookIntegration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions
3232
Objects.requireNonNull(options, "SentryOptions is required");
3333

3434
if (options.isEnableShutdownHook()) {
35-
thread = new Thread(() -> scopes.flush(options.getFlushTimeoutMillis()));
35+
thread =
36+
new Thread(() -> scopes.flush(options.getFlushTimeoutMillis()), "sentry-shutdownhook");
3637
handleShutdownInProgress(
3738
() -> {
3839
runtime.addShutdownHook(thread);

sentry/src/main/java/io/sentry/transport/AsyncHttpTransport.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,23 @@ public void close(final boolean isRestarting) throws IOException {
174174
executor.shutdown();
175175
options.getLogger().log(SentryLevel.DEBUG, "Shutting down");
176176
try {
177-
// We need a small timeout to be able to save to disk any rejected envelope
178-
long timeout = isRestarting ? 0 : options.getFlushTimeoutMillis();
179-
if (!executor.awaitTermination(timeout, TimeUnit.MILLISECONDS)) {
180-
options
181-
.getLogger()
182-
.log(
183-
SentryLevel.WARNING,
184-
"Failed to shutdown the async connection async sender within "
185-
+ timeout
186-
+ " ms. Trying to force it now.");
187-
executor.shutdownNow();
188-
if (currentRunnable != null) {
189-
// We store to disk any envelope that is currently being sent
190-
executor.getRejectedExecutionHandler().rejectedExecution(currentRunnable, executor);
177+
// only stop sending events on a real shutdown, not on a restart
178+
if (!isRestarting) {
179+
// We need a small timeout to be able to save to disk any rejected envelope
180+
long timeout = options.getFlushTimeoutMillis();
181+
if (!executor.awaitTermination(timeout, TimeUnit.MILLISECONDS)) {
182+
options
183+
.getLogger()
184+
.log(
185+
SentryLevel.WARNING,
186+
"Failed to shutdown the async connection async sender within "
187+
+ timeout
188+
+ " ms. Trying to force it now.");
189+
executor.shutdownNow();
190+
if (currentRunnable != null) {
191+
// We store to disk any envelope that is currently being sent
192+
executor.getRejectedExecutionHandler().rejectedExecution(currentRunnable, executor);
193+
}
191194
}
192195
}
193196
} catch (InterruptedException e) {

sentry/src/test/java/io/sentry/transport/AsyncHttpTransportTest.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,16 +352,22 @@ class AsyncHttpTransportTest {
352352
}
353353

354354
@Test
355-
fun `close with isRestarting true does not await termination`() {
356-
fixture.sentryOptions.flushTimeoutMillis = 123
355+
fun `close shuts down the executor and runs executing runnable through rejectedExecutionHandler`() {
356+
val rejectedExecutionHandler = mock<RejectedExecutionHandler>()
357357
val sut = fixture.getSUT()
358-
sut.close(true)
358+
val runnable = mock<Runnable>()
359359

360-
verify(fixture.executor).awaitTermination(eq(0), eq(TimeUnit.MILLISECONDS))
360+
// Emulate a runnable currently being executed
361+
sut.injectForField("currentRunnable", runnable)
362+
whenever(fixture.executor.rejectedExecutionHandler).thenReturn(rejectedExecutionHandler)
363+
sut.close(false)
364+
365+
verify(fixture.executor).shutdownNow()
366+
verify(rejectedExecutionHandler).rejectedExecution(eq(runnable), eq(fixture.executor))
361367
}
362368

363369
@Test
364-
fun `close shuts down the executor and runs executing runnable through rejectedExecutionHandler`() {
370+
fun `does not shut down executor immediately on restart`() {
365371
val rejectedExecutionHandler = mock<RejectedExecutionHandler>()
366372
val sut = fixture.getSUT()
367373
val runnable = mock<Runnable>()
@@ -371,8 +377,9 @@ class AsyncHttpTransportTest {
371377
whenever(fixture.executor.rejectedExecutionHandler).thenReturn(rejectedExecutionHandler)
372378
sut.close(true)
373379

374-
verify(fixture.executor).shutdownNow()
375-
verify(rejectedExecutionHandler).rejectedExecution(eq(runnable), eq(fixture.executor))
380+
verify(fixture.executor).shutdown()
381+
verify(fixture.executor, never()).shutdownNow()
382+
verify(rejectedExecutionHandler, never()).rejectedExecution(eq(runnable), eq(fixture.executor))
376383
}
377384

378385
@Test

0 commit comments

Comments
 (0)