From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 817B87F89E for ; Thu, 20 Mar 2014 21:07:25 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of info@gerd-stolpmann.de) identity=pra; client-ip=212.227.126.131; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="info@gerd-stolpmann.de"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of info@gerd-stolpmann.de) identity=mailfrom; client-ip=212.227.126.131; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="info@gerd-stolpmann.de"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of postmaster@moutng.kundenserver.de designates 212.227.126.131 as permitted sender) identity=helo; client-ip=212.227.126.131; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="postmaster@moutng.kundenserver.de"; x-conformance=sidf_compatible; x-record-type="v=spf1" X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtkBAEZJK1PU436DlGdsb2JhbABZg0GDXrgWhzKBGhYOAQEBAQcLCwkSKoIlAQEBAwEjBC4kBQsLGCoCAlcGEwmHaAwJrRuiSxeJI4UcJgeCLkGBSQSPIoc7iDEFjxI X-IPAS-Result: AtkBAEZJK1PU436DlGdsb2JhbABZg0GDXrgWhzKBGhYOAQEBAQcLCwkSKoIlAQEBAwEjBC4kBQsLGCoCAlcGEwmHaAwJrRuiSxeJI4UcJgeCLkGBSQSPIoc7iDEFjxI X-IronPort-AV: E=Sophos;i="4.97,697,1389740400"; d="asc'?scan'208";a="63869846" Received: from moutng.kundenserver.de ([212.227.126.131]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES128-SHA; 20 Mar 2014 21:07:25 +0100 Received: from office1.lan.sumadev.de (dslb-178-004-219-120.pools.arcor-ip.net [178.4.219.120]) by mrelayeu.kundenserver.de (node=mreue006) with ESMTP (Nemesis) id 0Me10b-1WlIUo331h-00Pa3B; Thu, 20 Mar 2014 21:07:23 +0100 Received: from [192.168.10.103] (ip-37-201-194-198.unitymediagroup.de [37.201.194.198]) by office1.lan.sumadev.de (Postfix) with ESMTPSA id 019F8DC270; Thu, 20 Mar 2014 21:07:22 +0100 (CET) Message-ID: <1395346042.27397.33.camel@zotac> From: Gerd Stolpmann To: ygrek Cc: caml-list Date: Thu, 20 Mar 2014 21:07:22 +0100 In-Reply-To: <20140320132941.09aa04f4@kiwi.local.tld> References: <532A0A5E.6070107@fugmann.net> <1395271296.27397.11.camel@zotac> <20140320132941.09aa04f4@kiwi.local.tld> Content-Type: multipart/signed; micalg="pgp-sha1"; protocol="application/pgp-signature"; boundary="=-O1Ump5GBMdGg9j5j76Yp" X-Mailer: Evolution 3.2.3-0ubuntu6 Mime-Version: 1.0 X-Provags-ID: V02:K0:GPInmugn7Sg6K9XnZ2C422+BEky/wL6OD3v4DBYVeLe TpKa4/ev/hSgWndPXv7E3W5QtPx4pE1PD2So+tp+xs5bqb/L6u JmufzYaEwzH3fA/fyO8lGjd/4g0QDX4gK+4ShGUajSw3aEqWDU /jtf5RU009xNP4+bMN3Lx0pqU30eapmMUpv/H5AA6mY85wLjZH oKmhjPoYzOdRl7MgmdlFVa433cE33zxKQZ2ltdKrZcgh2bgBxP asJ4n6xY5aucLBk6kSKY6P0ABM0yx38pRBc/z7ghSuT4DYTyCq a6vpo2Z33lcFpl26zBA8h32Os+Hq1aKlrpyOrEkRIBibE5Oyud G76aN/rFHoRZ0eYXWW1a3mydAcMLhkvqgPvOUxAdD Subject: Re: [Caml-list] Bug when printing from signal handlers? --=-O1Ump5GBMdGg9j5j76Yp Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Am Donnerstag, den 20.03.2014, 13:29 +0800 schrieb ygrek: > On Thu, 20 Mar 2014 00:21:36 +0100 > Gerd Stolpmann wrote: >=20 > > OCaml printing isn't printf in C - it calls directly write() and is > > always possible. OCaml signal handlers aren't signal handlers from the C > > viewpoint: When the signal is caught, a flag in the OCaml runtime is > > set, and this flag is regularly checked by the running code. (I.e. what > > you suggest is already done in the runtime.) > >=20 > > So, I'd say this is a bug in the OCaml runtime. The bug goes away when > > you print to a different channel from the signal handler, so it looks > > like channels and signal handlers have some unwanted effect on each > > other. >=20 > stdlib channels are protected with non-recursive mutex, so the deadlock o= n re-entrant invocation is guaranteed.=20 No, there aren't any mutexes here involved - the failing program is single-threaded. > AFAICS runtime system tries to execute signal immediately (see signal_han= dle in asmrun/signals_asm.c) > and if that is not possible - records signal for later execution. Right, this handles the case that the current thread is doing a blocking system call (sorry, forgot this case). Apparently, the OCaml code is then run directly from the signal handler. > Anyway doing complex stuff in signal handler is a bad idea, because even = with delayed processing (when > things are safe from the libc point of view) the points of invocation of = OCaml signal handler are scattered > all around the program (allocation sites) and any OCaml resource that doe= sn't support reentrant usage will break > the program. Anyway, I don't think this has anything to do with calling non-signal-safe libc functions (so far I can see the only called function is write()). It is most likely because flush isn't reentrant: caml_flush() calls caml_flush_partial() which in turn calls do_write(). The signal arrives during the write() syscall, and in the signal handler another flush is invoked, for the same channel. The effect is that (so far I see it) channel->offset and channel->curr are set to illegal values. I don't see how this can be fixed properly. You probably can avoid the livelock by doing nothing when flush is invoked for the second time, but (a) this changes the semantics of flushing, and (b) doesn't fix the other potential problems (when a flush is interrupted and one of the other channel functions is called from the signal handler). Nevertheless it's a bit surprising that innocent-looking OCaml code turns out as unsafe. The current state is a bit unsatisfactory, at least. Gerd --=20 ------------------------------------------------------------ Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de My OCaml site: http://www.camlcity.org Contact details: http://www.camlcity.org/contact.html Company homepage: http://www.gerd-stolpmann.de ------------------------------------------------------------ --=-O1Ump5GBMdGg9j5j76Yp Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQEcBAABAgAGBQJTK0p6AAoJEAaM4b9ZLB5TOyQH/29QmAqgjfL0a2spyDIGnXDB kuTm0n0GeujzQp+FE4WSPA4QaApnJVX/Ts9DVa5lzAN0vcXZVN5Hb8WEsO10zaEh s8cb5SxrjFrXUsDt0eAQ1fI92vmYMseLYajk0llY0KwxSUhsOo4ZQkiHqA0Y5+Ax av2Po5Q3U+vIOOqSnDEYcxQ6PzfW9NGuEOc1Fi7lWNXc3miTiySyl9tuo7DW8/aq 4LDN9jv6Mdo16SF8Hv1eC+mKK8RqtT635jA+/j7C7lrOENmggXA0hkbLSj0KPGnZ Zf+2Xb4oVEXIqGDiPmNBXjksCv79uJUZ5mBKG5trn6/gtAHMpCJimPNIxGPoVTg= =1100 -----END PGP SIGNATURE----- --=-O1Ump5GBMdGg9j5j76Yp--