twilio / twilio/twilio-java

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

Open
#912 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: bug
Dominant language
Java
Stars
527
Forks
453
Avg merge
10h 59m
Merged PRs (30d)
6

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Twilio.java at the static initialization block that calls Runtime.addShutdownHook, and review how shutdown-in-progress is handled by Java 24. Reproduce or test initialization during JVM shutdown, then verify that the unchecked exception no longer propagates from the library.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.