Improve thread-safety of RPC connections
I use JRPC extensively in some custom webpages. mhttpd is multi-threaded, and I observed race conditions with multiple threads talking to the same RPC client at the same time. One thread would close the connection before the other thread had finished its call.
In 9f0cc5a I added a mutex so that only one mhttpd thread would talk to a client at once. In the discussion of that commit, KO has suggested that we should instead just make the whole RPC system thread-safe.
Comments (10)
-
-
proposed solution: change the list of open connections to std::vector, so the number of connections is unlimited. protect this list with a mutex, this makes rpc_client_connect() and rpc_client_call() thread safe. not sure if we prefer to have connections closed or left open (multithreaded transitions run faster).
K.O.
-
Ok, There is a big problem. rpc_client_call() & co are not thread safe and the rpc_connect_client() & rpc_client_call() & rpc_disconnect_client() API is not thread safe against different threads connecting and disconnecting at the same time as RPC calls are in progress. K.O.
-
the “jrpc” method in mjsonrpc.cxx is incorrectly labeled as thread-safe. It should have been labeled “needs locking”. K.O.
-
recoded rpc_client_connect(), rpc_client_call() & co for thread safety and unlimited number of connections. Will review, retest and commit probably tomorrow. More likely Saturday on Sunday. It will go to the midas-2020-12 release branch for testing on the alpha-g system. K.O.
-
the updated RPC code works well in the ALPHA-g system. I will merge it into the develop branch soon. K.O.
-
rpc code is now thread safe. workaround should be remove from mjsonrpc.cxx. sending it to ben. K.O.
-
impossible to assign this bug to bsmith@triumf.ca. wtf. K.O.
-
reporter -
assigned issue to
-
assigned issue to
-
reporter - changed status to resolved
- Log in to comment
the RPC system is almost thread safe. mserver connection is thread safe. but it looks like there are problems with rpc_client_connect() and rpc_client_call(). By luck we do not see these problems in the multithreaded transition code.