From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: Threads vs url-http Date: Mon, 30 Sep 2024 15:22:00 +0300 Message-ID: <86r0912uxj.fsf@gnu.org> References: <3GKP9PAUIwZh8IYYiDahnaeVDrY2O7V_LkjMJ_EcOfSz0ltAUheLQ47o9l8VIm0-8vGVQpKuFMjq2qLiHVN3BIeLKx_AbaacMBQbfvR9_cs=@protonmail.com> <8634lj58uc.fsf@gnu.org> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11327"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: 813gan <813gan@protonmail.com> Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Sep 30 14:23:11 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1svFQk-0002mj-NV for ged-emacs-devel@m.gmane-mx.org; Mon, 30 Sep 2024 14:23:10 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1svFQ9-0002ad-6e; Mon, 30 Sep 2024 08:22:36 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1svFPx-0002aA-5L for emacs-devel@gnu.org; Mon, 30 Sep 2024 08:22:22 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1svFPv-0002Jz-8z; Mon, 30 Sep 2024 08:22:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=RbhZcBOWls4daqWmVtt0GCX/QbxcNzBVk/x8JkBXpaY=; b=GUwGlgaqIncz yd3Ycqxkq8wT2QfTH/xkwVMbJOP/FkzaPgOlIZj6jllWZEHv451+AECV79VBRF5O06kCOxsOtTj1h ml8vmTOd3iinWWRHIDBHYkv2ylKuHydayhu6Pp2tJBmUhjWcwkhqBAJ2w7WLggeE8HPuoJtUDBL7K tuHB+kvr9ZWY0nS0PTbKIdQpbmuOXQFx0tkJCv88xAVwkLqQDuijtcTqVgyyYqJ3M9rgMGS4I+Z+L sxhErpsiTjPBmRSQiEZWuampQ1JoNKyGpImFnfGn1bJOXMFt1kAhn6+zscpDWFUj8/AcWgnb+1E9r zYldGCJZk63V7/MxZlHFQw==; In-Reply-To: (message from 813gan on Sun, 29 Sep 2024 23:53:44 +0000) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:324219 Archived-At: > Date: Sun, 29 Sep 2024 23:53:44 +0000 > From: 813gan <813gan@protonmail.com> > Cc: emacs-devel@gnu.org > > (defun wiki-read--url-retrieve (url) > "Wrapper around url-http that download `URL' without blocking thread." > (let* ((parsed-url (if (url-p url) url (url-generic-parse-url url))) > (buf nil) > (cb (lambda (&rest _) (setq buf (current-buffer)) )) ) > (url-https parsed-url cb nil) > (while (not buf) (thread-yield)) > buf)) > (make-thread (apply 'wiki-read--url-retrieve "https://google.com" nil)) > > Why thread-yield don't unblock this? make-thread needs a function as its first argument. But you pass it the value returned by 'apply', which is not a function at all. So what happens here, I think, is this: . Emacs invokes 'apply' to evaluate the argument of make-thread . 'apply' invokes your wiki-read--url-retrieve function . the function runs in the main thread, not in the thread started by make-thread (which is not even called, because the code is stuck in 'apply', see below) . after url-https returns (which it does immediately), the code immediately enters the while-loop, where it calls thread-yield . because there's only one running thread, thread-yield grabs the global lock immediately after releasing it, and the loop keeps looping . because the program loops continuously, it doesn't let url-https read from the network process (since Emacs only does that when it is idle) . so the output of the network sub-process is never read, and so the sub-process never ends, and so the callback is never called, and the loop never ends