Improve closing the stream.

This commit is contained in:
Martin Dosch 2024-03-03 12:10:45 +01:00
parent aed021cf3e
commit c051d69509

View File

@ -338,15 +338,18 @@ func (c *Client) Close() error {
break break
default: default:
ee, err := nextEnd(c.p) ee, err := nextEnd(c.p)
// 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 { if err != nil {
return err return c.conn.Close()
} }
if ee.Name.Local == "stream" { if ee.Name.Local == "stream" {
return c.conn.Close() return c.conn.Close()
} }
} }
} }
return c.conn.Close()
} }
return nil return nil
} }
@ -1438,6 +1441,7 @@ func nextStart(p *xml.Decoder) (xml.StartElement, error) {
// Scan XML token stream to find next EndElement // Scan XML token stream to find next EndElement
func nextEnd(p *xml.Decoder) (xml.EndElement, error) { func nextEnd(p *xml.Decoder) (xml.EndElement, error) {
p.Strict = false
for { for {
t, err := p.Token() t, err := p.Token()
if err != nil || t == nil { if err != nil || t == nil {