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.
This commit is contained in:
Max Romanov
2017-09-18 17:35:24 +03:00
parent 75a6325656
commit 4f7e00ef34
4 changed files with 13 additions and 2 deletions

View File

@@ -106,6 +106,7 @@ nxt_go_new_port_mmap(nxt_go_process_t *process, nxt_port_id_t id)
hdr->id = process->outgoing.nelts - 1; hdr->id = process->outgoing.nelts - 1;
hdr->pid = process->pid; hdr->pid = process->pid;
hdr->sent_over = id;
/* Mark first chunk as busy */ /* Mark first chunk as busy */
nxt_port_mmap_set_chunk_busy(hdr, 0); nxt_port_mmap_set_chunk_busy(hdr, 0);
@@ -174,7 +175,9 @@ nxt_go_port_mmap_get(nxt_go_process_t *process, nxt_port_id_t port_id,
while (port_mmap < end_port_mmap) { 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; hdr = port_mmap->hdr;
goto unlock_return; goto unlock_return;

View File

@@ -142,6 +142,8 @@ nxt_go_new_incoming_mmap(nxt_pid_t pid, nxt_fd_t fd)
port_mmap->hdr->id, process->incoming.nelts - 1); port_mmap->hdr->id, process->incoming.nelts - 1);
} }
port_mmap->hdr->sent_over = 0xFFFFu;
fail: fail:
nxt_go_mutex_unlock(&process->incoming_mutex); nxt_go_mutex_unlock(&process->incoming_mutex);

View File

@@ -198,6 +198,8 @@ nxt_port_incoming_port_mmap(nxt_task_t *task, nxt_process_t *process,
nxt_abort(); nxt_abort();
} }
hdr->sent_over = 0xFFFFu;
fail: fail:
nxt_thread_mutex_unlock(&process->incoming_mutex); 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->id = process->outgoing->nelts - 1;
hdr->pid = process->pid; hdr->pid = process->pid;
hdr->sent_over = port->id;
/* Mark first chunk as busy */ /* Mark first chunk as busy */
nxt_port_mmap_set_chunk_busy(hdr, 0); 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) { 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; hdr = port_mmap->hdr;
goto unlock_return; goto unlock_return;

View File

@@ -49,6 +49,7 @@ typedef nxt_atomic_uint_t nxt_free_map_t;
struct nxt_port_mmap_header_s { struct nxt_port_mmap_header_s {
uint32_t id; uint32_t id;
nxt_pid_t pid; /* For sanity check. */ nxt_pid_t pid; /* For sanity check. */
nxt_port_id_t sent_over;
nxt_free_map_t free_map[MAX_FREE_IDX]; nxt_free_map_t free_map[MAX_FREE_IDX];
}; };