From lemming at henning-thielemann.de Fri Aug 17 14:10:02 2012 From: lemming at henning-thielemann.de (Henning Thielemann) Date: Fri, 17 Aug 2012 14:10:02 +0200 (CEST) Subject: addForeignPtrFinalizer - finalizers run in FIFO or LIFO order? Message-ID: The doc of base-4.5:Foreign.ForeignPtr.addForeignPtrFinalizer states: "This function adds a finalizer to the given foreign object. The finalizer will run before all other finalizers for the same object which have already been registered." I do addForeignPtrFinalizer finalize1 fp addForeignPtrFinalizer finalize2 fp and let the finalizers print something when they are started. I get the output: finalizer1 finalizer2 This indicates that the newly added finalizer is run _after_ the finalizers that are already registered. Is this a documentation bug or a bug in the base library? From lemming at henning-thielemann.de Fri Aug 17 17:34:05 2012 From: lemming at henning-thielemann.de (Henning Thielemann) Date: Fri, 17 Aug 2012 17:34:05 +0200 (CEST) Subject: addForeignPtrFinalizer - finalizers run in FIFO or LIFO order? In-Reply-To: References: Message-ID: On Fri, 17 Aug 2012, Henning Thielemann wrote: > The doc of base-4.5:Foreign.ForeignPtr.addForeignPtrFinalizer states: > "This function adds a finalizer to the given foreign object. The finalizer > will run before all other finalizers for the same object which have already > been registered." > > > I do > > addForeignPtrFinalizer finalize1 fp > addForeignPtrFinalizer finalize2 fp > > and let the finalizers print something when they are started. > I get the output: > > finalizer1 > finalizer2 > > This indicates that the newly added finalizer is run _after_ the finalizers > that are already registered. Is this a documentation bug or a bug in the base > library? Hm, it seems to depend on the context. In some situations I get the correct (reversed) order finalizer2 finalizer1 and in others I get finalizer1 first and then finalizer2. Any hint what can change the order of finalizers? From lemming at henning-thielemann.de Fri Aug 17 18:32:14 2012 From: lemming at henning-thielemann.de (Henning Thielemann) Date: Fri, 17 Aug 2012 18:32:14 +0200 (CEST) Subject: addForeignPtrFinalizer - finalizers run in FIFO or LIFO order? In-Reply-To: References: Message-ID: On Fri, 17 Aug 2012, Henning Thielemann wrote: > On Fri, 17 Aug 2012, Henning Thielemann wrote: > >> The doc of base-4.5:Foreign.ForeignPtr.addForeignPtrFinalizer states: >> "This function adds a finalizer to the given foreign object. The >> finalizer will run before all other finalizers for the same object which >> have already been registered." >> >> >> I do >> >> addForeignPtrFinalizer finalize1 fp >> addForeignPtrFinalizer finalize2 fp >> >> and let the finalizers print something when they are started. >> I get the output: >> >> finalizer1 >> finalizer2 >> >> This indicates that the newly added finalizer is run _after_ the finalizers >> that are already registered. Is this a documentation bug or a bug in the >> base library? > > > Hm, it seems to depend on the context. In some situations I get the correct > (reversed) order > > finalizer2 > finalizer1 > > and in others I get finalizer1 first and then finalizer2. Any hint what can > change the order of finalizers? continuing my monologue ... It seems that a simple modification like turning a not directly related allocaBytes into a mallocBytes reverses the order from incorrect to correct (reversed). Maybe adding the finalizers is not the problem but running them. Maybe there are different ways to run the finalizers and allocaBytes causes an invalid execution of finalizers whereas mallocBytes leaves the correct way. From ezyang at MIT.EDU Fri Aug 17 22:06:44 2012 From: ezyang at MIT.EDU (Edward Z. Yang) Date: Fri, 17 Aug 2012 16:06:44 -0400 Subject: addForeignPtrFinalizer - finalizers run in FIFO or LIFO order? In-Reply-To: References: Message-ID: <1345233987-sup-6852@ezyang> No. 1 question: do you have a reproduceable test case of both behaviors? Edward Excerpts from Henning Thielemann's message of Fri Aug 17 08:10:02 -0400 2012: > > The doc of base-4.5:Foreign.ForeignPtr.addForeignPtrFinalizer states: > "This function adds a finalizer to the given foreign object. The > finalizer will run before all other finalizers for the same object which > have already been registered." > > > I do > > addForeignPtrFinalizer finalize1 fp > addForeignPtrFinalizer finalize2 fp > > and let the finalizers print something when they are started. > I get the output: > > finalizer1 > finalizer2 > > This indicates that the newly added finalizer is run _after_ the > finalizers that are already registered. Is this a documentation bug or a > bug in the base library? > From lemming at henning-thielemann.de Fri Aug 17 22:29:34 2012 From: lemming at henning-thielemann.de (Henning Thielemann) Date: Fri, 17 Aug 2012 22:29:34 +0200 (CEST) Subject: addForeignPtrFinalizer - finalizers run in FIFO or LIFO order? In-Reply-To: <1345233987-sup-6852@ezyang> References: <1345233987-sup-6852@ezyang> Message-ID: On Fri, 17 Aug 2012, Edward Z. Yang wrote: > No. 1 question: do you have a reproduceable test case of both behaviors? Unfortunately I was not able to reduce it to a small test. Below a certain complexity the problem vanishes. Currently my example has many dependencies on external libraries (libav etc.) :-( Thus I hoped I could approach the problem from a different direction. If there are different ways to run the finalizers of a ForeignPtr then I could check them. I could not find something suspicious in base:GHC.ForeignPtr, though.