[racket] tcp-read handlers

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat Jan 18 17:11:47 EST 2014

If you can modify url.rkt for now, try something like this [untested]

;; url.rkt
(define (purify-http-port in-port)
 (define-values (in-pipe out-pipe) (make-pipe))
 (define exn-raised #f)
 (thread
  (λ ()
     (with-handlers ((exn? (lambda (e) (set! exn-raised e)))) ;; do choose the exn you really wish to catch 
       (define status (http-read-status in-port))
       (define chunked? (http-read-headers in-port))
       (http-pipe-data chunked? in-port out-pipe)
       (close-input-port in-port))))
 (or exn-raised in-pipe))





On Jan 18, 2014, at 5:08 PM, Eduardo Bellani wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Btw, forgot to ask, any alternatives that could solve my problem in
> the short term?
> 
> Again, thanks for the attention.
> 
> On 01/18/2014 02:18 PM, Matthias Felleisen wrote:
>> 
>> To supplement Matthew's response, see exn style:
>> 
>> http://plt.eecs.northwestern.edu/snapshots/current/doc/style/Choosing_the_Right_Construct.html#%28part._.Exceptions%29
>> 
>> This does not address the problem part.
>> 
>> On Jan 18, 2014, at 11:04 AM, Matthew Flatt wrote:
>> 
>>> The `exn?` predicate would catch all exceptions from the base 
>>> libraries.[*]
>>> 
>>> I think the problem may be that the underlying TCP port is read
>>> in a separate decoding thread, and so the exception and printing
>>> happens in that thread. If I'm right, then that seems like a flaw
>>> in our implementation of decoding.
>>> 
>>> [*] To avoid interfering with the `exn:break?` exceptions
>>> triggered by Ctl-C, you almost always want `exn:fail?` instead of
>>> just `exn?`.
>>> 
>>> At Sat, 18 Jan 2014 09:50:15 -0200, Eduardo Bellani wrote:
>> Is there a way to install a handler for the tcp-error exception? I
>> have a function like this
>> 
>> (define (get-page link-url (max-tries 5)) (let loop ([current-try
>> 0]) (with-handlers ([exn? (? (e) (if (> current-try max-tries) 
>> (error 'get-page "failed because it exceeded ~a tries" max-tries) 
>> (begin (sleep 2) (loop (add1 current-try)))))]) (get-pure-port
>> link-url))))
>> 
>> that I'm using to crawl some data, but sometimes it's printing:
>> 
>> tcp-read: error reading system error: Connection reset by peer;
>> errno=104 context...: /usr/racket/collects/net/url.rkt:392:3
>> 
>> Thanks for the attention.
>> 
>>>> ____________________ Racket Users list: 
>>>> http://lists.racket-lang.org/users
>>> ____________________ Racket Users list: 
>>> http://lists.racket-lang.org/users
>> 
> 
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.14 (GNU/Linux)
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
> 
> iEYEARECAAYFAlLa+0AACgkQSbLl0kCTjGm2swCfaFcrArWUUWp/4nMXR06wK8bn
> /yMAniLAl9bWU28Y1FVkrBKtkniFGohS
> =F4Li
> -----END PGP SIGNATURE-----



Posted on the users mailing list.