question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Subscriptions are processing after Spring graceful shutdown

See original GitHub issue

Describe the bug Not sure if this is some problem in how we use pubsub, or an actual bug. Let me know if this does not belong here and I can close it. But we have a Subscriber wrapper class that contains a bunch of subscribers in a collection. And when spring shuts down we do the following to shutdown and await each one:

@PreDestroy
override fun stop() {
    subscribers.onEach { (subscriptionName, subscriber) ->
        logger.info("Stopping {}", kv("subscription", subscriptionName))
        subscriber.stopAsync()
    }
    subscribers.onEach { (subscriptionName, subscriber) ->
        logger.info("Waiting for {} to terminate", kv("subscription", subscriptionName))
        subscriber.awaitTerminated(5, TimeUnit.SECONDS)
    }
}

The problem is we get exceptions like these: class java.lang.reflect.InvocationTargetException null, Cause: class org.springframework.transaction.CannotCreateTransactionExceptionCould not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException: EntityManagerFactory is closed in our subscribers. Not all, but some.

Spring is configured like this in terms of shutdown:

server.shutdown=graceful
spring.lifecycle.timeout-per-shutdown-phase=30s

Our hunch is that maybe the shutdown times out and EntityManagerFactory is closed before the subscriptions are shutdown properly.

Does our shutdown logic look sane, or should we handle this differently? Is it possible that PubSub executors are still running after the timeout given our setup?

Very thankful for any input or guidance on this topic.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
eriklagerholmcommented, Mar 30, 2021

For future reference we opted to use Spring’s SmartLifecycle API in order to achieve what we want. It was able to execute the code before the EntityManager was closed.

1reaction
meltsufincommented, Mar 29, 2021

Closing due to inactivity. Please re-open if you are still experiencing the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot graceful shutdown - java - Stack Overflow
To enable graceful shutdown, add server.shutdown=graceful to properties (by default it is set to immediate ). Grace period can be configured ...
Read more >
Configuring Graceful-Shutdown, Readiness and Liveness ...
In general, Graceful shutdown means that before shutting down the application, there should be timeout period given to allow the requests to ...
Read more >
A Study of Graceful Shutdown for Spring Boot Applications
ThreadPoolExecutor becomes SHUTDOWN after shutdown and cannot accept new tasks, and then waits for the execution of the task being executed to ...
Read more >
Graceful Shutdown of Spring Boot Applications in Kubernetes
Generally, applications can gracefully shut down when they receive SIGTERM signal, but this may not happen due to some third party applications ...
Read more >
Clean Ways to Shutdown Spring Boot Applications - Foojay
The process outlives the Ctrl+C termination of Spring Boot ... It is different from a graceful shutdown at the application level, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found