Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116912 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 86917 invoked from network); 22 Jan 2022 17:50:08 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Jan 2022 17:50:08 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 214371804BA for ; Sat, 22 Jan 2022 11:01:54 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f174.google.com (mail-yb1-f174.google.com [209.85.219.174]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 22 Jan 2022 11:01:53 -0800 (PST) Received: by mail-yb1-f174.google.com with SMTP id o80so37988733yba.6 for ; Sat, 22 Jan 2022 11:01:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=w3nF4KEyl5Vna9/QMaP8BHImpo3XYBsnKWXnl3gzro4=; b=OL5mRLO7boSJ7CCYN2pkKEEMpM3y0hYTzwxRmgIMJtSOOT3XtPXpFzouKVhS+YMQHc z4GN6RPH7oOiJpZmdt188+MycpwQHLTlah+45xNp60kYg0hcQZq2pnBwW4sXsetsVHvk NZzJYigpNLKiDa6N1i+qJCcgcubJZ5lNZ3AJ3CNZFf8yUTyoX4WK48YmOY58rmMose4B sBB2FPh1zsVKX4y73VZIGl9058KjPVhK3IW7ih1WpjoHqjIn5ilS2qf26FKdjQQQ7Jn7 d7Z+ZPDNdpGzpSi4Ov6Y7jBPGFOVKwowkxv7kTJUbG01KICj4WblHugjVzyQxAYf5PmE jyWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=w3nF4KEyl5Vna9/QMaP8BHImpo3XYBsnKWXnl3gzro4=; b=ylA2HXFmk+x9TiH03+ggqb3zA/asnueEV+Sq+nMtqzZHOGkvow/8s7jnNf9WE/yRFZ hpSWHCPF9AqSYIfVKdqcZkq3q3SzG5hue3+ljpiyOcDDOGqd/anz68MANKlvy5ka4H2I rynxCvRcrUcQUhMp4QTNTv3nvOUOB1/F8fKA/uGsuHNATMx18oS0kxx3w+Rl03ZqpJ5I V+HLCFRZJFpIwZZhBYvh7GOQ5BdfUm2djIJrp92FNBPIg4v+vlgEcl59o/menF1jCh7j F0vfkFFLJvB/G/Epi8haCBiYZmgrwwZgmigvBtbsCTpfUcfnbGrZLizEWNJokAVwO3j3 dmOg== X-Gm-Message-State: AOAM531BQwIcArKZg2/Azog/QjvBVYwXcn214XjX1aDGUrEo6sKqNEza 1YfJWpqJdqRF6Y5WbUngBBPvQFFewPblwUH0tDLmO6o= X-Google-Smtp-Source: ABdhPJwpwUG2IE/sMfoJAoz7cywc8y/LH8zbz4OhyNmVWJuOhfrZJ4ru1yiS0Vhq0Mt2xcXkanag/p5Z9lS03VyDBJc= X-Received: by 2002:a25:d38e:: with SMTP id e136mr13208024ybf.269.1642878113165; Sat, 22 Jan 2022 11:01:53 -0800 (PST) MIME-Version: 1.0 References: <7ea94d3b-7f1f-b604-b94a-781e0b992c69@gmx.de> In-Reply-To: <7ea94d3b-7f1f-b604-b94a-781e0b992c69@gmx.de> Date: Sat, 22 Jan 2022 19:01:42 +0000 Message-ID: To: "Christoph M. Becker" Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000005835a205d6305e37" Subject: Re: Weak Closures From: codekestrel@googlemail.com (Dominic Grostate) --0000000000005835a205d6305e37 Content-Type: text/plain; charset="UTF-8" That's very strange, I had thought it might be a bug. For simple use cases where an object references it's own closure it might be a memory leak, but for complex event loops it potentially allows unnecessary code to run indefinitely. I'm afraid I don't know any more than that. Thanks for the tip though. On Sat, 22 Jan 2022, 15:30 Christoph M. Becker, wrote: > On 21.01.2022 at 11:31, Dominic Grostate wrote: > > > I'd like to express my interest in a possible feature concerning weak > > references. Currently closures created within a class appear to > > contain a reference to the object that created it. This is of course > > necessary in order for the closure to retain the necessary scope. > > However I would like to suggest we have the option for closure to > > weakly reference this object so that when the object is garbage > > collected, the closure too may be rendered null or invalid > > (inspectable). > > > > Consider the following example: > > https://gist.github.com/orolyn/7651e4127759aad1736547490baa1394 > > > > The idea here is that without unset($sample) the loop would run > > forever, because there is a reference to the callback from the object, > > and in turn there is a reference to the object from the main stack. > > With the unset($sample), the idea is that before the callback is even > > called, the reference to the object is destroyed thusly so is the > > reference to callback, and since the callback is only referenced now > > in a WeakMap, the callback will finally be gone. > > > > In reality it appears there is a circular reference between the object > > and the callback. I don't know if this is a bug or not, because from > > what I can find PHP was fixed a long time ago to resolve circular > > references. > > > > However if this is intentional, I would like the option to make the > > closure weak for example: > > > > $c = WeakClosure::fromCallable(function () {}); > > $c = Closure::fromCallable(function () {}, true); > > > > Please let me know your thoughts, because maybe there is a way of > > achieving this with PHP as is. > > If you trigger the garbage collector manually (i.e. call > gc_collect_cycles() after unset($callback)), the loop terminates right > away. I'm not sure why it doesn't without manually triggering the GC. > > Christoph > --0000000000005835a205d6305e37--