Skip to content

Twilio class has a lot of multithreading issues. #430

@bohnman

Description

@bohnman

Version:

7.22.0

Steps to Reproduce

The Twilio class has a bunch of multithreading issues. I fundamentally disagree with using singletons that are mutable, but if you're going to do it, it needs to be thread safe.

Here's what I recommend:

  • add synchronized to all set* and init* methods . These methods are called infrequently so the synchronization penalty will be minimal
  • Make the restClient and executorService fields volatile
  • For getRestClient and getExecutorService we can minimize the synchronization by doing the double null check trick. Here is an example pattern on how to use it:
public static TwilioRestClient getRestClient() {
   if (restClient == null) {
      synchronized(Twilio.class) {
          if (restClient == null) {
              restClient = buildRestClient()
          }
      }
   }

  return restClient
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions