stun icon indicating copy to clipboard operation
stun copied to clipboard

Close hangs app when WithNoConnClose

Open adsko opened this issue 4 years ago • 4 comments

When stun connection is created from existing connection, and WithNoConnClose is provided, Close hangs app.

adsko avatar Apr 05 '21 20:04 adsko

I have the same issue. Debugging showed that Close waits for readUntilClosed go-routing to exit, but that go-routine is blocked on ReadFrom call here https://github.com/pion/stun/blob/0f7f72b905d54eaf8e477af78c6a3d42f54ea217/client.go#L327

Workaround is to call SetReadDeadline(time.Now()) before calling Close for the stun.Client. It causes ReadFrom to immediately exit.

hashok avatar Apr 15 '21 15:04 hashok

@hashok Calling SetReadDeadline(time.Now()) closes the connection.

adsko avatar Apr 15 '21 19:04 adsko

@adsko It shouldn't do that. Make sure you call SetReadDeadline(time.Time{}) on the underlying connection after stun connection is closed to restore the original behavior of the underlying connection.

hashok avatar Apr 16 '21 13:04 hashok

How do we fix this issue?

Setting the read deadline in Close() is not feasible since the connection only implements the following interface:

// Connection wraps Reader, Writer and Closer interfaces.
type Connection interface {
	io.Reader
	io.Writer
	io.Closer
}

We could call c.wg.Done() manually to unblock the c.wg.Wait() in Close(). However, that would leave us with a leaked Goroutine and might cause some panic if the connection still ends up receiving data while the client has been closed?

stv0g avatar Nov 15 '22 13:11 stv0g