From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Tomas Volf <~@wolfsden.cz> Newsgroups: gmane.lisp.guile.user Subject: How to abort a read from a socket after some time? Date: Sun, 21 Jan 2024 17:00:23 +0100 Message-ID: Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="F+b930sb4Lufg5JB" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="3584"; mail-complaints-to="usenet@ciao.gmane.io" To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Jan 21 17:00:58 2024 Return-path: Envelope-to: guile-user@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 1rRaFl-0000kM-Tm for guile-user@m.gmane-mx.org; Sun, 21 Jan 2024 17:00:58 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rRaFQ-0002vV-No; Sun, 21 Jan 2024 11:00:36 -0500 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 <~@wolfsden.cz>) id 1rRaFO-0002vA-1n for guile-user@gnu.org; Sun, 21 Jan 2024 11:00:34 -0500 Original-Received: from wolfsden.cz ([37.205.8.62]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <~@wolfsden.cz>) id 1rRaFL-0005yQ-JI for guile-user@gnu.org; Sun, 21 Jan 2024 11:00:33 -0500 Original-Received: by wolfsden.cz (Postfix, from userid 104) id F01BD2738B2; Sun, 21 Jan 2024 16:00:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=wolfsden.cz; s=mail; t=1705852825; bh=SgixF/8zH6bWW1mk9r1tvi5/ecIRKmq04dDv4SPLBAQ=; h=Date:From:To:Subject; b=vNQHPwxxgffA5MwqdgKehunBFgUkR5K6bqjRE0qncw4v8ijgb/gbeP1ycF5zQ752C ZKyiH9KMpay+gOpAsOc9+E/dTnRzXWazVjbj1JTKEzazEWMdoHPCWMKlkwDgZJ9D+f 2y2fv2NxUHwSLFonGSdBVCml1cBIexA0tRBh3lVq7Cz8rhnU5pxUfb8wIuTSkAhOgS Qf80q53B5k17640oEKUJRHGxvzix2xxD8F57u80Nr2kfmMZ+3Z+GX1VS143eiYHAd9 YrdUrBJWW8VLlrz+EzCMSmWgwXPAcmFJt23eVY0ZSE6yjhWH1cZGwsaVKuBZ+fC8Ye nSRw8IrHSmolyZ3Tl2r1u9KQV9iUzyXhpE5lddNeie5DvnEdYWmZoqTzqG4JQjOCxp zlC3Bphfm9vNPVSwYxIFG83EiheBQ0C+o4MhIQ2Nd7zlgf9Lj2SkFNBZIZrrCI62Ax 9XqJ4TElq+qSKU/UkCeLoddaHwtjbN+Zh1XUcxxE9TaLbpo9ZSli3EMpXEQvXRwVm2 v9zZla7B/vTD8hp4hJMiCD2LVrWun/QERDhwJZ22Vca7WkZL+WIAoBDgCp0q0GFjVi wApJHaGFPcU9L3P5BRSIRLP5dWV4SmLTwIKVXFf4XVgBB4p2CCJnZcnw1MTo2Fl/nh kbRA4JrfGgKP09NiH3LggneU= Original-Received: from localhost (unknown [146.70.134.133]) by wolfsden.cz (Postfix) with ESMTPSA id 491EA273147 for ; Sun, 21 Jan 2024 16:00:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=wolfsden.cz; s=mail; t=1705852824; bh=SgixF/8zH6bWW1mk9r1tvi5/ecIRKmq04dDv4SPLBAQ=; h=Date:From:To:Subject; b=yocthpW/zp9dijbDEmzBdUvUAMsIKnkstffC7HApRSn/HikH85bsTDY844uhxtLuf wYxlMMC3xF44BBFa+h09KPt0YtbcL3ohXNMBvgVWiKoHQVB2e0LpN+77uBEajR1zPP lyADjbjIe8ouOSEGmIQppUN37btqPX08GvO8iuZDywKhmRExNldjKl17ud7WuKRNYN OHcVuMQ+MRnQkN11Xfh5EREpBQvmn8Ah9FJiBqHKYWudVuFfevuUXtC/FPjtk8goLy N4ziyXLKbYGCFX1jNFYkebjSrifRFp7MjoF6GxyVa8UOI0MJ5XTtAIEl3Y3DZwoXJZ jPT6nnd1oiwkk0KCblYrXdtCJXmZRj/VXLLgfLWbf4anZjJt6qF5VE2gIT8StOYBz/ YYc2cwN0njpqzVt/elL9n+bxGs/RQHQqMHOe+lgLKIaP2rIkQ1RHb3/Owfz7pg0psI fY/g3akwJgnSZ9VDGcD9k2ypvc7+AtO+m2J6Poppx8HAK1p5vUu/P0k6O1PZkLdcSM 7tkRV579Qs5LSg1SYsgQrSNCW1kq9oTK/ULz359YgGH9H+pecs90+GSKVSAOlgPW0E fTZ2kZbFUQ707dxU7/pOOkDMwmzl2F1r80WTnKRyBm3cZH7lG57OD2rj73cHvlT1Q+ dsy/kSmOpmfCl2OVwTzh7cpc= Mail-Followup-To: guile-user@gnu.org Content-Disposition: inline Received-SPF: pass client-ip=37.205.8.62; envelope-from=~@wolfsden.cz; helo=wolfsden.cz X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.user:19409 Archived-At: --F+b930sb4Lufg5JB Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hello, I am trying to figure out how to abort a read from a socket after some time elapses. I failed to figure out how to do so. All code below runs after handler is set: (sigaction SIGALRM (lambda _ (display "Alarm!\n"))) The (time) procedure is just modified ,time from the REPL, full code at the= end of the email. I started with the usual C way, alarm, and tested it with a sleep: (display "Sleep:\n") (alarm 1) (time (=CE=BB () (sleep 3))) That does seems to work fine: Sleep: Alarm! ;; Result: 1 ;; 1.000361s real time, 0.000119s run time. 0.000000s spent in GC. However when I moved to the sockets, I quickly hit a wall: (display "read-char:\n") (let* ((ai (car (getaddrinfo "www.gnu.org" "http"))) (s (socket (addrinfo:fam ai) (addrinfo:socktype ai) (addrinfo:protocol ai)))) (connect s (addrinfo:addr ai)) (alarm 1) (time (=CE=BB () (read-char s)))) That does hang for a long time: read-char: Alarm! ;; Result: # ;; 51.742715s real time, 0.000375s run time. 0.000000s spent in GC. The alarm was obviously triggered, but the read-char was not interrupted. I would have expected to receive (maybe as an exception) EINTR. When I try non-blocking code: (display "read-char (non-block):\n") (let* ((ai (car (getaddrinfo "www.gnu.org" "http"))) (s (socket (addrinfo:fam ai) (addrinfo:socktype ai) (addrinfo:protocol ai)))) (connect s (addrinfo:addr ai)) (let ((flags (fcntl s F_GETFL))) (fcntl s F_SETFL (logior O_NONBLOCK flags))) (alarm 1) (time (=CE=BB () (read-char s)))) It still does not work: read-char (non-block): Alarm! ;; Result: # ;; 51.581392s real time, 0.000371s run time. 0.000000s spent in GC. I would have expected to receive EWOULDBLOCK. But let's not get distracted= by the non-blocking variant. So, what would be a good way to do this? Thank you for any advice on this. Have a nice day, Tomas Volf Here is full script I used for playing with this: ;;; I took this code from ,time implementation. (use-modules (ice-9 format)) (define (time thunk) (let* ((gc-start (gc-run-time)) (real-start (get-internal-real-time)) (run-start (get-internal-run-time)) (result (thunk)) (run-end (get-internal-run-time)) (real-end (get-internal-real-time)) (gc-end (gc-run-time))) (define (diff start end) (/ (- end start) 1.0 internal-time-units-per-second)) (format #t ";; Result: ~s\n" result) (format #t ";; ~,6Fs real time, ~,6Fs run time. ~,6Fs spent in GC.\n" (diff real-start real-end) (diff run-start run-end) (diff gc-start gc-end)) result)) (sigaction SIGALRM (lambda _ (display "Alarm!\n"))) (display "Sleep:\n") (alarm 1) (time (=CE=BB () (sleep 3))) (display "read-char:\n") (let* ((ai (car (getaddrinfo "www.gnu.org" "http"))) (s (socket (addrinfo:fam ai) (addrinfo:socktype ai) (addrinfo:protocol ai)))) (connect s (addrinfo:addr ai)) (alarm 1) (time (=CE=BB () (read-char s)))) (display "read-char (non-block):\n") (let* ((ai (car (getaddrinfo "www.gnu.org" "http"))) (s (socket (addrinfo:fam ai) (addrinfo:socktype ai) (addrinfo:protocol ai)))) (connect s (addrinfo:addr ai)) (let ((flags (fcntl s F_GETFL))) (fcntl s F_SETFL (logior O_NONBLOCK flags))) (alarm 1) (time (=CE=BB () (read-char s)))) -- There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors. --F+b930sb4Lufg5JB Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEt4NJs4wUfTYpiGikL7/ufbZ/wakFAmWtP5cACgkQL7/ufbZ/ wakpSg/7BhgoSMjJWstzZkVmKfrRorf20JOy7sO7GADCdvrixsuLGqEm5JFUhfmx QozcZYq/hkxZwb6Zi6wdpSRiCsA3gZpVG+24xVn4eEkHa/uDsfn9qc8Zr8j/kRwD gt8J8c90kVcmZd+PbqjFdIWnm1YC3AjSt9z0yeD3wbc2bSzdcejlBD1lpv1cNIr3 E4Kuq5SVdv3WukfG04YlOFV2Mk84qo9WX+goa+Uf7ITa2gLOrbkQ9OYEGMMZp0K8 TrY3OBo5mq0GrtvIzbfDoRkx3jRI+7rcvxQbN32aKcVn5lslE7nQyS74J5drsFP6 bfFo0KoBk5WXvaTbvNcDx7/XI1zhjjePJE7+t/lWhdiWrMeqIfvnmfz5EYoEwuk+ 1VBlBKVRiKjJdAUg968zWd73Kp6AKBFMX9ETSPzlcPp6MbyQPCR4QO6/8HzGT/5j ZHD+4ZdTS3VSCFciyw7mxcqCeMplvf0jKrWvOSn4L67+AtuNv9DtUItxkiFKNsMz uk6udr69YQjwjy5m1aYdLga/qILgCMyaVh8XbntTlkbLfrcnK87+AhJfYDcn5Xvv YnxnYNnP0rzTGk9/xk3LG6XJS25XGaQlxWbyFAgY3JkDzL2PSE7nXGEFSNraF2JA j1HKFRpiYQG1dwqU+lKYS+a5mRKggcaAMMtMzQaNwYwsJvuDyrM= =3LCt -----END PGP SIGNATURE----- --F+b930sb4Lufg5JB--