Skip to content

Commit 82fc83d

Browse files
committed
[fix] Verify expected server-ch identity when updating conns
As a matter of hygiene, ensure that handlers only ever operate on the expected server-ch.
1 parent c6deca6 commit 82fc83d

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

src/taoensso/sente.cljc

+21-28
Original file line numberDiff line numberDiff line change
@@ -756,24 +756,21 @@
756756

757757
(send-handshake! server-ch websocket?)
758758

759-
;; Start ws-kalive loop
760-
;; This also works to gc ws conns that were suddenly
761-
;; terminated (e.g. by turning on airplane mode)
759+
;; Start server-side ws-kalive loop
760+
;; Also helps server detect broken conns earlier
762761
(when-let [ms ws-kalive-ms]
763762
(go-loop [udt-t0 udt-open]
764763
(<! (async/timeout ms))
765-
(when-let [[_sch udt-t1] (get-in @conns_ [:ws uid client-id])]
766-
(when (interfaces/sch-open? server-ch)
767-
;; (assert (= _sch server-ch))
764+
(when-let [[sch udt-t1] (get-in @conns_ [:ws uid client-id])]
765+
(when (identical? sch server-ch)
768766
(when (= udt-t1 udt-t0)
769-
;; Ref. issue #230:
770-
;; We've seen no send/recv activity on this
771-
;; conn w/in our kalive window so send a ping
772-
;; ->client (should auto-close conn if it's
773-
;; gone dead).
774-
(interfaces/sch-send! server-ch websocket?
775-
(pack packer :chsk/ws-ping)))
776-
(recur udt-t1))))))
767+
;; Ref. #230
768+
;; No conn send/recv activity w/in kalive window
769+
;; so send ping to client. Should trigger close
770+
;; if conn is broken.
771+
(interfaces/sch-send! server-ch websocket?
772+
(pack packer :chsk/ws-ping)))
773+
(recur udt-t1))))))
777774

778775
;; Ajax handshake/poll
779776
(let [_ (tracef "New Ajax handshake/poll: %s (%s)" uid sch-uuid)
@@ -791,10 +788,8 @@
791788
(when-let [ms lp-timeout-ms]
792789
(go
793790
(<! (async/timeout ms))
794-
(when-let [[_sch udt-t1] (get-in @conns_ [:ajax uid client-id])]
795-
(when (= udt-t1 udt-open)
796-
;; (assert (= _sch server-ch))
797-
;; Appears to still be the active sch
791+
(when-let [[sch udt-t1] (get-in @conns_ [:ajax uid client-id])]
792+
(when (and (= identical? server-ch) (= udt-t1 udt-open))
798793
(interfaces/sch-send! server-ch websocket?
799794
(pack packer :chsk/timeout))))))))))
800795

@@ -828,8 +823,8 @@
828823
(<! (async/timeout 5000)) ; TODO Configurable
829824
(let [disconnect? ; Removed entry for client-id?
830825
(swap-in! conns_ [conn-type uid client-id]
831-
(fn [[_sch udt-t1]]
832-
(if (= udt-t1 udt-close)
826+
(fn [[sch udt-t1]]
827+
(if (and (identical? sch server-ch) (= udt-t1 udt-close))
833828
(swapped :swap/dissoc true)
834829
(swapped :swap/abort false))))]
835830

@@ -1358,21 +1353,19 @@
13581353
(reset! retry-count_ 0)
13591354
(connect-fn)
13601355

1356+
;; Start client-side ws-kalive loop
1357+
;; Also helps client detect broken conns earlier
13611358
(when-let [ms ws-kalive-ms]
13621359
(go-loop []
13631360
(let [udt-t0 @udt-last-comms_]
13641361
(<! (async/timeout ms))
13651362
(when (have-handle?)
13661363
(let [udt-t1 @udt-last-comms_]
13671364
(when (= udt-t0 udt-t1)
1368-
;; Ref. issue #259:
1369-
;; We've seen no send/recv activity on this
1370-
;; conn w/in our kalive window so send a ping
1371-
;; ->server (should auto-close conn if it's
1372-
;; gone dead). The server generally sends pings so
1373-
;; this should be rare. Mostly here to help clients
1374-
;; identify conns that were suddenly dropped.
1375-
1365+
;; Ref. #259
1366+
;; No conn send/recv activity w/in kalive window
1367+
;; so send ping to server. Should trigger close
1368+
;; if conn is broken.
13761369
(-chsk-send! chsk [:chsk/ws-ping] {:flush? true})))
13771370
(recur)))))
13781371

0 commit comments

Comments
 (0)