Deadlock due to synchronized on RouteImpl
Version
- vert.x core: 3.5.1
- vert.x web: 3.5.1
Context
When we have two Route in Router, sometimes the failure handling would cause deadlock when there are multiple requests in process. Assuming we have two Routes, Route1 and Route2, in Router, here is the sequence of events in the two threads:
Thread 1: Lock Route1 and handle context, Lock Route2 and handle context, Release Route1 and Route2 due to scheduling event, Lock Route2 only due to running the scheduled event in the same thread, Try lock Route1 due to failure handling Thread 2: Lock Route1 and handle context, Try lock Route2 for handling context.
This creates a deadlock.
Do you have a reproducer?
- A unit test is added to the pull request demonstrating the deadlock.
Extra
- See PR https://github.com/vert-x3/vertx-web/pull/929 for a potential fix and a unit test to reproduce the problem.
Fixed by: #929
I see this issue is labeled as closed, but is currently not targeted until the 3.6.0 release. We are now finding this bug has wider impacts. This bug can impact a single route; it does not require multiple routes to trigger. And worse, it is a performance impact on each route. Having handleContext and handleFailure synchronized means that only one thread can be in a Handler on a route at a time, basically serializing the active handler processing for each route. Not as big a deal if all the code is asynchronous, but if you hand of route processing to a worker thread and the worker thread invokes the handler for a blocking operation, you block the entire route.
Could this bug be considered for including in 3.5.4?
that sounds doable to me
@pmlopes can you back-port this to 3.5 branch ?
well I can handle it too it seems easy to back-port
Thank you. I really appreciate it if you are able to pull this in.
Fantastic, thanks for the quick response @vietj !