relay: fix potential thread hang on exit
To make sure there is no status message pending in the tx pipe, relay_cbus_detach() waits on relay->status_cond before proceeding to relay destruction. The status_cond is signaled by the status message completion routine (relay_status_update()) handled by cbus on the relay's side. The problem is by the time we call relay_cbus_detach(), the cbus loop has been stopped (see relay_subscribe_f()), i.e. there's no one to process the message that is supposed to signal status_cond. That means, if there happens to be a status message en route when the relay is stopped, the relay thread will hang forever. To fix this issue, let's introduce a new helper function, cbus_flush(), which blocks the caller until all cbus messages queued on a pipe have been processed, and use it in relay_cbus_detach() to wait for in-flight status messages to complete. Apart from source and destination pipes, this new function takes a callback to be used for processing incoming cbus messages, so it can be used even if the loop that is supposed to invoke cbus_process() stopped.
Loading
Please register or sign in to comment