Skip to content

Commit

Permalink
erts: Fix fragmented send to finish before exiting
Browse files Browse the repository at this point in the history
If a process is suspended doing a fragmented send and then
receives an exit signal it was terminated before it could
finish sending the message leading to a memory leak on the
receiving side.

This change fixes that so that the message is allowed to
finish being sent before the process exits.

Closes erlang#5876
  • Loading branch information
garazdawi committed Apr 14, 2022
1 parent f00827e commit 6c6c7b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion erts/emulator/beam/bif.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static Export* flush_monitor_messages_trap = NULL;
static Export* set_cpu_topology_trap = NULL;
static Export* await_port_send_result_trap = NULL;
Export* erts_format_cpu_topology_trap = NULL;
static Export dsend_continue_trap_export;
Export dsend_continue_trap_export;
Export *erts_convert_time_unit_trap = NULL;

static Export *await_msacc_mod_trap = NULL;
Expand Down
17 changes: 16 additions & 1 deletion erts/emulator/beam/erl_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -13684,6 +13684,7 @@ enum continue_exit_phase {
ERTS_CONTINUE_EXIT_CLEAN_SYS_TASKS,
ERTS_CONTINUE_EXIT_FREE,
ERTS_CONTINUE_EXIT_CLEAN_SYS_TASKS_AFTER,
ERTS_CONTINUE_EXIT_DIST_SEND,
ERTS_CONTINUE_EXIT_LINKS,
ERTS_CONTINUE_EXIT_MONITORS,
ERTS_CONTINUE_EXIT_LT_MONITORS,
Expand All @@ -13706,6 +13707,8 @@ struct continue_exit_state {
Uint32 block_rla_ref;
};

extern Export dsend_continue_trap_export;

void
erts_continue_exit_process(Process *p)
{
Expand Down Expand Up @@ -13897,6 +13900,9 @@ erts_continue_exit_process(Process *p)
/* notify free */
erts_atomic32_read_bor_relb(&p->state, ERTS_PSFLG_FREE);

/* clear suspend count */
p->rcount = 0;

trap_state->dep = ((p->flags & F_DISTRIBUTION)
? ERTS_PROC_SET_DIST_ENTRY(p, NULL)
: NULL);
Expand Down Expand Up @@ -13945,15 +13951,24 @@ erts_continue_exit_process(Process *p)
trap_state->pectxt.dist_state = NIL;
trap_state->pectxt.yield = 0;

if (p->current == &dsend_continue_trap_export.info.mfa) {
trap_state->pectxt.dist_state = p->arg_reg[0];
}

erts_proc_lock(p, ERTS_PROC_LOCK_MSGQ);

erts_proc_sig_fetch(p);

erts_proc_unlock(p, ERTS_PROC_LOCK_MSGQ);

trap_state->yield_state = NULL;
trap_state->phase = ERTS_CONTINUE_EXIT_LINKS;
trap_state->phase = ERTS_CONTINUE_EXIT_DIST_SEND;
if (reds <= 0) goto yield;
case ERTS_CONTINUE_EXIT_DIST_SEND:
if (is_not_nil(trap_state->pectxt.dist_state)) {
goto continue_dist_send;
}
trap_state->phase = ERTS_CONTINUE_EXIT_LINKS;
case ERTS_CONTINUE_EXIT_LINKS:

reds = erts_link_tree_foreach_delete_yielding(
Expand Down

0 comments on commit 6c6c7b6

Please sign in to comment.