From debbugs-submit-bounces@debbugs.gnu.org Mon Mar 08 08:45:28 2021 Received: (at 46796) by debbugs.gnu.org; 8 Mar 2021 13:45:28 +0000 Received: from localhost ([127.0.0.1]:42397 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lJGCO-00023y-5F for submit@debbugs.gnu.org; Mon, 08 Mar 2021 08:45:28 -0500 Received: from eggs.gnu.org ([209.51.188.92]:34120) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lJGCM-00023m-Op for 46796@debbugs.gnu.org; Mon, 08 Mar 2021 08:45:27 -0500 Received: from fencepost.gnu.org ([2001:470:142:3::e]:47348) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lJGCF-0006yT-N0 for 46796@debbugs.gnu.org; Mon, 08 Mar 2021 08:45:21 -0500 Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=41010 helo=ribbon) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1lJGCC-00030c-AY; Mon, 08 Mar 2021 08:45:16 -0500 From: =?utf-8?Q?Ludovic_Court=C3=A8s?= To: Mathieu Othacehe Subject: Re: bug#46796: Cuirass & pointer finalization. References: <8735xihq60.fsf@gnu.org> <87ft1hvfm4.fsf@gnu.org> <87k0qrusde.fsf@gnu.org> <87mtvmnfjb.fsf@gnu.org> <87blc1oeba.fsf@gnu.org> <87im69bia7.fsf@gnu.org> X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: 18 =?utf-8?Q?Vent=C3=B4se?= an 229 de la =?utf-8?Q?R?= =?utf-8?Q?=C3=A9volution?= X-PGP-Key-ID: 0x090B11993D9AEBB5 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 3CE4 6455 8A84 FDC6 9DB4 0CFB 090B 1199 3D9A EBB5 X-OS: x86_64-pc-linux-gnu Date: Mon, 08 Mar 2021 14:45:14 +0100 In-Reply-To: <87im69bia7.fsf@gnu.org> (Mathieu Othacehe's message of "Tue, 02 Mar 2021 18:02:40 +0100") Message-ID: <87v9a1ep3p.fsf@gnu.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 46796 Cc: 46796@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) Hi, Mathieu Othacehe skribis: >> Hmm I think the bytevector and the pointer object can be finalized in >> the same GC cycle; when that happens, you have no guarantee as to the >> order in which they are finalized. > > That would explain the crashes indeed. > >> But=E2=80=A6 is it really similar to your ZMQ issue? There you had mess= age >> object wrappers (as per =E2=80=98define-wrapped-pointer-type=E2=80=99) a= nd a pointer >> object to the underlying C object, right? > > I think the only difference is that the reproducer doesn't introduce the > wrapped pointer object. Using ZMQ, the message creation looks like: > > zmq-msg-init > Bytevector creation with make-bytevector at address P > Bytevector initialization with zmq_msg_init(P) > Install zmq_msg_close as finalizer on P > Message wrapping using (pointer->message P) > Return the wrapped message Shouldn=E2=80=99t the finalizer be on , then? > The user can then operate on the wrapped message by passing it to other > message API procedures such as zmq-message-size. Those procedures will > call ZMQ using the underlying pointer. > > The bytevector/pointer object undetermined GC order is really > problematic then. I'm not sure why I'm not experiencing this crash using > Guardians since they are also using finalizers. Guardians =E2=80=9Crevive=E2=80=9D objects: when you call the guardian, it = returns the object that _would have_ been GC=E2=80=99d. IOW, guardians delay =E2=80=9C= actual=E2=80=9D finalization. That may be the explanation. > The ultimate work around would be to leave the message closing > responsibility to the user but that would be sad. Yeah, don=E2=80=99t do that. :-) > Do you know if there's another to prevent the bytevector from being > collected before the pointer object? I=E2=80=99d really need to dive into the code but I=E2=80=99m confident the= re=E2=80=99s nothing special about this scenario; we=E2=80=99re probably just overlooking some pointer ownership rule. I see something risky: AIUI, =E2=80=98zmq-message-content=E2=80=99 returns = a bytevector that aliases a message=E2=80=99s buffer. The problem is that the bytevecto= r may still be used from Scheme after the message is destroyed, and then bad things can happen. Also, regarding the message API, my goal back then (but I never got around to it) was to not expose the msg API as such, and instead to have =E2=80=98zmq-send=E2=80=99, =E2=80=98zmq-receive=E2=80=99 etc. transpa= rently create msg_t objects. That simplifies things for users and perhaps also for the implementation. HTH, Ludo=E2=80=99.