BackendBench
Coffee
Spot the Bug #93

The asynchronous method that blocks

A medium Java puzzle in the spring pitfalls family. One line below contains a real bug. Try to spot it first, then read why it fails and how to fix it.

mediumSpring pitfallsbuggy line: 7

Highlighted: line 7. Play this one as a puzzle without the answer.

What is wrong

The call to sendEmail goes through this, not through the proxy, so @Async is not applied and the three-second method runs on the calling thread. The page waits for the email to be sent.

The fix

Call sendEmail through another bean (for example an EmailSender), and make sure @EnableAsync is configured.

How to avoid it next time

Async, transactions, caching and retry all rely on proxies, so they work only across bean boundaries.

Want the background? Read @Async in the Java concepts library.

About spring pitfalls

Spring features such as @Transactional, @Async and @Cacheable work through proxies. Calls that bypass the proxy, wrong bean scopes and misplaced annotations make behavior silently disappear.

In code review: For every annotation that adds behavior, check that the call crosses a Spring-managed bean boundary, that the method is public, and that the bean is a singleton only when it is stateless.

More puzzles in this family

🐞 Play today's puzzleAll 132 bugs← #92#94 →

Frequently asked questions

What is the bug in "The asynchronous method that blocks"?

The call to sendEmail goes through this, not through the proxy, so @Async is not applied and the three-second method runs on the calling thread. The page waits for the email to be sent. The faulty line is line 7: sendEmail(user);

How do I fix it?

Replace it with: Call sendEmail through another bean (for example an EmailSender), and make sure @EnableAsync is configured.. Async, transactions, caching and retry all rely on proxies, so they work only across bean boundaries.

How do I catch spring pitfalls in code review?

For every annotation that adds behavior, check that the call crosses a Spring-managed bean boundary, that the method is public, and that the bean is a singleton only when it is stateless.

Is there a new puzzle every day?

Yes. The daily Spot the Bug puzzle picks one of these snippets for each UTC day, with the difficulty set by the weekday. Your streak is stored only in your browser.

More tools you might need