tonyg / syndicate

syn·di·cate: a language for interactive programs
http://syndicate-lang.org/
GNU Lesser General Public License v3.0
152 stars 11 forks source link

Bug in tcp driver when switching ports #21

Closed tonyg closed 7 years ago

tonyg commented 7 years ago
#lang syndicate/actor
;; Example showing a bug when changing listen port. Connect after the
;; switch to port 5998. You will see two "Welcome" messages! You
;; should only see one. The TCP driver is spawning *two* listeners for
;; the new port, when only one should exist.

(require syndicate/protocol/advertise)
(require/activate syndicate/drivers/tcp)
(require (only-in racket/port read-bytes-line-evt))

(file-stream-buffer-mode (current-output-port) 'none)
(define stdin-evt (read-bytes-line-evt (current-input-port) 'any))

(struct listen-port (number) #:prefab) ;; assertion

(spawn (during (listen-port $port)
         (define server-id (tcp-listener port))
         (assert (advertise (observe (tcp-channel _ server-id _))))
         (during/spawn (advertise (tcp-channel $c server-id _))
                       (assert (advertise (tcp-channel server-id c _)))
                       (on-start
                        (printf "Accepted connection from ~v on port ~a\n" c port)
                        (send! (tcp-channel server-id c
                                            (string->bytes/utf-8
                                             (format "Welcome! You connected to port ~a.\n"
                                                     port)))))
                       (on (message (tcp-channel c server-id $bs))
                           (printf "Discarding input ~v from ~v on port ~a\n" bs c port))
                       (on-stop (printf "Closed connection ~v on port ~a\n" c port)))))

(spawn* (printf "Asserting port 5999...\n")
        (assert! (listen-port 5999))
        (flush!)
        (retract! (listen-port 5999))
        (printf "Switching to port 5998...\n")
        (react
         (assert (listen-port 5998))))
tonyg commented 7 years ago

This one's a doozy. There are two problems:

The former problem I'm punting on for the moment.

The latter:

Upcoming patch harmonizes the treatment of the action-accumulator, and sure enough the double-listener goes away.