Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor service never gets cleaned up #431

Closed
bohnman opened this issue Jul 24, 2018 · 3 comments · Fixed by #502
Closed

executor service never gets cleaned up #431

bohnman opened this issue Jul 24, 2018 · 3 comments · Fixed by #502
Labels
difficulty: medium fix is medium in difficulty status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap

Comments

@bohnman
Copy link

bohnman commented Jul 24, 2018

Version:

7.22.0

Code Snippet

public static void main(String[] args) {
   Twilio.init("username", "password");
   Twilio.getExecutorService().submit(() -> System.out.println("Run"));
   Message.creator(new PhoneNumber("to"), new PhoneNumber("from"), "Testing.").create();
}

Steps to Reproduce

  1. Run the provided code
  2. Notice how the jvm doesn't shut down

The JVM doesn't shut down because there is a thread left hanging around in the executor service.

I would recommend the following:

Add a new field:

private static boolean selfCreatedExecutorService = false;

Modify setExecutorService:

public synchronized void setExecutorService(ListeningExecutorService executorService) {
    ListeningExecutorService oldExecutorService = Twilio.executorService;
    Twilio.executorService = executorService;
    
    if (oldExecutorService != null && selfCreatedExecutorService) {
        oldExecutorService.shutdownNow();
    }

    selfCreatedExectorService = false;
}

Modify getExecutorService, adding the line right after the creation and assignment of the executor service and after applying multi-threading fixes as described in #430.

selfCreatedExecutorService = true;

Add a destroy method:

public static synchronized void destroy() {
    if (executorService != null && selfCreatedExecutorService) {
        executorService.shutdownNow();
    }
}

Add a shutdown hook:

static {
   Runtime.getRuntime().addShutdownHook(new Thread(() -> Twilio.destroy()));
}

Finally, document that it is recommended to call Twilio.destroy before JVM shutdown. However, the shutdown hook will take care of it as a fallback.

@atbaker
Copy link

atbaker commented Jul 24, 2018

Hey @bohnman - really appreciate these thoughtful issues. Our team took a look at them and agree they're good suggestions, but I can't say exactly when we'll be able to implement them.

I'd love to send you a Twilio t-shirt to thank you for your thoughtful issues, though. Can you shoot me an email at [email protected] with your address and t-shirt size?

Thank you!

@bohnman
Copy link
Author

bohnman commented Jul 24, 2018

email sent. Thanks!

@thinkingserious thinkingserious added difficulty: medium fix is medium in difficulty status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap labels Nov 28, 2019
@thinkingserious
Copy link
Contributor

Thanks @bohnman!

This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

With best regards,

Elmer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: medium fix is medium in difficulty status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants