microsoft-authentication-library-for-java icon indicating copy to clipboard operation
microsoft-authentication-library-for-java copied to clipboard

Use a Custom ThreadPool made from Daemon Threads to execute Authentication Requests as Completable Futures.

Open g2vinay opened this issue 3 years ago • 1 comments

Hi,

The MSAL4J library uses Completable Futures to execute authentication requests. The completable futures by default rely on common Fork join thread pool to execute authentication requests. This causes problems such as deadlock for the users when the application code is using all the threads and the msal request is waiting on the thread to be made available. To get around this, user is forced to create & manage an Executor service and supply it to MSAL clients. This adds complexity to user code and doesn't offer them the best UX.

In MSAL4J, you can create an Executor Service from daemon threads as default internally and pass it to Completable Future.

// Below code is just a sample to Executor Service as Daemon Threads.
ExecutorService exec = Executors.newFixedThreadPool(4,
        new ThreadFactory() {
            public Thread newThread(Runnable r) {
                Thread t = Executors.defaultThreadFactory().newThread(r);
                t.setDaemon(true);
                return t;
            }
        });


Pros of this are that:

  1. The executor service made from Daemon threads will not block JVM from shutting down and will auto exit/close when application terminates.
  2. Users will not be forced to create a custom executor service and manage it in their code, offering them a good UX.
  3. Users will not run into Deadlocks when running MSAL4J clients with default configuration, improving the UX and reliability.

If you're fine, I can do a PR into Msal4j repo as well.

g2vinay avatar Apr 26 '22 20:04 g2vinay

This makes sense Vinay! Feel free to create a PR for this.

siddhijain avatar Apr 27 '22 02:04 siddhijain