Skip to content
This repository has been archived by the owner on Apr 23, 2019. It is now read-only.

Fix thread deadlock issue in RPC handler. #211

Merged
merged 3 commits into from Apr 17, 2019

Conversation

Happy0
Copy link
Contributor

@Happy0 Happy0 commented Apr 12, 2019

There is a thread deadlock condition in RPCHandler which prevents any further messages being sent or received.

  • Thread 1 wants to send a request via makeAsyncRequest or openStream which are synchronized methods.

  • This results in a call to socket.write in vertx as part of creating the request or stream.

  • Thread 2, the vertx event loop wants to call back the receivedMessage handler. This is a synchronized method in RPCHandler , so vertx blocks because thread 1 is waiting on socket.write - however, socket.write cannot be serviced by the event loop since it's blocked.

@Happy0
Copy link
Contributor Author

Happy0 commented Apr 12, 2019

Also, I noticed the 'Cava is moving' notice on the README - will the recent scuttlebutt module commits also be moved across? (and this one?)

@Happy0
Copy link
Contributor Author

Happy0 commented Apr 13, 2019

I'll update this PR with something which doesn't block vertx at all (or only blocks on adding to a concurrent queue.) It feels counterintuitive to use locks in the middle of a vertx event loop :)

@atoulme
Copy link
Contributor

atoulme commented Apr 14, 2019

Yes, Cava is moving. I've been pushing your patches to Apache Tuweni though.

@Happy0 Happy0 force-pushed the deadlock_fix branch 3 times, most recently from 06eb6c7 to d762d20 Compare April 14, 2019 11:49
@Happy0
Copy link
Contributor Author

Happy0 commented Apr 14, 2019

Thanks @atoulme - I'll make future pull requests after this one to Apache Tuweni instead :). This pull request is ready to review again.

@Happy0
Copy link
Contributor Author

Happy0 commented Apr 14, 2019

I also updated the pull request to make the RPCHandler methods return a type which makes it clear that the result of the future is a successful response body, while any errors mean the future is completed exceptionally. Unlike RPCMessage, it doesn't have the header fields which aren't relevant in the context the result is being handled.

I did this because I actually made this mistake of checking for errors in the RPCMessage in the library I'm consuming this from, so it was unclear even to me as the implementer of RPCHandler.

…onse bodies as implementers may think they have to check and handle errors in RPCMessage, rather than handling the future completing exceptionally.
this.bodyType = bodyType;
}

public Bytes getBody() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we opted for short method names "body()" would be best. Same for "bodyType()"

* We run each each update on this executor to update the request state synchronously, and to handle the underlying
* connection closing by failing the in progress requests and not accepting future requests
*/
private final ExecutorService executor = Executors.newSingleThreadExecutor();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this executor closed? If vert.x is in use, you may be able to leverage it for this purpose.

*/
package net.consensys.cava.scuttlebutt.rpc.mux.exceptions;

public class RPCRequestFailedException extends Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the class final. Also we try to extend RuntimeException if possible.

@Happy0
Copy link
Contributor Author

Happy0 commented Apr 14, 2019

Is this executor closed? If vert.x is in use, you may be able to leverage it for this purpose.

That's a better idea :). I've pushed a commit which removes the executor and uses this instead, and also the other changes that were requested.

Happy0 added a commit to openlawteam/scuttlebutt-akka-persistence that referenced this pull request Apr 15, 2019
Happy0 added a commit to openlawteam/scuttlebutt-akka-persistence that referenced this pull request Apr 16, 2019
@atoulme
Copy link
Contributor

atoulme commented Apr 17, 2019

Going to merge this if it works for you and port it to Tuweni.

@atoulme atoulme merged commit 8091b89 into ConsenSysMesh:master Apr 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants