unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Tomas Volf <~@wolfsden.cz>
To: guile-user@gnu.org
Subject: How to abort a read from a socket after some time?
Date: Sun, 21 Jan 2024 17:00:23 +0100	[thread overview]
Message-ID: <Za0_ly1K7l3RV1hJ@ws> (raw)

[-- Attachment #1: Type: text/plain, Size: 3855 bytes --]

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 (λ () (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 (λ () (read-char s))))

That does hang for a long time:

    read-char:
    Alarm!
    ;; Result: #<eof>
    ;; 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 (λ () (read-char s))))

It still does not work:

    read-char (non-block):
    Alarm!
    ;; Result: #<eof>
    ;; 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 (λ () (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 (λ () (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 (λ () (read-char s))))


--
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

             reply	other threads:[~2024-01-21 16:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-21 16:00 Tomas Volf [this message]
2024-01-21 18:34 ` How to abort a read from a socket after some time? Maxim Cournoyer
2024-01-21 18:57   ` Basile Starynkevitch
2024-01-22 11:52     ` Tomas Volf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Za0_ly1K7l3RV1hJ@ws \
    --to=~@wolfsden.cz \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).