Skip to content

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

@bfreitastgtg

Description

@bfreitastgtg

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugbug in the library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions