<html><head><style type="text/css">.style1 {font-family: "Times New Roman";}</style></head><body>Hi Sverker,<BR>
<BR>
> On 21 Apr 2021, at 17:37, Sverker Eriksson <sverker.eriksson@ericsson.com> wrote:<BR>
><BR>
> At process termination the deallocation of the heap is delayed until a running dirty NIF has returned. This to make sure the dirty NIF can safely continue read its arguments.<BR>
<BR>
That makes things somewhat simpler;  thank you.<BR>
<BR>
> The firing of links and monitors (including NIF monitor down calls), on the other hand, are not delayed by a running dirty NIF.<BR>
<BR>
Is enif_monitor_process() serialised with respect to the delivery of<BR>
an exit signal by a concurrent thread?  I.e. if the function returns zero<BR>
and the caller is later killed then is it guaranteed that its down<BR>
callback will be invoked?  I am wondering if (absent any documentation<BR>
to the contrary) there might exist a race condition in which process A<BR>
might kill process B at exactly the time that the latter calls<BR>
enif_monitor_process(), e.g. in the (untested) code below.<BR>
<BR>
Regards,<BR>
<BR>
Robert<BR>
<BR>
<BR>
<BR>
/* resource object */<BR>
typedef struct foo {<BR>
int abort;<BR>
...<BR>
} foo_t;<BR>
<BR>
void<BR>
down(ErlNifEnv *env, void *obj, ErlNifPid *pid, ErlNifMonitor *mon)<BR>
{<BR>
((foo_t *)obj)->abort = 1;<BR>
}<BR>
<BR>
ERL_NIF_TERM<BR>
some_dirty_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])<BR>
{<BR>
foo_t *foop;<BR>
ErlNifPid pid;<BR>
ErlNifMonitor monitor;<BR>
<BR>
enif_get_resource(env, argv[0], foo_resource, (void **)&foop));<BR>
foop->abort = 0;<BR>
<BR>
enif_self(env, &pid);<BR>
if (enif_monitor_process(env, foop, pid, &monitor) > 0)<BR>
/* process already dead;  abort */<BR>
<BR>
/* lost a signal here? */<BR>
<BR>
while (/* some very long loop */) {<BR>
if (foop->abort) {<BR>
/* process waiting to die;  abort */<BR>
...<BR>
}<BR>
/* do stuff */<BR>
}<BR>
<BR>
/* no longer care about being directed to abort */<BR>
enif_demonitor_process(env, foop, &monitor);<BR>
/* return */<BR>
...<BR>
}<BR>
Confidentiality Notice | This email and any included attachments may be privileged, confidential and/or otherwise protected from disclosure. Access to this email by anyone other than the intended recipient is unauthorized. If you believe you have received this email in error, please contact the sender immediately and delete all copies. If you are not the intended recipient, you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.<BR>

<br><br><p style="font-family: Verdana; font-size:10pt; color:#666666;"><b>Disclaimer</b></p><p style="font-family: Verdana; font-size:8pt; color:#666666;">The information contained in this communication from the sender is confidential. It is intended solely for use by the recipient and others authorized to receive it. If you are not the recipient, you are hereby notified that any disclosure, copying, distribution or taking action in relation of the contents of this information is strictly prohibited and may be unlawful.<br><br>This email has been scanned for viruses and malware, and may have been automatically archived by Mimecast, a leader in email security and cyber resilience. Mimecast integrates email defenses with brand protection, security awareness training, web security, compliance and other essential capabilities. Mimecast helps protect large and small organizations from malicious activity, human error and technology failure; and to lead the movement toward building a more resilient world. To find out more, visit our website.</p></body></html>