<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 correct.
<div class=""><br class="">
<div class="">The kind of magic that would be required here would have to make the platform much more high-level.</div>
<div class=""><br class="">
</div>
<div class="">On the other hand, some languages — Clojure maybe? — might be restrictive enough in their control of side-effects, or at least have sufficiently useful subsets that are restrictive enough, that they could consider having their compilers automatically
 emit interrupting checks into the bytecode they generate.</div>
<div class=""><br class="">
</div>
<div class="">— Ron<br class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On 27 Jul 2022, at 19:36, <a href="mailto:eric@kolotyluk.net" class="">
eric@kolotyluk.net</a> wrote:</div>
<br class="Apple-interchange-newline">
<div 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: 400; 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; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span class="">Thanks for that clarity, Ron.<o:p class=""></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span class="">So, as always, use existing best practices to make sure that tasks and subtasks are robust in handling cancellation, interrupts, exceptions, etc.<o:p class=""></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span class="">Loom does not bring any new magic here…<o:p class=""></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span class=""><o:p class=""> </o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span class="">Cheers, Eric<o:p class=""></o:p></span></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span 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; 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>Ron Pressler <<a href="mailto:ron.pressler@oracle.com" style="color: blue; text-decoration: underline;" class="">ron.pressler@oracle.com</a>><span class="Apple-converted-space"> </span><br class="">
<b class="">Sent:</b><span class="Apple-converted-space"> </span>July 27, 2022 10:17 AM<br class="">
<b class="">To:</b><span class="Apple-converted-space"> </span>Eric Kolotyluk <<a href="mailto:eric@kolotyluk.net" style="color: blue; text-decoration: underline;" class="">eric@kolotyluk.net</a>><br class="">
<b class="">Cc:</b><span class="Apple-converted-space"> </span><a href="mailto:loom-dev@openjdk.java.net" style="color: blue; text-decoration: underline;" class="">loom-dev@openjdk.java.net</a><br class="">
<b class="">Subject:</b><span class="Apple-converted-space"> </span>Re: Unclear on close()<o:p class=""></o:p></span></div>
</div>
</div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div class="">
<div style="margin: 0cm; 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="" type="cite">
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
On 27 Jul 2022, at 17:35,<span class="Apple-converted-space"> </span><a href="mailto:eric@kolotyluk.net" style="color: blue; text-decoration: underline;" class="">eric@kolotyluk.net</a><span class="Apple-converted-space"> </span>wrote:<o:p class=""></o:p></div>
</div>
<div class="">
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
 <o:p class=""></o:p></div>
</div>
<ol start="1" type="1" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<li class="MsoListParagraph" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
Is there any scenario where close() waits forever?<o:p class=""></o:p></li></ol>
<ol start="1" type="1" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<ol start="1" type="a" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<li class="MsoListParagraph" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
Where it is implicit in this try block.<o:p class=""></o:p></li><li class="MsoListParagraph" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
I can imagine scenarios where subtasks don’t cancel properly or respond correctly to interrupts.<o:p class=""></o:p></li></ol>
</ol>
</div>
</blockquote>
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Yes, that could happen. It is a property of very general languages, like Java, and there’s no getting around it. It takes a very carefully-controlled language, like Erlang, to support the forceful (non-cooperative) termination of a thread, and even there things
 could go wrong unless some discipline is followed.<o:p class=""></o:p></div>
</div>
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
The core of the issue is that waiting for a thread to terminate is the least of your worries. It is more important to ensure that threads maintain your program’s logical invariants, and so we must ensure threads are terminated when they decide they’re ready.
 This requires their cooperation. So we can only ever *ask* for a thread to terminate; we can’t kill it in a way that safely maintains program invariants.<o:p class=""></o:p></div>
</div>
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<blockquote style="margin-top: 5pt; margin-bottom: 5pt;" class="" type="cite">
<div class="">
<ol start="2" type="1" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<li class="MsoListParagraph" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
If there is, is there any programmatic way out of this?<o:p class=""></o:p></li></ol>
<ol start="2" type="1" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<ol start="1" type="a" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<li class="MsoListParagraph" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
Does the InterruptedException bypass close() and exit the try block?<o:p class=""></o:p></li><li class="MsoListParagraph" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
Is this guaranteed by the runtime?<o:p class=""></o:p></li><li class="MsoListParagraph" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
I assume it is, but I have made bad assumptions about the runtime before…<o:p class=""></o:p></li></ol>
</ol>
</div>
</blockquote>
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
An exception could not bypass the close if used in a try-with-resources block, but because we rely on try-with-resources, which is optional (i.e. you could neglect to use the construct altogether), there are ways to write code that doesn’t call close. The runtime
 would, currently, only detect that if this interferes with other things that rely on correct nesting of scopes. So, e.g. if you don’t close a scope but then close an enclosing scope, that will be detected.<o:p class=""></o:p></div>
</div>
<div style="margin: 0cm; 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="" type="cite">
<div class="">
<ol start="3" type="1" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<ol start="3" type="a" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<li class="MsoListParagraph" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<o:p class=""> </o:p></li></ol>
</ol>
<ol start="4" type="1" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<li class="MsoListParagraph" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
Personally, I would have thought that “<span style="font-size: 10pt; font-family: "Courier New";" class="">scope.joinUntil(deadline);</span>” would guarantee this code exits the try block, but the documentation, as written, does not give me that confidence.
 There are two wait points…<o:p class=""></o:p></li></ol>
<ol start="4" type="1" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<ol start="1" type="a" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<li class="MsoListParagraph" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size: 10pt; font-family: "Courier New";" class="">scope.joinUntil(deadline);</span><o:p class=""></o:p></li><li class="MsoListParagraph" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size: 10pt; font-family: "Courier New";" class="">scope.close();<span class="apple-converted-space"> </span></span><o:p class=""></o:p></li></ol>
</ol>
<ol start="5" type="1" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<li class="MsoListParagraph" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
While this may not be ambiguous to others, it is to me.<o:p class=""></o:p></li></ol>
<ol start="5" type="1" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<ol start="1" type="a" style="margin-bottom: 0cm; margin-top: 0cm;" class="">
<li class="MsoListParagraph" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
It would be nice if there was text that made this more explicit.<o:p class=""></o:p></li></ol>
</ol>
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
 <o:p class=""></o:p></div>
</div>
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Cheers, Eric<o:p class=""></o:p></div>
</div>
</div>
</blockquote>
</div>
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
close can only return after all threads have terminated (except, maybe, due to a VM error). join/joinUntil wait for forks to: 1. terminate OR 2. be cancelled due to shutdown OR 3. until the waiting thread is interrupted or the timeout expires.<o:p class=""></o:p></div>
</div>
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Whatever happens, close waits for all forked threads to fully terminate.<o:p class=""></o:p></div>
</div>
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
— Ron</div>
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</div>
</body>
</html>