Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125710 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 4FB4A1A00BD for ; Tue, 1 Oct 2024 12:12:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1727784880; bh=YvfGd1bNUy/jFiojUQALgha36jy/iTV7KEG3HcEwhdI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=QmOd1C/OjUj6UsaXl7UfMEm4lP6hMsgElePVqlm9/f/gssK/mgySoxkShV0e/QQpg vsRLIJqb52JsZk6zms1ussFT8L3eX10pXODY0bHypNjwf1EHZct7xsJypwA94lPv7h QSl6eCBd6yfLo236r5NzXC47m+hpTpEL9dfTbAZoYUY7EMqe6yg0ueg1CdBTagaJy1 RUr13nVoMX+WMY+laBWIM6sBlM9RMDHgu9GIZinFSuhkdYGxWIoTVOBZ9Aw6JwOVfM ByTSd7nuizKyYqaZDxP7csQxPfER4jmtb3TYHo/1moLMZdv0EysysZ88vqmx9WGOsw CjNBQlfSpYjpA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3AE83180037 for ; Tue, 1 Oct 2024 12:14:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 1 Oct 2024 12:14:39 +0000 (UTC) Received: by mail-lf1-f51.google.com with SMTP id 2adb3069b0e04-5398df2c871so3071687e87.1 for ; Tue, 01 Oct 2024 05:12:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727784745; x=1728389545; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=2YO3ubBpvexgFWtKFrTRR7QdqmLjgwQ4j/uM0HA9gpA=; b=KIUgRIvJ8WJJPNcdZvrz0OZuUvRwFODq+C0GQblbiSfak21yWkGOGYVkzYMC+P319h rXqAQK9fWT/NoQ+eSy4fs0NB8HJkAPNYmwJWv0NMBr+OOfPWKxt9EG54/j5cvKySqHmC 1Vj/OiUZAM93auBAiNwa0ClixYJoBU+cZ0jORVOTJSnRYl3L8W715WpiSg9Vyw05xKQP YbuMBqumPpB53RppCLviyLMAvIyD6W/arUiJxNkP4SnwNah9VijROdwJWQMGm4/NBtj9 oyriOSBgL61tiTOyaDJVzqBgIdLm8qgjn5nW+O6jmZaLqj887EIlzQbd34oIbvgbjw7z HBgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727784745; x=1728389545; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2YO3ubBpvexgFWtKFrTRR7QdqmLjgwQ4j/uM0HA9gpA=; b=pnGemICnZ7QWppg2cvL6YunuXwmb6LPYZ2+m0zmbvi2QuoM6rrr2hB7KRBN2taBcDT p52SHGAcuf8lPkD5PnpjmYFNBGiuidn2ypPr97agF9S94jOz3fOpDlgzCKLxbzVysMWR eYnX/ZGWRSV4Y1TLlwnxzDX4Idca7Pg6UQLRHvinuzmyIBQ2nvU9St9h9KXFFA5Q2b3a Rd5G7TIijT+GjbCJcE/N4swNxbKS+c2nz4bSUaOt+JSZO+QvYLmrmLHT8oORmKvrI2cm FOlGpssC1keCPx+bDa5O0r8nfITStzdixN3dVKVM6vw0WlhOUnMDYZKWLAKL06LkALbU h6gg== X-Forwarded-Encrypted: i=1; AJvYcCVQt7sTX4CCGJBhrldL0lT6jdcLM3FDBtMfuTbPJ9ZdcNg4fw0RosU7MtiNXRC+eUirlXLYMoQp7+4=@lists.php.net X-Gm-Message-State: AOJu0YzarSQddGNbCaIGaVdqMHi/bp5OksBnCcv/9krGVa648krk5ReX 4cGNPkB0vxnXF3FAGgDH+xnvgIkBCK0Vh8Vz+eCPLiiggZx5fjaqshQdFkMWJU5UxjheUQM7Bif sZUufrTNhyZ1CJgn2DlFLqkRguss= X-Google-Smtp-Source: AGHT+IG6k26uyCrWJrqBHyF6YbQkeipxv7DuwpUiSigbg0DQWYJjEhkXbtCz5S6IN0Jnm4XOi6aoou0S9E3l7RorDoM= X-Received: by 2002:a05:6512:158f:b0:533:46cc:a736 with SMTP id 2adb3069b0e04-5389fc6b0c1mr8309795e87.37.1727784744980; Tue, 01 Oct 2024 05:12:24 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <0a45288d-8ae1-4b1c-8836-6bdd1cf34eb2@gmx.de> In-Reply-To: Date: Tue, 1 Oct 2024 14:12:14 +0200 Message-ID: Subject: Re: [PHP-DEV] Protected destructors To: "Christoph M. Becker" Cc: Jonathan Vollebregt , internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: arnaud.lb@gmail.com (Arnaud Le Blanc) Hi Christoph, On Sat, Sep 28, 2024 at 4:47=E2=80=AFPM Christoph M. Becker wrote: > > Besides closing resources and killing processes I've seen them store > > data to disk for caching, remove temp files, call callbacks/dispatch > > events, change state on other objects, dump stored errors to error_log > > in a loop in an error handler... > > Okay. My point is that you cannot know (unless there are no circular > dependencies) *when* a destructor is called by the engine; it may be > called during some GC run, or during the request shutdown sequence. As > it's now, that happens pretty early during shutdown, but that *might* > change when stream resources are converted to objects. So you cannot be > absolutely sure that everything works as expected in destructors. This > is a general issue for garbage collected languages; some of these have > no destructors at all, for such reasons. I agree. To expand on this, I think that the use of destructors should be discouraged for many reasons: 1. Objects with destructors slow down garbage collection due to potential resurrections. It's not possible to detect resurrections, so in the presence of destructors the GC has to forget most of its state, run destructors, and restart. 2. They may be called in undefined order during GC and shutdown, including after their dependencies have already been destroyed 3. They may cause concurrency issues (even in non-concurrent programs) because they can be called at any time by the GC 4. They give a false sense of security as destructors will not be called in case of fatal errors or crashes 5. Using them to manage non-memory resources (such as file descriptors, temp files, ...) is not a good idea because the GC is only triggered by memory metrics. The process may run out of file descriptors before it triggers a garbage collection, for example. Part of these problems would be solved by disabling the GC, but ensuring that large code bases are free of cycles is not an easy task. Java has deprecated destructors (finalizers) [2] and recommends using Cleaners [1] instead. Cleaners would resolve the 1st issue, and partially the 2nd one (for GC, not shutdown), but I'm not entirely sure they are transposable. Ruby's ObjectSpace.define_finalizer is similar to Cleaners, AFAIK, in that the finalizer must not reference the object. Some use-cases of destructors could be replaced with patterns like Python's with() [3], Java's try-with [4], or Go's defer [5]. [1] https://docs.oracle.com/javase/9/docs/api/java/lang/ref/Cleaner.html [2] https://docs.oracle.com/javase/9/docs/api/java/lang/Object.html#finaliz= e-- [3] https://docs.python.org/3/reference/compound_stmts.html#with [4] https://docs.oracle.com/javase/8/docs/technotes/guides/language/try-wit= h-resources.html [5] https://go.dev/doc/effective_go#defer Best Regards, Arnaud