From 5ab8a3ff1e1042733255a487e9ce836620a084bc Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 17 Apr 2018 14:15:26 +1000 Subject: [PATCH] ConnectionPool: monitor idle connections When a connection is returned to the (read) pool, add a monitor to it for receiving unexpected data (or EOF), and kill / close the Connection object if any activity occurs before the next write (when it should have simply been waiting idle in the pool) per https://github.com/JuliaWeb/MbedTLS.jl/pull/145#issuecomment-381832442 closes #214 closes #199 closes #220 closes JuliaWeb/GitHub.jl#106 --- src/ConnectionPool.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 06b21ecf1..81be09181 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -265,6 +265,20 @@ function IOExtras.closeread(t::Transaction) notify(poolcondition) @ensure !isreadable(t) + + # (re)start a watcher for idle connections in the pool + # short-circuit if it is already dead or has already started to be reused + if isopen(t.c) && !t.c.writebusy && (t.c.writecount <= t.sequence + 1) # && !isreadable(t) + @schedule begin + if !eof(t.c.io) && # wait to see if receive any more data + !t.c.readbusy && # and aren't actively expecting to receive data + !t.c.writebusy && (t.c.writecount <= t.sequence + 1) # nor have returned from startwrite and/or called closewrite again + # other end must be in an invalid state, or terminated the connection: + # drop our end from the pool also + close(t.c) + end + end + end return end