<div dir="ltr">
<p style="margin-bottom:0in;line-height:100%">Thank your for
reply. I think I should clarify what exactly I'd like to discuss.</p>
<p style="margin-bottom:0in;line-height:100%">The “data
structures” I'm talking about are in general single-threaded
mutable containers (like mentioned hashtables, or like ArrayList in
Java). Such structures are not thread safe, yet it would be nice to
have async exception safety. The word “atomicity” I used in a
sense mentioned here
<a href="https://en.wikipedia.org/wiki/Atomicity_(database_systems">https://en.wikipedia.org/wiki/Atomicity_(database_systems</a>)
: operation either occurs or fails and data structure remains in
previous state. In many cases such behavior can be achieved without
complex exception clean-up routines.</p>
<p style="margin-bottom:0in;line-height:100%">Let me give an
example. Consider something like ArrayList from Java (an vector which
can grow while elements added). I want to implement 'add' action. The
contract is straightforward – the action may either add element to
structure, possibly reallocating underlying memory buffer, writing
element at last position, incrementing element counter, or it may
throw OutOfMemory exception. But in the latter case the structure
should stay “undamaged”. This could be implemented as following:</p>
<p style="margin-bottom:0in;line-height:100%">if
count_equals_capacity thenallocate_new_buffer (let's suppose it garbage-collected)</p><p style="margin-bottom:0in;line-height:100%">
copy_elements</p>
<p style="margin-bottom:0in;line-height:100%">
update_buffer_pointer</p>
<p style="margin-bottom:0in;line-height:100%">
update_capacity_variable</p>
<p style="margin-bottom:0in;line-height:100%">write_new_element_to_buffer</p>
<p style="margin-bottom:0in;line-height:100%">update_count_variable</p>
<p style="margin-bottom:0in;line-height:100%"><br>
</p>
<p style="margin-bottom:0in;line-height:100%">This code does not
contains any explicit exception handling but it satisfies the
contract. The only place there exception can occur is
allocate_new_buffer. In this case action will be interrupted before
any state modifications. All other operations are basically memory
writes and completely safe (assuming code correct and will not
segfault).</p>
<p style="margin-bottom:0in;line-height:100%">Things become
complicated in presence of async exceptions. Suppose async exception
raised between write_new_element_to_buffer and update_count_variable.
At first glance nothing wrong happed, but if the buffer holds
references, it will now contain a reference to some object,
preventing it from being GC-d, and this reference will be beyond
buffer's count value, because exception occurred before updating
count variable, so programmer will be completely unaware of it. But
this still can be fixed by masking exceptions in critical blocks. And
we can defenelly implement all of this in the IO monad.</p>
<p style="margin-bottom:0in;line-height:100%">The question is how
to write “monad polymorhic” code. i.e. code, which can run both
in IO and ST. Mutable data structures benefit from being “monad
polymorhic”. Most Haskell mutable containers (vectors, hashtables,
impure-containers) are build on PrimState monad allowing them run
both in IO and ST. But they seems just ignore the fact that async
exception may corrupt state. Some of them ( e.g.
<a href="https://hackage.haskell.org/package/impure-containers-0.4.0/docs/src/Data-ArrayList-Generic.html#ArrayList">https://hackage.haskell.org/package/impure-containers-0.4.0/docs/src/Data-ArrayList-Generic.html#ArrayList</a>
) seem even ignore that unsafeGrow may throw OutOfMemory (though
attempting to recover from OutOfMemory may be bad idea itself).</p></div><div class="gmail_extra"><br><div class="gmail_quote">2017-09-28 15:45 GMT+03:00 Michael Snoyman <span dir="ltr"><<a href="mailto:michael@snoyman.com" target="_blank">michael@snoyman.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><span class=""><div>> Since exception can arise at any point, it is not possible to guarantee
atomicity of operation, hence mutable data structure may remain in
incorrect state in case of interruption.</div><div><br></div></span><div>Even if async exceptions didn't exist, we couldn't guarantee atomicity in general without specifically atomic functions (like atomicModifyIORef or STM), since another thread may access the data concurrently and create a data race.</div><div><br></div><div>If you're only talking about single-threaded cases—of which ST is _basically_ a subset[1]—I don't think you're really worried about _atomicity_, but about exception safety. Exception safety goes beyond async exceptions, since almost all IO actions can throw some form of synchronous exception. For those cases, you can use one of the many exception-cleanup functions, like finally, onException, bracket, or bracketOnError.</div><div><br></div><div>It's true that those functions don't work inside ST, but I'd argue you don't need them to. The expected behavior of code that receives an async exception is to (1) clean up after itself and (2) rethrow the exception. But as ST blocks are supposed to be free of externally-visible side effects, worrying about putting its variables back into some safe state is unnecessary[2].</div><div><br></div><div>To summarize:</div><div><br></div><div>* If you need true atomicity, you're in IO and dealing with multiple threads. I'd recommend sticking with STM unless you have a strong reason to do otherwise.</div><div>* If you are single threaded and in IO, you can get away with non-STM stuff more easily, and need to make sure you're using exception-aware functions.</div><div>* If you're inside ST, make sure any resources you acquire are cleaned up correctly, but otherwise you needn't worry about exceptions.</div><div><br></div><div>Also, you may be interested in reading the documentation for safe-exceptions[3], which talks more about async exception safety.<br></div><div><br></div><div>[1] I say basically since you'd have to pull out unsafe functions to fork a thread that has access to an STVar or similar, though it could be done.</div><div>[2] If you're doing something like binding to a C library inside ST, you may have some memory cleanup to perform, but the STVars and other data structures should never be visible again.</div><div>[3] <a href="https://haskell-lang.org/library/safe-exceptions" target="_blank">https://haskell-lang.org/<wbr>library/safe-exceptions</a><br></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Thu, Sep 28, 2017 at 2:00 PM, Станислав Черничкин <span dir="ltr"><<a href="mailto:schernichkin@gmail.com" target="_blank">schernichkin@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr"><div>It's quite hard to implement mutable data structures in presence of asynchronous exceptions. Since exception can arise at any point, it is not possible to guarantee atomicity of operation, hence mutable data structure may remain in incorrect state in case of interruption. One can certainly use maskAsyncExceptions# and friends to protect critical regions, but masking function are living in IO, mutable data structures on other hand trend to be state-polymorphic (to allow it usage in ST).</div><div><br></div><div>This lead to conflicting requirements: </div><div>- One should not care about asynchronous exceptions inside ST (it is not possible to catch exception in ST, hence not possible to use something in invalid state). More over, it is not even possible to do write “exception-safe” code, because masking functions not available.</div><div>- One should provide accurate masking then using same data structures in IO.</div><div><br></div><div>So I want do discuss several questions topics on this case.</div><div><br></div><div>1. Impact. Are async exceptions really common? Would not be easier to say: “ok, things can go bad if you combine async exceptions with mutable data structures, just don't do it”. </div><div><br></div><div>2. Documentation. Should library authors explicitly mention async exceptions safety? For example <a href="https://hackage.haskell.org/package/hashtables" target="_blank">https://hackage.haskell.org/pa<wbr>ckage/hashtables</a> – is it async exceptions safe when used in IO? Or even worse <a href="https://hackage.haskell.org/package/ghc-prim-0.5.1.0/docs/GHC-Prim.html#v:resizeMutableByteArray-35-" target="_blank">https://hackage.haskell.org/pa<wbr>ckage/ghc-prim-0.5.1.0/docs/GH<wbr>C-Prim.html#v:resizeMutableByt<wbr>eArray-35-</a> - what will happened in case of async exception? This functions is sate-polimorphic, will it implicitly mask exceptions if used from IO?</div><div><br></div><div>3. Best practices. How should we deal with problem? Is creating separate versions of code for ST and IO is the only way? Probably it is possible to add “mask” to something like <a href="https://hackage.haskell.org/package/primitive-0.6.2.0/docs/Control-Monad-Primitive.html#t:PrimMonad" target="_blank">https://hackage.haskell.org/pa<wbr>ckage/primitive-0.6.2.0/docs/<wbr>Control-Monad-Primitive.html#<wbr>t:PrimMonad</a> emit mask in IO instance and NOOP in ST version? Or maybe somebody know better patterns for async exeption safe code?</div><span class="m_-7433608041285848226HOEnZb"><font color="#888888"><div><br></div>-- <br><div class="m_-7433608041285848226m_-4399805966142326916gmail_signature"><div dir="ltr"><span style="font-family:arial;font-size:small">Sincerely, Stanislav Chernichkin.</span><br></div></div>
</font></span></div>
<br></div></div><span class="">______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/haskell-caf<wbr>e</a><br>
Only members subscribed via the mailman list are allowed to post.<br></span></blockquote></div><br></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><span style="font-family:arial;font-size:small">Sincerely, Stanislav Chernichkin.</span><br></div></div>
</div>