From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Andrew Tropin Newsgroups: gmane.lisp.guile.devel Subject: [BUG] Non-local exit in thunk from system-async-mark doesn't restore asyncs block Date: Fri, 06 Oct 2023 08:50:55 +0400 Message-ID: <871qe8fj68.fsf@trop.in> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="7947"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Andy Wingo , Maxime Devos To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Fri Oct 06 06:51:28 2023 Return-path: Envelope-to: guile-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 1qocoC-0001vH-Pk for guile-devel@m.gmane-mx.org; Fri, 06 Oct 2023 06:51:28 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qocnx-0001JJ-TC; Fri, 06 Oct 2023 00:51:13 -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 1qocnu-0001Iq-Ik for guile-devel@gnu.org; Fri, 06 Oct 2023 00:51:10 -0400 Original-Received: from relay9-d.mail.gandi.net ([2001:4b98:dc4:8::229]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qocnq-0006oA-C6 for guile-devel@gnu.org; Fri, 06 Oct 2023 00:51:09 -0400 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id 5DEBEFF806; Fri, 6 Oct 2023 04:50:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=trop.in; s=gm1; t=1696567861; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=7vsHbUJHDkEjEgrGGpqLKaaGlbnZL0NHGTqsQ38qW2c=; b=flHC9Cqo+JqyMRafCHXbzNcpt9QJi1mGKjKvKbwmeJMTgKP3TugEoEqCNWELUrSVZIM3kt UuynHUdJDenul1qUiLR3gaT703ZdMJLP0jy7K4nV0n9LAA1rF/0mIdhjd351RYqdsLwvr4 dUcIxygpNurb7KxvQzixwBjBh1kCzLOjHZv/WWCZRKat74OLroTRG8MPZKt4WqZyl8QnfE e5e1W1ZMB0R6llW+F6OnioFVe4wghtldioA5omiticb8TuIW0sIZnNu0yorMgdYhLtgaL6 5sVwZpd0xZovDvsdDiBiO9Ke6JN0q7lvUE4cBq1auDIarIEmdgVXW3l5Q8pWXQ== X-GND-Sasl: andrew@trop.in Received-SPF: pass client-ip=2001:4b98:dc4:8::229; envelope-from=andrew@trop.in; helo=relay9-d.mail.gandi.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.devel:22018 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable TLDR: abort-to-prompt restores asyncs block level. abort-to-prompt from a procedure scheduled with system-async-mark doesn't restort asyncs block level. Some more context: I was trying to implement interruptible and reusable threads on top of delimited continuations and asynchronous interrupts. The interruption of evaluation is implemented simply by aborting to prompt by procedure scheduled with system-async-mark. It works fine. The problem is that there is a short moment of time when we are outside of prompt and if someone will try to abort to prompt it will fail. To prevent this, the prompt setup routine is done in the context, where asyncs are blocked, after prompt is set we can run a thunk in context with unblocked asyncs (so it can be interrupted). So this was a plan, but unfortunatelly if abort-to-prompt is done by a procedure scheduled by system-async-mark the asyncs are not re-blocked and thus next attempt to unblock asyncs fails with exception. I have an idea on how to workaround my use case, but it seems that non-local escape in procedures scheduled by system-async-mark doesn't re-block asyncs is potentially a bug. Here is a simplified reproducer of the problem: =2D-8<---------------cut here---------------start------------->8--- (begin (use-modules (ice-9 threads)) (define prompt-tag 'tmp) (define (jump-out-of-prompt-thunk . args) (lambda () (apply abort-to-prompt prompt-tag args))) (define (with-unblocked-asyncs-thunk thunk) "Return a thunk, which runs a THUNK in context with unblocked-asyncs." (lambda () (call-with-unblocked-asyncs (lambda () (format #t "asyncs unblocked.\n") (thunk))))) (define (blocked-async-loop) (call-with-blocked-asyncs (lambda () (let loop ((i 0)) (newline) (format #t "---> going to enter the prompt\n") (call-with-prompt 'tmp (with-unblocked-asyncs-thunk ;; This jump works fine. Async get re-blocked (jump-out-of-prompt-thunk 'from-inside-loop)) (lambda (k . args) (format #t "=3D=3D=3D=3D jumped out of prompt: ~a\n" args))) (format #t "<--- I'm out of prompt: ~a.\n" i) (sleep 2) (when (< i 4) (loop (1+ i)))) (format #t "I'm almost finished.\n")))) (let* ((th (call-with-new-thread (lambda () (blocked-async-loop))))) (sleep 4) (system-async-mark ;; This jump doesn't lead to re-blocking asyncs (jump-out-of-prompt-thunk 'from-async-mark) th) (sleep 7))) =2D-8<---------------cut here---------------end--------------->8--- The output is following: =2D-8<---------------cut here---------------start------------->8--- =2D--> going to enter the prompt asyncs unblocked. =3D=3D=3D=3D jumped out of prompt: (from-inside-loop) <--- I'm out of prompt: 0. =2D--> going to enter the prompt asyncs unblocked. =3D=3D=3D=3D jumped out of prompt: (from-inside-loop) <--- I'm out of prompt: 1. =2D--> going to enter the prompt =3D=3D=3D=3D jumped out of prompt: (from-async-mark) <--- I'm out of prompt: 2. =2D--> going to enter the prompt 5 (call-with-blocked-asyncs #) In ice-9/eval.scm: 619:8 4 (_ #(#(#))) 619:8 3 (_ #(#(# #) = 3)) In ice-9/boot-9.scm: 724:2 2 (call-with-prompt tmp # =E2=80=A6) In unknown file: 1 (call-with-unblocked-asyncs #) In ice-9/boot-9.scm: 1685:16 0 (raise-exception _ #:continuable? _) ice-9/boot-9.scm:1685:16: In procedure raise-exception: In procedure call-with-unblocked-asyncs: asyncs already unblocked =2D-8<---------------cut here---------------end--------------->8--- =2D-=20 Best regards, Andrew Tropin --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEKEGaxlA4dEDH6S/6IgjSCVjB3rAFAmUfki8ACgkQIgjSCVjB 3rD1BA//ctsM+M31Fkr2FtceSLJ1y6sM7l0OEMmKt+AxWJKj4Z1Y7bIihp8C41w7 w9yCY4OvSQGf2S7IAxH9IzvviLJ2j+7kQTLRvFTZJsjSVT5uFWUzxJB/E+GP6tZv Lkd59xvZYH2z4b4NeQ52fKF9Tl7tiojlB4v05jrEUxcen88PjWqYGg/tT9VJv4ox +K9+ihghQkFUowUe9wmFnn+eHsikSCT/9nDxZQE9NTGskwj9er2Wc9LOtD1IidWp plc8E7lMZNFtiLKIsNj4gb5iXBZp88tIs08S4WVwD4wHIAfQOhyORH+B/D6pD+3c 5mgZnGTQXHwHJ7eWsleRD1t+pp80E9i0IbEjMMDC2wvKjvOK0HNaAoKmTFKwZ4DH newh/CiwDkK2dKTUxcyw0vb+vMK4SPqshka2RJK4mqTFYecPz6T6cI9axZHBNCWW Ni+TZXSNaJaHVtuJqdas9R54IDIYJ7ufSx1vN0BjvNMjtW6/O923Jb8ag5CGSgcf T26bsz0ofO8cnLOBw9xFGFmnJJ9+4kuh7e1KA9zcbLmNwlxfd/MCGCs+WLLwOWfQ SG/y+UYejlQi4OwCELeLFXuEtoPEcQ957pPHoGjV1MUFd7+RJZzN2RoIeAxL0wec fBhSNa4d7NRWBLvE7NxPOyt5wS77poAcGYeqNPzMr7T03Y0Zvhc= =M1WM -----END PGP SIGNATURE----- --=-=-=--