Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 3D5FF429E30 for ; Mon, 29 Aug 2011 13:31:03 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.799 X-Spam-Level: X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id jD7c1Y5o4M5X for ; Mon, 29 Aug 2011 13:31:02 -0700 (PDT) Received: from mail-qy0-f181.google.com (mail-qy0-f181.google.com [209.85.216.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 6830B429E28 for ; Mon, 29 Aug 2011 13:31:02 -0700 (PDT) Received: by qyk34 with SMTP id 34so4441737qyk.5 for ; Mon, 29 Aug 2011 13:31:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:cc:subject:in-reply-to:references:user-agent:date :message-id:mime-version:content-type; bh=m7q3XiWrjcGlIlZFvmzrWptslZmVJwa3jX5wUOVoPNc=; b=pO8wxAnzakFaL4CYEyMn3bAi5JxmycqgFnAlGC4cnJ1q3vuP+OENhPr9JM4C0cnzbA NAmtvnfYOc74dJ5DWyxWCEw2KVwC2Wq83hmKG84TDGrVctngeSO28y+Q3qHphYu9cZ0d kN3g2fQO2LOUAVa88dvjD1Guox5SC0WKrrK60= Received: by 10.229.34.205 with SMTP id m13mr578282qcd.138.1314649860035; Mon, 29 Aug 2011 13:31:00 -0700 (PDT) Received: from localhost (gamari.physics.umass.edu [128.119.56.223]) by mx.google.com with ESMTPS id o4sm3642286qct.13.2011.08.29.13.30.58 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 29 Aug 2011 13:30:58 -0700 (PDT) From: Ben Gamari To: notmuch Subject: Memory management practices In-Reply-To: <20110829183010.GA2605@24f89f8c-e6a1-4e75-85ee-bb8a3743bb9f> References: <8739h1pbaq.fsf@gmail.com> <87pqjprzu2.fsf@gmail.com> <20110829183010.GA2605@24f89f8c-e6a1-4e75-85ee-bb8a3743bb9f> User-Agent: Notmuch/0.7-37-g5c3c7f6 (http://notmuchmail.org) Emacs/23.2.1 (x86_64-pc-linux-gnu) Date: Mon, 29 Aug 2011 16:30:57 -0400 Message-ID: <87liucyn7i.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: Bertram Felgenhauer , Bart Massey X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 29 Aug 2011 20:31:03 -0000 Hey all, Over the last few weeks I've been trying to fix some brokeness in the notmuch-haskell bindings with respect to memory management. In discussion included below, I describe the issue as I approached it. In short, it appears that GHC's garbage collector is quite liberal with the order in which it frees resources (which is apparently permitted by Haskell's FFI specification), allowing, for instance, a Messages object to be freed before a Query object despite my attempts to hold the proper references in the Haskell wrapper objects to keep the Query reachable. In general, it seems to me that memory management in notmuch bindings is a little bit harder than it needs to me due to the decision not to talloc_ref parent objects when a new child object is created. This means that a bindings author needs to recreate the ownership tree in their binding, a task which is fairly easily done (except in the case of Haskell due to the weak GC finalization guarantees) but seems quite unnecessary. Is there a reason this decision was made? Would a patch be accepted adding talloc_ref'ing parents in those functions creating children and talloc_frees in *_destroys? Cheers, - Ben On Mon, 29 Aug 2011 20:30:10 +0200, Bertram Felgenhauer wrote: > Dear Ben, > > Ben Gamari wrote: > > After looking into this issue in a bit more depth, I'm even more > > confused. In fact, I would not be surprised if I have stumbled into a > > bug in the GC. > [...] > > MessagesMessage > > | > > | msmpp > > \/ > > QueryMessages > > | > > | qmpp > > \/ > > Query > > > > As we can see, each MessagesMessage object in the Messages list > > resulting from queryMessages holds a reference to the Query object from > > which it originated. For this reason, I fail to see how it is possible > > that the RTS would attempt to free the Query before freeing the > > MessagesPtr. > > When a garbage collection is performed, the RTS determines which heap > objects are still reachable. The rest is then freed _simultaneously_, > and the corresponding finalizers are run in some random order. > > So assuming the application holds a reference to the MessagesMessage > object for a while and then drops it, the GC will detect unreachability > of all the three objects at the same time and in the end, the finalizer > for MessagesMessage may be run before that of Query. > > So I think this is not a bug. > > To solve this problem properly, libnotmuch should stop imposing order > constraints on when objects are freed - this would mean tracking > references using talloc_ref and talloc_unlink instead of > talloc_free inside the library. > > For a bindings author who does not want to touch the library, the best > idea I have is to add a layer with the sole purpose of tracking those > implicit references. > > Best regards, > > Bertram > > _______________________________________________ > Glasgow-haskell-users mailing list > Glasgow-haskell-users@haskell.org > http://www.haskell.org/mailman/listinfo/glasgow-haskell-users