Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129354 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 lists.php.net (Postfix) with ESMTPS id DFF641A00BC for ; Fri, 21 Nov 2025 07:14:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1763709257; bh=ADdkKGRQAT0w40hFMb7hrDaOKnKJ5m4fIHxMmd6D/PE=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=fVZbwnYeYjRZCN3mqM2vbo1chSp9hRZf37dxNtsb0nyvCKa+K0PTNx7TRDVBusEPo CTj5j1U88znBmHVR5487Kz6bAyB0TiixPn1636l8yxhq3tmJrBpEI69uMeGlpdh+ee rM3qYBDyNw/fwAhV+8zWnBZQHTSXnSYPvdgPjKvTJ9ZTj51A1QuyaKtAi+E7iLZgkW XdOIxUwEo4DHAI4/3lEuFtxpUpg9hjSAOiMi0noxpnT2PZ7F7ZJC3jHn7OlV043YrG /GsNQnAIFEpRtUGZoGWySueIzgqmO2wReJhjMg9WbC3TAzo/XrRypiPfK8Z0Zek11b pn1Pthn73xhMg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B24AC180590 for ; Fri, 21 Nov 2025 07:14:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_20,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.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-ua1-f41.google.com (mail-ua1-f41.google.com [209.85.222.41]) (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 ; Fri, 21 Nov 2025 07:14:16 +0000 (UTC) Received: by mail-ua1-f41.google.com with SMTP id a1e0cc1a2514c-935241a7a0eso947516241.2 for ; Thu, 20 Nov 2025 23:14:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763709251; x=1764314051; 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=vN+NdFtPbrRAV046N5QTfYXvkb+KskH196AiAgAAIKQ=; b=RJEEkK10gezy0jNGLVN3cRys1LImbwwKKHHTmtK+/ZJAYnhbWct1r832xf66tvre9F m/fRizoHAMHfzHmQsqROdJ0R2s2yB8TvFulVxQi67bChENWo57c0IwVmgcz3lDJ0adhi L0G4Mv7F6iusDoEiMTNZcZVZmaoatuLf41LsLtDDw6OjMYnDs4J/1KuCCP1dnuj1RzU3 3f+adh6x3AJRS4TMTY220+BZXeH70BCBrDbZK+f30/a0FGR2qTruMMFbaEOh6XIqL5Cv ng+vpwxsNKOCZmNhtX+UUSXdKZ8rzBbhchyK0EQ3m0UtKVZEcdMMk97AvWRlleNLWdPS 5H0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763709251; x=1764314051; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=vN+NdFtPbrRAV046N5QTfYXvkb+KskH196AiAgAAIKQ=; b=R1ByEzciLx7zr9PrBO+d5uTpNvMWwMnVY+gIm9P6seXb65eAR5z6ysbGgPhvpfJiBP B42UmcsadQ+hkJJpYLIdOwho5d/rP0HuJf7GOef60AsB/zYxc6FXNmZzo7bBY9lM7QU0 P0yaHB9UB7sOZlOVeY6e0XW84BCfIweSc154zSl/wOFPuqEfHH0YG8p8TaND8tUCwK2I XDfsfIVUC6MHsCApR0H8A/R8rARPNYUeoUffdFnjFDMwIzKp7hJDKzoYXmVlqAf9yV9+ 5TjNAocx7uLpfOwnvCPtfSuYZPmtW3ruDZDW9Pkt1Gp8tNQBdTbYnXWH8vc3ulD+03Mb GkqA== X-Gm-Message-State: AOJu0YwWwWerpGWZws9ZNACyPyFvse+gOS8FBHrds6QFddu9WoK6y6MU LiAKjkjr1tDSZWp/IoCoUba3lVmJQ11MN9gsrhXvMGWvbt3W3OdZQ7CSu4y5EPtIKwMZUOMEg0w /Mj373Nshwu3e7SmJ1ILQ6al+yzc2h6h/MYFvLEc= X-Gm-Gg: ASbGncvWii96S0ZZ7WEd5Y6yrTOLjlaO24fsh6fNkQTsz8CMQ6eY3nxCES4lGgREk8y GiGaIw2RGjSsb3HWPcRQXn8wUpNrvOR6HlbDgGrN0TNXVSYixBAXnnyE+xZrwp8TzBs+xVa4Zmd MJawuazo1KxC5/R3SeLnFMC3nD3zPr6b7t/FnxX8u1Q/dVOIy5GWJtAaypv9404S/hdFYVD35tn EEt88vokrkMofrC26NN0ReCdrf3HI5pkjvnNyX1cGuMFcDg5wIY2OO+zAikKaCJic+xzZ+kZKap qK8TkAInxwKmj69wIvrUxIV11sY= X-Google-Smtp-Source: AGHT+IFl1EcHWG8OSfKxmHBjoozn+8c88o3FG3ZdYtqcmBBfH/HcPPn4qt4LGBwU2bDERgmfuiEE74foY5vzg8sMM/o= X-Received: by 2002:a05:6102:c52:b0:5dd:b2ee:4423 with SMTP id ada2fe7eead31-5e1de088196mr359333137.11.1763709250814; Thu, 20 Nov 2025 23:14:10 -0800 (PST) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 21 Nov 2025 09:13:59 +0200 X-Gm-Features: AWmQ_bnq4AmflK-tDsGAGzDQjp1-V5cQyg1XaMfNcI92Ft_uqqyjY-pAEWHKqSY Message-ID: Subject: Re: [PHP-DEV] [VOTE] True Async RFC 1.6 To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: edmond.ht@gmail.com (Edmond Dantes) Hello. Imagine that we have an application like this. ```php class AuthService { private static ?self $instance =3D null; private PDO $db; private ?string $sessionId =3D null; // Private constructor for singleton private function __construct(PDO $db) { $this->db =3D $db; } // Get singleton instance public static function getInstance(PDO $db): self { if (self::$instance =3D=3D=3D null) { self::$instance =3D new self($db); } return self::$instance; } public function login(string $email, string $password): bool { // Find user by email $stmt =3D $this->db->prepare('SELECT * FROM users WHERE email =3D ?= '); $stmt->execute([$email]); $user =3D $stmt->fetch(PDO::FETCH_ASSOC); // Invalid credentials if (!$user || !password_verify($password, $user['password_hash'])) = { return false; } // Generate and save session ID $this->sessionId =3D bin2hex(random_bytes(16)); $stmt =3D $this->db->prepare( 'INSERT INTO sessions (user_id, session_id) VALUES (?, ?)' ); $stmt->execute([$user['id'], $this->sessionId]); return true; } // Return current session ID public function getSessionId(): ?string { return $this->sessionId; } } ``` One day you decide you want more performance and make a single PHP process handle multiple connections concurrently. You wrap each request in a separate coroutine and try to use the old code. ```php $server =3D new Swoole\Http\Server("127.0.0.1", 9501); $server->on("request", function ($req, $res) { // create DB connection (just for example) $db =3D new PDO('mysql:host=3Dlocalhost;dbname=3Dtest', 'root', ''); // get singleton $auth =3D AuthService::getInstance($db); // read request data $data =3D json_decode($req->rawContent(), true); $email =3D $data['email'] ?? ''; $password =3D $data['password'] ?? ''; // call old sync code $ok =3D $auth->login($email, $password); if ($ok) { $res->end("Logged in, session: " . $auth->getSessionId()); } else { $res->status(401); $res->end("Invalid credentials"); } }); $server->start(); ``` What is happening here? Now, in PHP, inside a single process or thread, the same code is literally handling multiple connections. At the same time, there are constant switches between different requests at the points where MySQL queries occur. That is, when the code executes $stmt->execute([$email]); control is passed to another coroutine with a different $stmt->execute([$email]); What breaks in this code? Correct, coroutines break the singleton because they alternate writing different Session IDs! And what does not change in this code? The SQL queries can remain unchanged. The first problem with shared memory between coroutines can ONLY be solved by the programmer. Only the programmer. There is no solution that would make this happen automatically. Yesterday we talked about how we can help the programmer detect such situations during debugging. But in any case, only the programmer **CAN** and **MUST** solve this problem. The difference is that you don=E2=80=99t need to rewrite everything else. The focus is only on the issue of concurrent access to memory. The essence of the choice is how much code needs to be rewritten. Almost everything, or only the code with global state. My choice is: it=E2=80=99s better to rewrite only the code with global stat= e =E2=80=94 or switch to Go and avoid the pain :) As for the rest, I will write a separate message so as not to clutter thing= s up. ---- Edmond