From 4f7e00ef34345235f3b118749b5183a3f1ad5732 Mon Sep 17 00:00:00 2001 From: Max Romanov Date: Mon, 18 Sep 2017 17:35:24 +0300 Subject: Fixing shared memory thread safety issue. Do not reuse shared memory segment with different port until this segment successfully received and indexed on other side. However, segment can be used to transfer data via the port it was sent at any time. --- src/nxt_port_memory.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/nxt_port_memory.c') diff --git a/src/nxt_port_memory.c b/src/nxt_port_memory.c index 82d96004..ec4bbdf7 100644 --- a/src/nxt_port_memory.c +++ b/src/nxt_port_memory.c @@ -198,6 +198,8 @@ nxt_port_incoming_port_mmap(nxt_task_t *task, nxt_process_t *process, nxt_abort(); } + hdr->sent_over = 0xFFFFu; + fail: nxt_thread_mutex_unlock(&process->incoming_mutex); @@ -297,6 +299,7 @@ nxt_port_new_port_mmap(nxt_task_t *task, nxt_process_t *process, hdr->id = process->outgoing->nelts - 1; hdr->pid = process->pid; + hdr->sent_over = port->id; /* Mark first chunk as busy */ nxt_port_mmap_set_chunk_busy(hdr, 0); @@ -356,7 +359,9 @@ nxt_port_mmap_get(nxt_task_t *task, nxt_port_t *port, nxt_chunk_id_t *c, while (port_mmap < end_port_mmap) { - if (nxt_port_mmap_get_free_chunk(port_mmap->hdr, c)) { + if ( (port_mmap->hdr->sent_over == 0xFFFFu || + port_mmap->hdr->sent_over == port->id) && + nxt_port_mmap_get_free_chunk(port_mmap->hdr, c)) { hdr = port_mmap->hdr; goto unlock_return; -- cgit