<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">That's an interesting API function thanks for the pointer.<div class=""><br class=""></div><div class="">It's always possible that users can hang onto resources. Indeed I was once told a major telco had decided against using Erlang in some in house team because their top C++ programmer had tried it and found a way to leave a socket connection open once it had "gone out of scope".</div><div class=""><br class=""></div><div class="">Sean</div><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 25 Nov 2020, at 17:05, Sverker Eriksson <<a href="mailto:sverker.eriksson@ericsson.com" class="">sverker.eriksson@ericsson.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta charset="UTF-8" class=""><div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-US" class="">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 class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-US" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-US" class="">For the error case when a resource owning process crashes, a good alternative can be to use<span class="Apple-converted-space"> </span><a href="https://erlang.org/doc/man/erl_nif.html#enif_monitor_process" style="color: blue; text-decoration: underline;" class="">https://erlang.org/doc/man/erl_nif.html#enif_monitor_process</a><span class="Apple-converted-space"> </span>and reclaim the resource in the down callback.<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-US" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-US" class="">/Sverker, Erlang/OTP<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-US" class=""><o:p class=""> </o:p></span></div><div class=""><div style="border-style: solid none none; border-top-width: 1pt; border-top-color: rgb(225, 225, 225); padding: 3pt 0cm 0cm;" class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><b class=""><span lang="EN-US" class="">From:</span></b><span lang="EN-US" class=""><span class="Apple-converted-space"> </span>Sean Hinde <<a href="mailto:sean.hinde@mac.com" class="">sean.hinde@mac.com</a>><span class="Apple-converted-space"> </span><br class=""><b class="">Sent:</b><span class="Apple-converted-space"> </span>den 25 november 2020 11:36<br class=""><b class="">To:</b><span class="Apple-converted-space"> </span>Jesper Louis Andersen <<a href="mailto:jesper.louis.andersen@gmail.com" class="">jesper.louis.andersen@gmail.com</a>><br class=""><b class="">Cc:</b><span class="Apple-converted-space"> </span>Sverker Eriksson <<a href="mailto:sverker.eriksson@ericsson.com" class="">sverker.eriksson@ericsson.com</a>>; Erlang Questions <<a href="mailto:erlang-questions@erlang.org" class="">erlang-questions@erlang.org</a>><br class=""><b class="">Subject:</b><span class="Apple-converted-space"> </span>Re: Are NIF resources meant to be destroyed immediately on GC?<o:p class=""></o:p></span></div></div></div><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">I agree. My use case for this was building a nif for the oracle oci bindings.<o:p class=""></o:p></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">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 class=""></o:p></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">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 class=""></o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">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 class=""></o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">Sean<o:p class=""></o:p></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><br class=""><br class=""><o:p class=""></o:p></div><blockquote style="margin-top: 5pt; margin-bottom: 5pt;" class=""><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">On 25 Nov 2020, at 11:23, Jesper Louis Andersen <<a href="mailto:jesper.louis.andersen@gmail.com" style="color: blue; text-decoration: underline;" class="">jesper.louis.andersen@gmail.com</a>> wrote:<o:p class=""></o:p></div></div><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div><div class=""><div class=""><div class=""><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-family: Arial, sans-serif;" class="">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 class=""></o:p></span></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-family: Arial, sans-serif;" class=""><o:p class=""> </o:p></span></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-family: Arial, sans-serif;" class="">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 class=""></o:p></span></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-family: Arial, sans-serif;" class=""><o:p class=""> </o:p></span></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-family: Arial, sans-serif;" class="">[0] <a href="https://en.wikipedia.org/wiki/Ephemeron" style="color: blue; text-decoration: underline;" class="">https://en.wikipedia.org/wiki/Ephemeron</a><o:p class=""></o:p></span></div></div></div><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div><div class=""><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">On Wed, Nov 25, 2020 at 10:54 AM Sean Hinde <<a href="mailto:sean.hinde@mac.com" style="color: blue; text-decoration: underline;" class="">sean.hinde@mac.com</a>> wrote:<o:p class=""></o:p></div></div><blockquote style="border-style: none none none solid; border-left-width: 1pt; border-left-color: rgb(204, 204, 204); padding: 0cm 0cm 0cm 6pt; margin-left: 4.8pt; margin-right: 0cm;" class=""><div class=""><div class=""><blockquote style="margin-top: 5pt; margin-bottom: 5pt;" class=""><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 9pt; font-family: Helvetica, sans-serif;" class=""><o:p class=""> </o:p></span></div><div class=""><div style="margin-left: 65.2pt;" class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">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 class=""></o:p></div></div></div><div class=""><div style="margin-left: 65.2pt;" class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""> <o:p class=""></o:p></div></div></div><div class=""><div style="margin-left: 65.2pt;" class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">Super cool!<o:p class=""></o:p></div></div></div><div class=""><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""> <o:p class=""></o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-US" class="">Sorry to disappoint, but no.</span><o:p class=""></o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-US" class="">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 class=""></o:p></div></div></div></div></blockquote><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">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 class=""></o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">Sean<o:p class=""></o:p></div></div></div><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div></div></blockquote></div><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><br clear="all" class=""><o:p class=""></o:p></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><o:p class=""> </o:p></div></div><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">--<span class="Apple-converted-space"> </span><o:p class=""></o:p></div><div class=""><div style="margin: 0cm 0cm 0.0001pt 65.2pt; font-size: 11pt; font-family: Calibri, sans-serif;" class="">J.</div></div></div></div></blockquote></div></div></div></div></div></blockquote></div><br class=""></div></body></html>