Ensure safety before reuse of pipe

This commit is contained in:
Tevin Zhang 2019-02-12 14:59:09 +08:00
parent 201333fdee
commit 8e82d76788
2 changed files with 16 additions and 1 deletions

View File

@ -177,7 +177,10 @@ func (c *Checker) CheckAddrZeroLinger(addr string, timeout time.Duration, zeroLi
func (c *Checker) waitConnectResult(fd int, timeout time.Duration) error {
// get a pipe of connect result
resultPipe := c.getPipe()
defer c.putBackPipe(resultPipe)
defer func() {
c.deregisterResultPipe(fd)
c.putBackPipe(resultPipe)
}()
// this must be done before registerEvents
c.registerResultPipe(fd, resultPipe)
@ -190,6 +193,10 @@ func (c *Checker) waitConnectResult(fd int, timeout time.Duration) error {
return c.waitPipeTimeout(resultPipe, timeout)
}
func (c *Checker) deregisterResultPipe(fd int) {
c.fdResultPipes.Delete(fd)
}
func (c *Checker) registerResultPipe(fd int, pipe chan error) {
// NOTE: the pipe should have been put back if c.fdResultPipes[fd] exists.
c.fdResultPipes.Store(fd, pipe)

View File

@ -19,5 +19,13 @@ func (p *pipePool) getPipe() chan error {
}
func (p *pipePool) putBackPipe(pipe chan error) {
p.cleanPipe(pipe)
p.pool.Put(pipe)
}
func (p *pipePool) cleanPipe(pipe chan error) {
select {
case <-pipe:
default:
}
}