Skip to content

ThreadPoolExecutor terminated when sending an async mail #204

@amanteaux

Description

@amanteaux

Hello,

In production I had this error with simple-java-mail 4.4.5:

java.util.concurrent.RejectedExecutionException: Task sendMail process rejected from java.util.concurrent.ThreadPoolExecutor@79627f99[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 3]
        at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
        at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
        at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
        at org.simplejavamail.mailer.internal.mailsender.MailSender.send(MailSender.java:196)
        at org.simplejavamail.mailer.Mailer.sendMail(Mailer.java:364)

The call was: mailer.sendMail(email, true);

By looking at these classes:

There is indeed a bug:

  • in the synchronized checkShutDownRunningProcesses method, the pool is shutdown if there is no more mail to send,
  • in the synchronized send method, the pool is started if it is null or shutdown.
    But the thing is, calling the shutdown method on a ThreadPoolExecutor is not a synchronized operation, it just Initiates an orderly shutdown.

My main question is actually: why shutting down the pool if there is no mail to send?
In the comment before the shutdown is is written shutdown the threadpool, or else the Mailer will keep any JVM alive forever. But actually by default, the thread factory in the ThreadPoolExecutor build non daemon threads, so it will not keep the JVM alive.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions