Quick revision
The short answer
Reactor marks Netty's event-loop threads as non-blocking, and block(), blockFirst() and blockLast() check for that and throw an IllegalStateException instead of parking the thread, because parking an event loop would stall every connection it serves. The handler should compose instead of waiting, returning someMono.flatMap(...) or combining results with Mono.zip, and if a blocking API is unavoidable it should be wrapped in Mono.fromCallable(...).subscribeOn(Schedulers.boundedElastic()). block() is acceptable only at the edges of reactive code, such as tests and main methods.
What to remember
In this answer, we saw that block() parks the current thread, and that Reactor refuses to do so on threads marked NonBlocking, such as Netty's reactor-http-nio event loops and Schedulers.parallel(), throwing an IllegalStateException to protect the loop and to avoid deadlocks. The handler should compose with flatMap, map and zip and return the resulting Mono. A genuinely blocking API is wrapped in Mono.fromCallable(...).subscribeOn(Schedulers.boundedElastic()), and block() is used only at the edges of reactive code, such as tests and main methods.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Reactor marks Netty's event-loop threads as non-blocking, and block(), blockFirst() and blockLast() check for that and throw an IllegalStateException instead of parking the thread, because parking an event loop would stall every connection it serves. The handler should compose instead of waiting, returning someMono.flatMap(...) or combining results with Mono.zip, and if a blocking API is unavoidable it should be wrapped in Mono.fromCallable(...).subscribeOn(Schedulers.boundedElastic()). block() is acceptable only at the edges of reactive code, such as tests and main methods.
Why this question is asked
Developers coming from imperative code reach for block() because it turns a Mono into a value they can use on the next line. In a WebFlux handler that habit fails immediately with a clear error, which is a good thing: Reactor is protecting the server from a mistake that would otherwise show up as mysterious latency. Interviewers ask about this error to see whether we know why it exists and how to restructure code so it never needs to wait. In this answer, we will recap what block() does, see why Reactor refuses it on this thread, rewrite the handler by composition, handle the case of a genuinely blocking API, and look at where block() is fine.
1A Quick Recap of What block() Does
A Mono is a promise of at most one value that will arrive later. block() subscribes to it and then parks the current thread until the value, an empty completion or an error arrives, and returns the value. blockFirst() and blockLast() do the same for a Flux. In a main method or a test that is exactly what we want, because there is nothing else for that thread to do.
In a WebFlux handler, the current thread is a Netty event loop, such as reactor-http-nio-3, which serves hundreds of connections. Parking it would leave all of those connections unserved until the value arrived.
2Why Reactor Throws on This Thread
Reactor has a marker interface, NonBlocking. Threads created for Schedulers.parallel(), Schedulers.single() and Reactor Netty's event loops implement it. Before parking, block() calls Schedulers.isInNonBlockingThread(), and if the answer is true it throws instead of waiting.
6 messages in order. Play them, or step through them one at a time.
Reactor refuses to park a thread that other connections depend on.
Notice that this fails fast and deterministically, on the first request, even under no load. That is the design intent. Without the check, the code would work in a quick test and then, under load, park event loops for the duration of each lookup, creating the latency spikes that are so hard to diagnose. Reactor turns a production performance bug into an immediate development error.
There is also a deadlock risk the check prevents. If the value we wait for must itself be delivered by the same event loop, for example a WebClient response arriving on the connection's loop, the loop would be parked waiting for work only it can do, and the request would never complete.
3Compose Instead of Waiting
The fix is to describe what should happen when the value arrives, instead of waiting for it. flatMap takes the value and returns the next asynchronous step, map transforms it, and zip runs independent lookups together and combines their results.
Notice that the handler now returns immediately with a Mono describing the work. The event loop is free to serve other connections, and when the database replies, the rest of the chain runs and the response is written. Nothing waits.
A common mistake when converting code is to keep imperative steps in the middle of the chain, such as calling block() inside a map. The same rule applies there: inside a chain, the next asynchronous step goes in a flatMap.
4When the API Really Is Blocking
Sometimes the call underneath is blocking by nature, such as a JDBC repository, a legacy SDK or a file read. Composition cannot make it non-blocking, so we move it to a thread that may block. fromCallable makes the call lazy, so it runs at subscription and not at assembly, and subscribeOn runs that subscription on Schedulers.boundedElastic(), whose threads are not marked non-blocking.
This keeps the event loop free, at the cost of a thread waiting on boundedElastic for each call. It is the right tool for occasional blocking calls; if most of a service's work is like this, a Spring MVC service on virtual threads is usually a better fit than WebFlux.
5Where block() Is Acceptable
block() belongs at the boundaries where reactive code meets code that needs a value: a main method, a test, a command-line runner, a batch job, or an MVC controller running on a Servlet thread that calls WebClient. Even in tests, StepVerifier is usually better because it checks each signal and can use virtual time, but block() is fine for simple setup. We should also avoid sneaking around the check, for example by calling toFuture().get(), which blocks the event loop just the same without Reactor's protection; BlockHound in tests catches such cases.
| Thread | Marked non-blocking? | block() behaviour |
|---|---|---|
| reactor-http-nio-N (Netty event loop) | yes | IllegalStateException |
parallel-N (Schedulers.parallel) | yes | IllegalStateException |
boundedElastic-N | no | waits; acceptable for blocking work |
| main, test runner, Tomcat thread | no | waits; acceptable at the edges |
6Follow-up questions
Why not just catch the exception and use toFuture().get() instead?
Because the rule exists to protect the event loop, and toFuture().get() parks it just as block() would, only without the safety check. The result is the latency spikes or deadlocks the check was meant to prevent. BlockHound would report it as a blocking call.
Can I call block() inside subscribeOn(boundedElastic)?
Technically yes, because boundedElastic threads are not marked non-blocking, so block() waits there. It still wastes a thread per call and is a sign the code should be composed instead. It is sometimes used as a stepping stone when migrating imperative code, but it should not be the final shape.
Does the same error appear in Spring MVC?
Not normally. MVC controllers run on Servlet container threads, which are not marked non-blocking, so block() simply waits. That is acceptable there, though in MVC a synchronous client such as RestClient is usually simpler than blocking on WebClient.
7Conclusion
In this answer, we saw that block() parks the current thread, and that Reactor refuses to do so on threads marked NonBlocking, such as Netty's reactor-http-nio event loops and Schedulers.parallel(), throwing an IllegalStateException to protect the loop and to avoid deadlocks. The handler should compose with flatMap, map and zip and return the resulting Mono. A genuinely blocking API is wrapped in Mono.fromCallable(...).subscribeOn(Schedulers.boundedElastic()), and block() is used only at the edges of reactive code, such as tests and main methods.
Open with
Reactor throws this on purpose to protect the event loop. block() subscribes and then parks the current thread until the value arrives. In a WebFlux handler, the current thread is a Netty event loop, reactor-http-nio-3 here, which serves hundreds of connections, so parking it would stall all of them, and if the value has to be delivered by that same loop, like a WebClient response, it could even deadlock.
Then, if they want more
Reactor marks its event-loop threads, and the parallel and single schedulers, with a NonBlocking marker, and block, blockFirst and blockLast check Schedulers.isInNonBlockingThread() before waiting. If it's true they throw IllegalStateException instead. That's helpful, because it turns a latency bug that would only show under load into an immediate error on the first request.
The fix is to compose rather than wait: return someMono.flatMap(value -> next step) and build the response inside the chain, use map for transformations, and Mono.zip for independent lookups so they run concurrently. The handler returns the Mono right away and the event loop stays free.
If the API underneath is genuinely blocking, like JDBC or a legacy SDK, I wrap it in Mono.fromCallable so it's lazy and subscribeOn(Schedulers.boundedElastic()), which is a pool meant for blocking. And block() is fine only at the edges: tests, main methods, command-line runners, or an MVC controller on a Servlet thread.
I'd also avoid workarounds like toFuture().get(), which blocks the loop without the check, and I'd run BlockHound in tests to catch those.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring WebFlux, Project Reactor reference guide
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A WebFlux handler calls someMono.block() and fails with IllegalStateException...": https://scrivio-interview-library.netlify.app/spring-boot/webflux/c4d3528a0e/