I must have tried it wrong the first time, because I see what you see. I'm surprised that `with-limits` doesn't shut down a custodian on a timeout, but you can set up that termination yourself:
(parameterize ([current-subprocess-custodian-mode 'kill] [current-custodian (make-custodian)]) (with-handlers ([exn:fail:resource? (λ (e) (displayln "Timed out"))]) (with-limits 5 #f (system "sleep 10 && echo Finished"))) (custodian-shutdown-all (current-custodian))) Beware that the subprocess that's killed is actually the shell process, and not any subsubprocess the shell is running. Your actual use case may need to use `system*` to avoid the intermediate shell process. Beware also that Unix doesn't give you a way to reliably kill off the subprocess and any others that it may have started. At Fri, 20 Jul 2018 15:42:35 -0500, Philip McGrath wrote: > I thought `current-subprocess-custodian-mode` would be the solution, but > when I just tried your code (with Racket 6.12 on Mac OS), it first printed > "Timed out", then printed “Finished” after a brief delay. > > I get the same result if I put the `parameterize` directly around the call > to `system`, and this happens with both DrRacket and the command-line > racket repl. > > > -Philip > > On Fri, Jul 20, 2018 at 3:36 PM, Matthew Flatt <mfl...@cs.utah.edu> wrote: > > > At Fri, 20 Jul 2018 16:28:22 -0400, "'Leandro Facchinetti' via Racket > > Users" wrote: > > > Hi, > > > > > > I want to run a subprocess with a timeout. Here’s what I tried: > > > > > > #lang racket > > > (require racket/sandbox) > > > > > > (with-handlers ([exn:fail:resource? (λ (e) (displayln "Timed out"))]) > > > (with-limits 5 #f (system "sleep 10 && echo Finished"))) > > > > > > This doesn’t work, it prints both “Timed out” after 5 seconds and > > “Finished” > > > after 10 seconds. I wish ‘with-limits’ would stop the underlying > > process, so > > > I’d never see “Finished”. How should I do that? > > > > You can use `current-subprocess-custodian-mode`: > > > > #lang racket > > (require racket/sandbox) > > > > (parameterize ([current-subprocess-custodian-mode 'kill]) > > (with-handlers ([exn:fail:resource? (λ (e) (displayln "Timed out"))]) > > (with-limits 5 #f (system "sleep 10 && echo Finished")))) -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.