Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126688 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 3A57C1A00BC for ; Mon, 10 Mar 2025 10:30:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1741602486; bh=AOX2ZXLUWkOIBsZMiKEovhLcHJOya0xohptBkEUlrcA=; h=From:Subject:Date:References:To:In-Reply-To:From; b=Cr/hz/bd/dx6Mhp8vXa1QymXZsP8Sn+j/3COuOvd9tmJKviej6UdHKnFxnLAD6RMB EKgvqHNdpNRaPDzSshHvc8GROdCaDZlccolPh1r4RGJTBpWkfq9VSLCT5xUS0D421c gZP3QksLl8vGpx/Ym03POsquCh0Umuxz2Q4/sjbzdwSJv+j/v/ZOb/HquCGPeiHZek I+SJVpT25oGSWoSlwhY7dNs2r8iMFdVuBPYGYjaLV0/MJKQOYRIYSI0efXbw1lDfcQ hKxtDqDMs/oRz2qHntHyUxbwbOSd/vseRP+pTofBLB9s4nk8CihQ/4KfX4Dmk45e18 Y2LirqPPzYz2w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5DE9B1801ED for ; Mon, 10 Mar 2025 10:28:03 +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.2 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) (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 ; Mon, 10 Mar 2025 10:28:01 +0000 (UTC) Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-ac2a089fbbdso146249766b.1 for ; Mon, 10 Mar 2025 03:30:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741602635; x=1742207435; darn=lists.php.net; h=message-id:in-reply-to:to:references:date:subject:mime-version:from :from:to:cc:subject:date:message-id:reply-to; bh=3L7Vs4USSQdex0TPkIyQNMGTFmjEeVP0xcRNMHLU7O8=; b=YWxp6DxQj3yVl2S0O+iAiMUx4/SDixDbHYbVWpEWI0Tc39Pa/oIqwjpIdFQpvbLcWD H0r0+nrz9YIbvgj0chdOHc2E52ITG+M7QiJFmjl0gupWwgOa5VlgCWygPVEWsLtOv3MS Cvf8E8pM8mJrOpqZOgD18v85YITXyPm5oUm1u4HjlnZ6eCl3+25R0XCMYzwmB409oVYM eIDz+rcWDmmgETSIrbfEdRvw8b1f8CJAXaDnxBrY7Atrk54dleNo3aXDl86RyMhRGILU wY5DD+Elgk0jq/H/1wHlzvbUIcuWBQLSzc+SVOLcB74n06aRnP0KJiusmYlOQfTtZ0iw x1vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741602635; x=1742207435; h=message-id:in-reply-to:to:references:date:subject:mime-version:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3L7Vs4USSQdex0TPkIyQNMGTFmjEeVP0xcRNMHLU7O8=; b=VMkHocCANJjpyvvDKoVTrZ6KeP57suWVwIsfPDTmZjnD26KEwj/2vDgzc9b3DARft0 q6NOY4WB9/RxOiLsxy+fiFS8n0vBfxN7g4+IUHL3KIA9uZXp3BWiY9APv+1mc1133Dfg fhl5ggA0IgpD0LSHZR6BPjRDbCB+Bfe+5M51sL2yxK7W6e9dg9cAxredMHhZvwsQ6uzN 5WELgamq7PpTIhqgpLz/0ush6Py1Agh5/nxbpj6hKXrNNa0cq6cAIuVE+4lIMd7bZM/M FT7CM9hOBRG/MAs6IGM60JExjjaH56iEeT9G/gt4oeTtl+LFgoKdEK5p/yuM9W7mzPtR gkrA== X-Gm-Message-State: AOJu0YzCQfwDYxG7wYRtifEAeklOLgh1hA/KRpBKLx9LmQ6t2Tp9IDOF 2Qas3PJsnN7QCdnF6lcUP7GtIquUSTsiUSMEjOV6gMyRYm4rnpsVOnuW4A== X-Gm-Gg: ASbGncv2qi9nndxM0PELxZkQ/jhL2tbx3PdQiuYWM745dH9OJ7+y0koMJkiJvVSWgiB CkbhWxeOjj9Z+/Z+k8At3E5oXYOTRnGWWZxUDZP1ZZXbYplDrKN6Uhbd2CLQT8YeOgwzfKR9BR2 ZZJj0CS4piHEHK+nH68YGiL9s1YUuJt2Ao7TUReZN5doV1+XcAWNVolNbAiXAlgB0HxCXUUvC88 P429yStgVW/ADDmz6HpwFYAbpI/G6i4yRt+Ietw39xtKjKNqvGfJMh+OiQC1ToSwIv9v+oqq6r/ erSBG/AugMUM2HDHWo9nkxr17GPMoEFeP/wsr4Dt5mpbwjK3Oo4FAMGjh4FIgGiKUxeEniFCbCM Tfhs6gGIrjbWLW4RifqSl7KiGIPSgIWvjP/NzbJ/NZVdwgCiE+65vgXUMt6lM X-Google-Smtp-Source: AGHT+IGiq85+muWQtf0NT+Q+gcACpYZeI0HduRJMl+kOLLPUNiRdkfurpe8WsLIfkOqRi5/Ic3sC7w== X-Received: by 2002:a17:906:81d4:b0:ac2:b2b5:40ce with SMTP id a640c23a62f3a-ac2b2b54ba3mr49186466b.13.1741602634159; Mon, 10 Mar 2025 03:30:34 -0700 (PDT) Received: from smtpclient.apple (luna-7e8e7de843ec2ac10000f.net.as198747.daniil.it. [2a0e:97c0:38f:0:1ca2:ce34:8ed7:e8e7]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ac2779af6ffsm401284366b.131.2025.03.10.03.30.33 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 10 Mar 2025 03:30:33 -0700 (PDT) Content-Type: multipart/alternative; boundary="Apple-Mail=_7ED9BA93-8C20-43E5-88FD-6845DF652F2C" Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.400.131.1.6\)) Subject: Re: [PHP-DEV] PHP True Async RFC Date: Mon, 10 Mar 2025 11:30:22 +0100 References: <08c8ad0b-e8f4-46e3-99f0-b80748d40b89@app.fastmail.com> <07973EAE-2D83-47A8-8FA0-84286C77C02B@rwec.co.uk> <48d66433-3ae9-4895-8361-7c81a0a3670d@app.fastmail.com> <8599eb8b-d4a3-4cb8-899a-25b134e0d64d@gmail.com> <74c4c726-63aa-44e0-84c9-840e13a65a4f@gmail.com> <77DC5F50-531D-49E8-8BE2-504A19CB5FFD@rwec.co.uk> <676e36e4-0b84-4d8c-b3db-2998831cd79d@gmail.com> <510B8EF1-9D07-41A8-9EA0-7D99CF7BFC91@rwec.co.uk> <4690cff0-7a48-4fe0-8310-688be253f976@gmail.com> <4871b2e4-353e-424f-92b9-7e2bb753bafa@gmail.com> To: internals@lists.php.net In-Reply-To: Message-ID: <7D224BD3-2FF7-45E8-8D90-C9AEDC80DD4F@gmail.com> X-Mailer: Apple Mail (2.3826.400.131.1.6) From: daniil.gentili@gmail.com (Daniil Gentili) --Apple-Mail=_7ED9BA93-8C20-43E5-88FD-6845DF652F2C Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Let me summarize the current state for today: >=20 > I am abandoning startScheduler and the idea of preserving backward = compatibility with await_all or anything else in that category. The = scheduler will be initialized implicitly, and this does not concern = user-land. Consequently, the spawn function() code will work everywhere = and always. >=20 Very glad to hear this, this is the correct approach for concurrency, = one that will not break all existing libraries and give them the freedom = to handle their own resource cleanup. I=E2=80=99ve also seen your latest email about kotlin-like contexts, and = they also make more sense than an await_all block (which can only cause = deadlocks): note how a kotlin coroutine context may only be cancelled = (cancelling all inner coroutines with CancelledExceptions), never = awaited. >=20 > > > > I can give you several examples where such logic is used in Amphp = libraries, and it will break if they are invoked within an async block. > > >=20 > Got it, it looks like I misunderstood the post due to my focus. So, = essentially, you're talking not so much about wait_all itself, but = rather about the parent-child vs. free model. >=20 > This question is what concerns me the most right now. >=20 > If you have real examples of how this can cause problems, I would = really appreciate it if you could share them. Code is the best criterion = of truth. >=20 Sure: - = https://github.com/amphp/websocket/blob/2.x/src/PeriodicHeartbeatQueue.php= - https://github.com/amphp/redis/blob/2.x/src/Sync/RedisMutex.php This is the main example where it is most evident that background fibers = are needed: logic which requires periodic background pings to be sent in = order to keep a connection alive, a mutex held or something similar.=20 Constructing a PeriodicHeartbeatQueueinside of a wait_all block invoking = a a someClass::connect(), storing it in a property and destroying it = outside of it in someClass::close or someClass::__destruct, would cause = a deadlock (the EventLoop::repeat doesn=E2=80=99t technically spawn a = fiber immediately, it spawns one every $interval, but it behaves as = though a single background fiber is spawned with a sleep($interval), so = essentially it=E2=80=99s a standalone thread of execution, collected = only on __destruct). https://github.com/danog/MadelineProto/tree/v8/src/Loop/Connection = contains multiple examples of tasks of the same kind in my own library = (ping loops to keep connections alive, read loops to handle updates = (which contain vital information needed to keep the client running = correctly) in the background, etc...), all started on __construct when = initialising the library, and stopped in __destruct when they are not = needed anymore. A coroutine context/scope a-la kotlin is fine, but it should absolutely = not have anything to await all coroutines in the scope, or else it can = cause deadlocks with the very common logic listed above. Regards, Daniil Gentili.= --Apple-Mail=_7ED9BA93-8C20-43E5-88FD-6845DF652F2C Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

    Let me summarize the current = state for today:

    1. I am = abandoning startScheduler and the idea of = preserving backward compatibility = with await_all or anything else in that category. = The scheduler will be initialized implicitly, and this does not concern = user-land. Consequently, the spawn = function() code will work everywhere and = always.


