Repository navigation
Description
If the SMTP server terminates connections in the pool say after 10 seconds due to their own server policy, client side experiences the following exceptions:
org.simplejavamail.mailer.internal.MailerException: Failed to send email [<77c9d59f-d4cc-4a4c-b5c9-dd284269e58f.1645578246183@[192.168.1.123]>], reason: Third party error
at org.simplejavamail.mailer.internal.SendMailClosure.handleException(SendMailClosure.java:97)
at org.simplejavamail.mailer.internal.SendMailClosure.executeClosure(SendMailClosure.java:89)
at org.simplejavamail.mailer.internal.AbstractProxyServerSyncingClosure.run(AbstractProxyServerSyncingClosure.java:56)
at org.simplejavamail.internal.util.concurrent.NamedRunnable.run(NamedRunnable.java:41)
at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1640)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 421 Timeout waiting for data from client.
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2374)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1808)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1285)
at org.simplejavamail.mailer.internal.util.TransportRunner.lambda$sendMessage$0(TransportRunner.java:48)
at org.simplejavamail.mailer.internal.util.TransportRunner.sendUsingConnectionPool(TransportRunner.java:78)
at org.simplejavamail.mailer.internal.util.TransportRunner.runOnSessionTransport(TransportRunner.java:64)
at org.simplejavamail.mailer.internal.util.TransportRunner.sendMessage(TransportRunner.java:47)
at org.simplejavamail.mailer.internal.SendMailClosure.executeClosure(SendMailClosure.java:84)
... 6 common frames omitted
From then on, I see threads leaking, specially if the pool is configured with 100 core connections (the higher the worse). I think this is due to how the library handles the object pool during some connections in the pool experiencing failures.

I have resorted to changing the settings
from:
withConnectionPoolCoreSize: 5
withConnectionPoolMaxSize: 10
withConnectionPoolClaimTimeoutMillis: 5
withConnectionPoolExpireAfterMillis: 15
to:
withConnectionPoolCoreSize: 0
withConnectionPoolMaxSize: 10
withConnectionPoolClaimTimeoutMillis: 5
withConnectionPoolExpireAfterMillis: 5
The reason is:
- Stop the create/delete cycle of connections in the pool (since the core size is now 0). This prevents the ever increasing leak.
- Recycle any connections within 5 seconds as supposed to letting them reach the 10 second server 421). This removes the error path and therefore the leak.
With such changes, I have not observed the thread leak anymore, however the server timeout could still happen, and I would like to have connections in the pool warmed up (i.e. have a core count) with proper handling during failure modes.
Libraries I am using:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>6.6.1</version>
</dependency>
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>core-module</artifactId>
<version>6.6.1</version>
</dependency>
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>batch-module</artifactId>
<version>6.6.1</version>
</dependency>
I did see this, which is very similar:
bbottema/generic-object-pool@6a5684a
But the library version we are using is > 6.0.2, so the issue above is fixed in the version. we use. I think this may be another code path where the leak can happen.
I also tried the latest libraries across the board (7.1.0), and the issue is reproducible just the same.
Activity
bbottema commentedon Mar 18, 2022
Do you have some sample code with which to reproduce the problem your use case?
jllanes3 commentedon Mar 19, 2022
Here is one way I reproed this. Essentially, here are conditions:
Here are the results:


Same library version as the original post.
Same error as before is observed every 60 minutes, but pasting again just in case.
bbottema commentedon Mar 20, 2022
This is very puzzling indeed, the threads are clearly finishing, yet they are kept around by the executorService. I've run through all the code (without debugger) and it all looks solid to me. Must be some edge case race condition or something.
jllanes3 commentedon Mar 20, 2022
Interestingly, in the code above, if I change the thread pool size setting:
You can see the steps in the graph go from 10, to 50.

// from 10
// to 50

There is a correlation between the number of core sender threads (thread pool size) and the amount of connection pool threads leaking.


... (other threads in between)
jllanes3 commentedon Mar 26, 2022
Here is what I think the bug is:
The AutoAllocatorDeallocator pool-1-thread-1 is dying, silently, and no more deallocations are happening past that.
That class is in charge of cleaning items in this list:
@NotNull private final LinkedList<PoolableObject> waitingForDeallocation = new LinkedList<>();
Once that thread dies, that list continues to grow forever, no deallocations occur. The fix should be to simply catch that exception and handle it. Also, any background thread in general needs to have a solid try/catch in its run method so that it does not silently dies forever, or at the very list print that you are allowing it to die forever with a reason why. The code below is not protected against exceptions bubbling up.
You can follow the 0,1,2,3,4 steps below.
Here is the exception that kills it:
Past that point, the accumulation occurs at a rate equal to the number of core sender threads, since each of them will encounter no connection and will attempt to create a new one, which will succeed but never get cleaned up afterwards.
You can repro this by just throwing that exception in that last function yourself, and see how the system behaves past that point.
A few thread dumps show some progression:

Startup time when pool-1-thread-1 is bootstrapping the pool.
Regular functioning of the pool-1-thread-1 sleeping waiting to deallocate while sender threads are sending:

After that thread dies once the exception is hit:

bbottema commentedon Mar 27, 2022
Excellent analysis! You might very well pin pointed the problem. I've already updated and released the up stream libraries (generic-object-pool, clustered-object-pool and smtp-connection-pool) and a new version of Simple Java Mail 7.1.1.
Unfortunately, it's a pain to release this for the 6.x.x series. I hope you can switch to 7.1.1. Otherwise you might wanna try pinning the generic-object-pool dependency to version 2.0.1.
Give it a go! I'll close this until the problem pops up again.