question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resource leak by not closing the client

See original GitHub issue

The HTTP client is not explicitly closed, leading to possible leaks. The JAX-RS ClientBuilder is dynamically resolved, which in my case uses RestEasy instead of Jersey. In that case it uses a finalizer to log a warning that the client wasn’t closed, which is a requirement of the API. It would also be nice if these were pooled.

These issues makes it a little uncomfortable to use this SDK in a production with a reasonable email volume.

 * Client is the main entry point to the fluent API used to build and execute client
 * requests in order to consume responses returned.
 * <p/>
 * Clients are heavy-weight objects that manage the client-side communication
 * infrastructure. Initialization as well as disposal of a {@code Client} instance
 * may be a rather expensive operation. It is therefore advised to construct only
 * a small number of {@code Client} instances in the application. Client instances
 * must be {@link #close() properly closed} before being disposed to avoid leaking
 * resources.

https://github.com/sargue/mailgun/blob/abd667ceac159ae0d208b7e1d571cca8959c4bc7/src/main/java/net/sargue/mailgun/Mail.java#L143-L158

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ben-manescommented, Apr 8, 2020

@kopax, I was using simpleJavaMail, which is a really excellent library. However, the SMTP endpoint is very flaky at scale and requires retries. Mailgun’s support wasn’t helpful except acknowledging that they do some nasty things in their load balancer to cycle IPs for defensive reasons. They said the HTTP api was faster and more robust, so I had to port over.

I wrote an Email class similar to @sargue’s MailBuilder class to aggregate the contents into a multipart. This goes to a Retrofit interface, which is backed by a shared OkHttp client. Not much work thanks to this library and the api docs to clarify their endpoint.

mailgun.zip

1reaction
ben-manescommented, Apr 8, 2020

My requirements are the same and not bulk mail, but it amounts to around 800k/month. This is for example a company sending an invoice to their customer.

I did not include the code using this client, which adds those to the Email class, invokes the Mailgun.send method, and handles all of the metrics / retrials / etc. You would use this as,

Email email = new Email()
    .from(...).to(...)
    .text("Hello World")
    .attachment(filename, file, mediaType);
mailgun.send(domain, email);

Anyone is welcome to formalize into a reusable client, like @sargue’s nice library. It was not much work on my part, Retrofit does all the heavy lifting and this is just an example of how quickly one can bootstrap a usage.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Potential resource leak: 'client' may not be closed at this location
I'm using HttpClient , but in Eclipse I'm getting warning "Potential resource leak".
Read more >
Closing Sockets and Avoiding Resource Leaks - Documentation
Resource leaks, like memory leaks, can lead to subtle bugs, as shown in the ... The destructor for RWSocket, however, does not close...
Read more >
Resource leak | Amazon CodeGuru Reviewer, Detector Library
Allocated resources are not released properly. This can slow down or crash your system. They must be closed along all paths to prevent...
Read more >
Finding and fixing memory leaks in Go - DEV Community ‍ ‍
But, what happens if you don't Close the client when you should? You get a memory leak. The underlying connections never get cleaned...
Read more >
How to Detect Memory Leaks in Java: Causes, Types, & Tools
If memory leaks are not handled, your application can eventually run out of memory and terminate. Learn what causes memory leaks in Java...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found