Very glad to = hear this, this is the correct approach for concurrency, one that will = not break all existing libraries and give them the freedom to handle = their own resource cleanup.

I=E2=80=99ve also seen your latest email about = kotlin-like contexts, and they also make more sense than an await_all = block (which can only cause deadlocks): note how a kotlin coroutine = context may only be cancelled (cancelling all inner coroutines with = CancelledExceptions), never awaited.


>
>  I can give you several examples where such logic is used in Amphp = libraries, and it will break if they are invoked within an async = block.
>

Got it, it looks like I misunderstood the = post due to my focus. So, essentially, you're talking not so much about = wait_all itself, but rather about the parent-child vs. free = model.

This question is what concerns me the most right = now.

If you have real examples of how this can cause problems, I = would really appreciate it if you could share them. Code is the best = criterion of = truth.


Sure:


This is the main example where it is most evident that = background fibers are needed: logic which requires periodic background = pings to be sent in order to keep a connection alive, a mutex held or = something similar. 

Constructing a = PeriodicHeartbeatQueueinside of a wait_all block invoking a a = someClass::connect(), storing it in a property and destroying it outside = of it in someClass::close or someClass::__destruct, would cause a = deadlock (the EventLoop::repeat doesn=E2=80=99t technically spawn a = fiber immediately, it spawns one every $interval, but it behaves as = though a single background fiber is spawned with a sleep($interval), so = essentially it=E2=80=99s a standalone thread of execution, collected = only on __destruct).

https://github.com/danog/MadelineProto/tree/v8/src/Loop/Connection&n= bsp;contains multiple examples of tasks of the same kind in my own = library (ping loops to keep connections alive, read loops to handle = updates (which contain vital information needed to keep the client = running correctly) in the background, etc...), all started on = __construct when initialising the library, and stopped in __destruct = when they are not needed anymore.

A coroutine = context/scope a-la kotlin is fine, but it should absolutely not have = anything to await all coroutines in the scope, or else it can cause = deadlocks with the very common logic listed = above.

Regards,
Daniil = Gentili.
= --Apple-Mail=_7ED9BA93-8C20-43E5-88FD-6845DF652F2C--