twilio-java icon indicating copy to clipboard operation
twilio-java copied to clipboard

[BUG] IllegalStateException in Twilio.java's static initialization block

Open bfreitastgtg opened this issue 2 months ago • 0 comments

Describe the bug

This code block in the Twilio class can throw an IllegalStateException:

  /*
     * Ensures that the ExecutorService is shutdown when the JVM exits.
     */
    static {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                if (executorService != null) {
                    executorService.shutdownNow();
                }
            }
        });
    }
java.lang.IllegalStateException: Shutdown in progress
	at java.base/java.lang.ApplicationShutdownHooks.add(Unknown Source)
	at java.base/java.lang.Runtime.addShutdownHook(Unknown Source)
	at com.twilio.Twilio.<clinit>(Twilio.java:50)

This happens when the application has already started its shutdown. From Java's Runtime class:

IllegalStateException - If the shutdown sequence has already begun

The fact this is thrown on a static block in library code makes it really difficult to gracefully handle the exception in application code.

Code snippet

Cannot provide application code for legal reasons.

Actual behavior

The library throws the unchecked exception.

Expected behavior

The library should handle the unchecked exception, preventing the issue from propagating.

twilio-java version

11.0.0

Java version

Amazon Corretto Java 24

Logs or error messages

No response

Additional context

No response

bfreitastgtg avatar Oct 22 '25 08:10 bfreitastgtg