Re: Alarm signal and recv system call

Matthew Fluet <[email protected]> Thu, 15 Nov 2018 16:57:32 -0500
Newsgroups gmane.comp.lang.ml.mlton.user
Message-ID <CAMrhFL73+gAMX_FX_QL-xQWRt5W6PwSWtANOTN+jwJbVRX+euQ@mail.gmail.com>
--===============7241781668761474021==
Content-Type: multipart/alternative; boundary="000000000000ed09d2057abb2320"

--000000000000ed09d2057abb2320
Content-Type: text/plain; charset="UTF-8"

In addition to Henry's comments, there is a little more to the story.  It
is true that at the libc level, an interrupted system call will typically
return immediately with -1 and errno set to EINTR.  MLton's implementation
of the Basis Library chooses to restart most such libc functions.  However,
this is configurable via `val MLton.Signal.restart : bool ref`.  See the
"Interruptible System Calls" section of http://mlton.org/MLtonSignal.  The
default (of restarting system calls) makes sense for some uses of signals
(e.g., profiling or asking a long-running program to save state in response
to a periodic signal).  Also, in the context of high-level operations,
performing something like BinIO.output with a very large output vector
could result in multiple system calls (as buffers are filled), so it may be
almost impossible to handle the OS.SysError exception and resume so that no
output is lost.

On Thu, Nov 15, 2018 at 11:32 AM Henry Cejtin <[email protected]>
wrote:

> You  have  to  remember  that  MLton  signal  handlers  are running in a
> separate thread.  It gets passed the thread  that  was  interrupted  and
> returns  the  thread to run when the handler is finished.  The result of
> Handler.simple returns the thread it was called with, and hence restarts
> the interrupted thread.
>
> If  you  want the Socket.recvVec (or whatever) to be `aborted', then you
> can't use Handler.simple to make the handler.  Instead you have to  have
> some  other  thread to resume, or, if you want the program to terminate,
> you have to terminate it in the handler.  You can do  the  latter  with,
> for example, OS.Process.exit.
>
> Doing this, your sample program would look like this:
>
>     fun h = let val () = print "Alarm sig\n"
>             in OS.Process.exit OS.Process.failure
>             end
>     val () = MLton.Signal.setHandler (Posix.Signal.arm,
>                                       MLton.Signal.Handler.simple h)
>     val () = Posix.Process.Alarm (Time.fromSeconds (Int.toLarge 3))
>     val r = Socket.recvVec (socket, 1024)
>     val () = print "Done\n"
> On Thu, Nov 15, 2018 at 5:54 AM Kostirya <[email protected]> wrote:
> >
> > I was surprised to find that Alarm signal do not abort recvVec function.
> > How is this possible?
> > I thought recv was always aborted on signal.
> >
> >
> > I use for test:
> >
> > val _ = MLton.Signal.setHandler(Posix.Signal.alrm,
> > MLton.Signal.Handler.simple (fn _ => print "Alarm sig\n"))
> > val _ = Posix.Process.alarm (Time.fromSeconds (Int.toLarge 3))
> > val r = Socket.recvVec (socket, 1024)
> > val _ = print "Done\n"
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups "MLton-user" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected].
> >
> >
> >
> > _______________________________________________
> > MLton-user mailing list
> > [email protected]; [email protected]
> > https://lists.sourceforge.net/lists/listinfo/mlton-user
>
> --
> You received this message because you are subscribed to the Google Groups
> "MLton-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
>
>
>
> _______________________________________________
> MLton-user mailing list
> [email protected]; [email protected]
> https://lists.sourceforge.net/lists/listinfo/mlton-user
>

-- 
You received this message because you are subscribed to the Google Groups "MLton-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

--000000000000ed09d2057abb2320
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><div class=3D"gmail_default" style=3D"fon=
t-family:&quot;courier new&quot;,monospace;font-size:large">In addition to =
Henry&#39;s comments, there is a little more to the story.=C2=A0 It is true=
 that at the libc level, an interrupted system call will typically return i=
mmediately with -1 and errno set to EINTR.=C2=A0 MLton&#39;s implementation=
 of the Basis Library chooses to restart most such libc functions.=C2=A0 Ho=
wever, this is configurable via `val MLton.Signal.restart : bool ref`.=C2=
=A0 See the &quot;Interruptible System Calls&quot; section of=C2=A0<a href=
=3D"http://mlton.org/MLtonSignal">http://mlton.org/MLtonSignal</a>.=C2=A0 T=
he default (of restarting system calls) makes sense for some uses of signal=
s (e.g., profiling or asking a long-running program to save state in respon=
se to a periodic signal).=C2=A0 Also, in the context of high-level operatio=
ns, performing something like BinIO.output with a very large output vector =
could result in multiple system calls (as buffers are filled), so it may be=
 almost impossible to handle the OS.SysError exception and resume so that n=
o output is lost.</div><br><div class=3D"gmail_quote"><div dir=3D"ltr">On T=
hu, Nov 15, 2018 at 11:32 AM Henry Cejtin &lt;<a href=3D"mailto:henry.cejti=
[email protected]">[email protected]</a>&gt; wrote:<br></div><blockquote cla=
ss=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid =
rgb(204,204,204);padding-left:1ex">You=C2=A0 have=C2=A0 to=C2=A0 remember=
=C2=A0 that=C2=A0 MLton=C2=A0 signal=C2=A0 handlers=C2=A0 are running in a<=
br>
separate thread.=C2=A0 It gets passed the thread=C2=A0 that=C2=A0 was=C2=A0=
 interrupted=C2=A0 and<br>
returns=C2=A0 the=C2=A0 thread to run when the handler is finished.=C2=A0 T=
he result of<br>
Handler.simple returns the thread it was called with, and hence restarts<br=
>
the interrupted thread.<br>
<br>
If=C2=A0 you=C2=A0 want the Socket.recvVec (or whatever) to be `aborted&#39=
;, then you<br>
can&#39;t use Handler.simple to make the handler.=C2=A0 Instead you have to=
=C2=A0 have<br>
some=C2=A0 other=C2=A0 thread to resume, or, if you want the program to ter=
minate,<br>
you have to terminate it in the handler.=C2=A0 You can do=C2=A0 the=C2=A0 l=
atter=C2=A0 with,<br>
for example, OS.Process.exit.<br>
<br>
Doing this, your sample program would look like this:<br>
<br>
=C2=A0 =C2=A0 fun h =3D let val () =3D print &quot;Alarm sig\n&quot;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 in OS.Process.exit OS.Process.fai=
lure<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 end<br>
=C2=A0 =C2=A0 val () =3D MLton.Signal.setHandler (Posix.Signal.arm,<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 MLton.Signal.Ha=
ndler.simple h)<br>
=C2=A0 =C2=A0 val () =3D Posix.Process.Alarm (Time.fromSeconds (Int.toLarge=
 3))<br>
