Improve thread-safety of RPC connections

Issue #299 resolved
Ben Smith created an issue

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)

  1. dd1

    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.

  2. dd1

    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.

  3. dd1

    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.

  4. dd1

    the “jrpc” method in mjsonrpc.cxx is incorrectly labeled as thread-safe. It should have been labeled “needs locking”. K.O.

  5. dd1

    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.

  6. dd1

    the updated RPC code works well in the ALPHA-g system. I will merge it into the develop branch soon. K.O.

  7. Log in to comment