vertx-web icon indicating copy to clipboard operation
vertx-web copied to clipboard

Deadlock due to synchronized on RouteImpl

Open visualage opened this issue 8 years ago • 7 comments

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.

visualage avatar May 25 '18 17:05 visualage

Fixed by: #929

pmlopes avatar May 29 '18 07:05 pmlopes

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?

ghost avatar Sep 27 '18 14:09 ghost

that sounds doable to me

vietj avatar Sep 27 '18 14:09 vietj

@pmlopes can you back-port this to 3.5 branch ?

vietj avatar Sep 27 '18 14:09 vietj

well I can handle it too it seems easy to back-port

vietj avatar Sep 27 '18 14:09 vietj

Thank you. I really appreciate it if you are able to pull this in.

ghost avatar Sep 27 '18 14:09 ghost

Fantastic, thanks for the quick response @vietj !

matt404 avatar Sep 27 '18 14:09 matt404