experimental control.timeout/with-timeout

Jens Thiele <[email protected]> Tue, 21 Jan 2025 10:02:25 +0100
Newsgroups gmane.lisp.scheme.gauche
Message-ID <[email protected]>
Hi,

very likely you are already aware of this:

some "real-world" examples of imho unexpected behaviour with
control.timeout/with-timeout (one could argue pmap with fully concurrent
mapper + timeout also has this problem)

1) stop motor isn't printed:
gosh> (with-timeout
        (lambda() (unwind-protect (begin (print "start motor")
                                         (sys-sleep 10))
                                  (print "stop motor"))) 1)
start motor
#f

2) timer isn't stopped:
gosh> (use gauche.time)
gosh> (let1 tc (make <real-time-counter>)
	(guard (e [else #?=e])
	       (with-timeout (lambda() (with-time-counter tc
                                         (sys-sleep 10)
                                         (error "foo")))
		 1))
	(time-counter-value tc))
0

looks like guile does run dynamic-wind post thunks before thread
termination (I don't know whether this is really a good idea?):

test.sh:
,----
| #!/bin/bash -xe
| cat > test.scm <<EOF
| (cond-expand
|   (guile
|     (use-modules (srfi srfi-18))
|     (use-modules (srfi srfi-34)))
|   (gauche
|     (use gauche.threads)
|     (define sleep sys-sleep)))
| (let ((t (thread-start!
|           (make-thread (lambda()
|                          (dynamic-wind
|                           (lambda _ #t)
|                           (lambda()
|                             (display "start motor\n")
|                             (sleep 10))
|                           (lambda()
|                             (display "stop motor\n"))))))))
|   (sleep 1)
|   (thread-terminate! t)
|   (guard (e (else (display e) (newline)))
| 	 (thread-join! t)))
| EOF
| guile -s test.scm
| gosh test.scm
`----

./test.sh
+ cat
+ guile -s test.scm
[...]
start motor
stop motor
#<&terminated-thread-exception>
+ gosh test.scm
start motor
#<terminated-thread-exception: #<thread #f (1) terminated ...

jens