From 9eb1633b028a827e55508f511ca080189508f799 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 27 Apr 2023 11:03:53 +0200 Subject: [PATCH] protocol-native: don't remove all fds in clear When we consumed all the buffer data, don't clear all the fds but only those that were already consumed in the message. It is possible that we already have fds for the next message and we don't want to discard those. Fixes some intermittend memory map errors. --- src/modules/module-protocol-native.c | 4 ++-- src/modules/module-protocol-native/connection.c | 15 +++++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/modules/module-protocol-native.c b/src/modules/module-protocol-native.c index ad6e53dd5..044f8e843 100644 --- a/src/modules/module-protocol-native.c +++ b/src/modules/module-protocol-native.c @@ -199,8 +199,8 @@ static void debug_msg(const char *prefix, const struct pw_protocol_native_messag { struct spa_pod *pod; pw_logt_debug(mod_topic_connection, - "%s: id:%d op:%d size:%d seq:%d", prefix, - msg->id, msg->opcode, msg->size, msg->seq); + "%s: id:%d op:%d size:%d seq:%d fds:%d", prefix, + msg->id, msg->opcode, msg->size, msg->seq, msg->n_fds); if ((pod = get_first_pod_from_data(msg->data, msg->size, 0)) != NULL) spa_debug_pod(0, NULL, pod); diff --git a/src/modules/module-protocol-native/connection.c b/src/modules/module-protocol-native/connection.c index efc896dc8..3c4eb639a 100644 --- a/src/modules/module-protocol-native/connection.c +++ b/src/modules/module-protocol-native/connection.c @@ -271,16 +271,22 @@ too_many_fds: static void clear_buffer(struct buffer *buf, bool fds) { uint32_t i; + + pw_log_debug("clear fds:%d", fds); if (fds) { for (i = 0; i < buf->n_fds; i++) { pw_log_debug("%p: close fd:%d", buf, buf->fds[i]); close(buf->fds[i]); } + buf->n_fds = 0; + buf->fds_offset = 0; + } else { + buf->n_fds -= SPA_MIN(buf->fds_offset, buf->n_fds); + memmove(buf->fds, &buf->fds[buf->fds_offset], buf->n_fds * sizeof(int)); + buf->fds_offset = 0; } - buf->n_fds = 0; buf->buffer_size = 0; buf->offset = 0; - buf->fds_offset = 0; } /** Prepare connection for calling from reentered context. @@ -711,8 +717,9 @@ pw_protocol_native_connection_end(struct pw_protocol_native_connection *conn, if (mod_topic_connection->level >= SPA_LOG_LEVEL_DEBUG) { pw_logt_debug(mod_topic_connection, - ">>>>>>>>> out: id:%d op:%d size:%d seq:%d", - buf->msg.id, buf->msg.opcode, size, buf->msg.seq); + ">>>>>>>>> out: id:%d op:%d size:%d seq:%d fds:%d", + buf->msg.id, buf->msg.opcode, size, buf->msg.seq, + buf->msg.n_fds); spa_debug_pod(0, NULL, SPA_PTROFF(p, impl->hdr_size, struct spa_pod)); pw_logt_debug(mod_topic_connection, ">>>>>>>>> out: done");