- if (uatomic_read(&relayd->refcount) == 0) {
- /* Refcount of the relayd struct is 0, destroy it */
+
+ ret = relayd_send_close_stream(&relayd->control_sock,
+ stream->relayd_stream_id,
+ stream->next_net_seq_num - 1);
+ if (ret < 0) {
+ ERR("Unable to close stream on the relayd. Continuing");
+ /* Continue here. There is nothing we can do for the relayd.*/
+ }
+
+ /* Both conditions are met, we destroy the relayd. */
+ if (uatomic_read(&relayd->refcount) == 0 &&
+ uatomic_read(&relayd->destroy_flag)) {