=C2=A0 =C2=A0 val r =3D Socket.recvVec (socket, 1024)<br>
=C2=A0 =C2=A0 val () =3D print &quot;Done\n&quot;<br>
On Thu, Nov 15, 2018 at 5:54 AM Kostirya &lt;<a href=3D"mailto:kostirya@gma=
il.com" target=3D"_blank">[email protected]</a>&gt; wrote:<br>
&gt;<br>
&gt; I was surprised to find that Alarm signal do not abort recvVec functio=
n.<br>
&gt; How is this possible?<br>
&gt; I thought recv was always aborted on signal.<br>
&gt;<br>
&gt;<br>
&gt; I use for test:<br>
&gt;<br>
&gt; val _ =3D MLton.Signal.setHandler(Posix.Signal.alrm,<br>
&gt; MLton.Signal.Handler.simple (fn _ =3D&gt; print &quot;Alarm sig\n&quot=
;))<br>
&gt; val _ =3D Posix.Process.alarm (Time.fromSeconds (Int.toLarge 3))<br>
&gt; val r =3D Socket.recvVec (socket, 1024)<br>
&gt; val _ =3D print &quot;Done\n&quot;<br>
&gt;<br>
&gt; --<br>
&gt; You received this message because you are subscribed to the Google Gro=
ups &quot;MLton-user&quot; group.<br>
&gt; To unsubscribe from this group and stop receiving emails from it, send=
 an email to <a href=3D"mailto:mlton-user%[email protected]" target=
=3D"_blank">[email protected]</a>.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; MLton-user mailing list<br>
&gt; <a href=3D"mailto:[email protected]" target=3D"_blank">=
[email protected]</a>; <a href=3D"mailto:[email protected]=
g" target=3D"_blank">[email protected]</a><br>
&gt; <a href=3D"https://lists.sourceforge.net/lists/listinfo/mlton-user" re=
l=3D"noreferrer" target=3D"_blank">https://lists.sourceforge.net/lists/list=
info/mlton-user</a><br>
<br>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;MLton-user&quot; group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:mlton-user%[email protected]" target=3D"_bl=
ank">[email protected]</a>.<br>
<br>
<br>
<br>
_______________________________________________<br>
MLton-user mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">MLton=
[email protected]</a>; <a href=3D"mailto:[email protected]" ta=
rget=3D"_blank">[email protected]</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/mlton-user" rel=3D"=
noreferrer" target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/=
mlton-user</a><br>
</blockquote></div></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;MLton-user&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]">mlton-user+unsu=
[email protected]</a>.<br />

--000000000000ed09d2057abb2320--


--===============7241781668761474021==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============7241781668761474021==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
MLton-user mailing list
[email protected]; [email protected]
https://lists.sourceforge.net/lists/listinfo/mlton-user

--===============7241781668761474021==--