<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Helvetica;
        panose-1:2 11 6 4 2 2 2 2 2 4;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=SV link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>I fully agree with Jesper about not relying on GC for resource reclamation. An additional risk is that the resource does not get GC’ed at all if its reference has “leaked” away onto the heap of another process, like for example a logger or an io-server process.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>For the error case when a resource owning process crashes, a good alternative can be to use <a href="https://erlang.org/doc/man/erl_nif.html#enif_monitor_process">https://erlang.org/doc/man/erl_nif.html#enif_monitor_process</a> and reclaim the resource in the down callback.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>/Sverker, Erlang/OTP<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'><o:p> </o:p></span></p><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal style='margin-left:65.2pt'><b><span lang=EN-US>From:</span></b><span lang=EN-US> Sean Hinde <sean.hinde@mac.com> <br><b>Sent:</b> den 25 november 2020 11:36<br><b>To:</b> Jesper Louis Andersen <jesper.louis.andersen@gmail.com><br><b>Cc:</b> Sverker Eriksson <sverker.eriksson@ericsson.com>; Erlang Questions <erlang-questions@erlang.org><br><b>Subject:</b> Re: Are NIF resources meant to be destroyed immediately on GC?<o:p></o:p></span></p></div></div><p class=MsoNormal style='margin-left:65.2pt'><o:p> </o:p></p><p class=MsoNormal style='margin-left:65.2pt'>I agree. My use case for this was building a nif for the oracle oci bindings.<o:p></o:p></p><div><p class=MsoNormal style='margin-left:65.2pt'><o:p> </o:p></p></div><div><p class=MsoNormal style='margin-left:65.2pt'>In the happy path users should call the provided db_close function which would flush outstanding transactions and close everything down in the dirty nif.<o:p></o:p></p><div><p class=MsoNormal style='margin-left:65.2pt'><o:p> </o:p></p></div><div><p class=MsoNormal style='margin-left:65.2pt'>The problematic case is where the user's process holding the connection crashes. In that case we need the finaliser as the "secondary" resource reclamation path. If that path cannot safely close the db handle for fear of screwing up the emulator it's a problem.<o:p></o:p></p></div><div><p class=MsoNormal style='margin-left:65.2pt'><o:p> </o:p></p></div><div><p class=MsoNormal style='margin-left:65.2pt'>The solution today is to have an interim process to fault isolate the nif holding process from the user process. Which adds overhead.<o:p></o:p></p></div><div><p class=MsoNormal style='margin-left:65.2pt'><o:p> </o:p></p></div><div><p class=MsoNormal style='margin-left:65.2pt'>Sean<o:p></o:p></p><div><p class=MsoNormal style='margin-left:65.2pt'><br><br><o:p></o:p></p><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal style='margin-left:65.2pt'>On 25 Nov 2020, at 11:23, Jesper Louis Andersen <<a href="mailto:jesper.louis.andersen@gmail.com">jesper.louis.andersen@gmail.com</a>> wrote:<o:p></o:p></p></div><p class=MsoNormal style='margin-left:65.2pt'><o:p> </o:p></p><div><div><div><div><p class=MsoNormal style='margin-left:65.2pt'><span style='font-family:"Arial",sans-serif'>As a general rule: never use finalizers for the primary resource reclamation path. You don't know when they'll run. You don't know what changes are made to a GC in the future, that alters the rule set slightly. Use them as a safety measure if something goes out of scope. Resources such as DB connections can be very limited in scope (less than 50 for a typical non pg_bouncer postgres setup). So you have to manage them quite explicitly. Memory is far more abundant, and it is also fungible to a large extent, so you just need a block somewhere.<o:p></o:p></span></p></div><div><p class=MsoNormal style='margin-left:65.2pt'><span style='font-family:"Arial",sans-serif'><o:p> </o:p></span></p></div><div><p class=MsoNormal style='margin-left:65.2pt'><span style='font-family:"Arial",sans-serif'>This is true in *any* GC language I've worked with over the years. There may come something along which completely changes the landscape, but it hasn't happened yet. It's more in the other direction (see e.g., Ephemerons[0]) that things are progressing.<o:p></o:p></span></p></div><div><p class=MsoNormal style='margin-left:65.2pt'><span style='font-family:"Arial",sans-serif'><o:p> </o:p></span></p></div><div><p class=MsoNormal style='margin-left:65.2pt'><span style='font-family:"Arial",sans-serif'>[0] <a href="https://en.wikipedia.org/wiki/Ephemeron">https://en.wikipedia.org/wiki/Ephemeron</a><o:p></o:p></span></p></div></div><p class=MsoNormal style='margin-left:65.2pt'><o:p> </o:p></p><div><div><p class=MsoNormal style='margin-left:65.2pt'>On Wed, Nov 25, 2020 at 10:54 AM Sean Hinde <<a href="mailto:sean.hinde@mac.com">sean.hinde@mac.com</a>> wrote:<o:p></o:p></p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm'><div><div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal style='margin-left:65.2pt'><span style='font-size:9.0pt;font-family:"Helvetica",sans-serif'><o:p> </o:p></span></p><div><div style='margin-left:65.2pt'><p class=MsoNormal style='margin-left:65.2pt'>This a really nice change. It opens the door to resource destructors being safe for closing external resources like database handles that might take a while.<o:p></o:p></p></div></div><div><div style='margin-left:65.2pt'><p class=MsoNormal style='margin-left:65.2pt'> <o:p></o:p></p></div></div><div><div style='margin-left:65.2pt'><p class=MsoNormal style='margin-left:65.2pt'>Super cool!<o:p></o:p></p></div></div><div><div><p class=MsoNormal style='margin-left:65.2pt'> <o:p></o:p></p></div><div><p class=MsoNormal style='margin-left:65.2pt'><span lang=EN-US>Sorry to disappoint, but no.</span><o:p></o:p></p></div><div><p class=MsoNormal style='margin-left:65.2pt'><span lang=EN-US>By ”scheduled” I just meant “put in run queue to be executed at a later time”. Destructors are still executed by a normal scheduler threads and not dirty schedulers.</span><o:p></o:p></p></div></div></div></blockquote><div><p class=MsoNormal style='margin-left:65.2pt'><o:p> </o:p></p></div><div><p class=MsoNormal style='margin-left:65.2pt'>Ah well! I still think it would be pretty handy. It would make that kind of code much easier to get right, safer, and potentially more efficient.<o:p></o:p></p></div><div><p class=MsoNormal style='margin-left:65.2pt'><o:p> </o:p></p></div><div><p class=MsoNormal style='margin-left:65.2pt'>Sean<o:p></o:p></p></div></div><p class=MsoNormal style='margin-left:65.2pt'><o:p> </o:p></p></div></blockquote></div><p class=MsoNormal style='margin-left:65.2pt'><br clear=all><o:p></o:p></p><div><p class=MsoNormal style='margin-left:65.2pt'><o:p> </o:p></p></div><p class=MsoNormal style='margin-left:65.2pt'>-- <o:p></o:p></p><div><p class=MsoNormal style='margin-left:65.2pt'>J.<o:p></o:p></p></div></div></div></blockquote></div><p class=MsoNormal style='margin-left:65.2pt'><o:p> </o:p></p></div></div></div></body></html>