Skip to content

Fixed deadlock in the proxy #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,33 @@ type client struct {
done time.Time
}

func (c *client) copyTo(conn net.Conn) {
func (c *client) copyTo(conn net.Conn, done chan bool) {
io.Copy(conn, c.conn)
c.w.Done()
done <- true

}

func (c *client) copyFrom(conn net.Conn) {
func (c *client) copyFrom(conn net.Conn, done chan bool) {
io.Copy(c.conn, conn)
c.w.Done()
done <- true

}

func (c *client) copyAll() {
go c.copyTo(c.server)
go c.copyFrom(c.server)
// Wait for both copy operations to complete
c.w.Wait()
done := make(chan bool)

go c.copyTo(c.server, done)
go c.copyFrom(c.server, done)

//We wait until both ONE is done (an error or EOF can lead one of the sides to be done)

<-done
// Record when we finished. This way we won't report any of the post
// processing time that we took in the logs
c.done = time.Now()

}

func (c *client) doProxy() {
Expand Down Expand Up @@ -123,8 +132,10 @@ func (c *client) setup() {
}

func (c *client) teardown() {
log.Println("Teardown start")
c.conn.Close()
c.server.Close()
log.Println("Proxy connection closed")
// Lock our condition to avoid races when updating the active variable
wCond.L.Lock()
// Record that we're no longer active
Expand Down