From aef1257ed151a6758851ca4e6d978e291554566a Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Thu, 28 Mar 2024 17:22:02 +0100 Subject: [PATCH] Fix timeout when server doesn't reply with closing stream element. --- xmpp.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/xmpp.go b/xmpp.go index 72a49b0..986a97d 100644 --- a/xmpp.go +++ b/xmpp.go @@ -339,23 +339,22 @@ func NewClientNoTLS(host, user, passwd string, debug bool) (*Client, error) { func (c *Client) Close() error { if c.conn != (*tls.Conn)(nil) { fmt.Fprintf(c.stanzaWriter, "\n") + go func() { + <-time.After(10 * time.Second) + c.conn.Close() + }() // Wait for the server also closing the stream. for { - select { - case <-time.After(10 * time.Second): - break - default: - ee, err := c.nextEnd() - // If the server already closed the stream it is - // likely to receive an error when trying to parse - // the stream. Therefore the connection is also closed - // if an error is received. - if err != nil { - return c.conn.Close() - } - if ee.Name.Local == "stream" { - return c.conn.Close() - } + ee, err := c.nextEnd() + // If the server already closed the stream it is + // likely to receive an error when trying to parse + // the stream. Therefore the connection is also closed + // if an error is received. + if err != nil { + return c.conn.Close() + } + if ee.Name.Local == "stream" { + return c.conn.Close() } } }