Do not try to read from the xml stream if it's going to be closed.

This commit is contained in:
Martin Dosch 2024-04-02 16:32:30 +02:00
parent 6e5d6e449e
commit ca4e49201e

View File

@ -79,6 +79,7 @@ type Client struct {
jid string // Jabber ID for our connection jid string // Jabber ID for our connection
domain string domain string
nextMutex sync.Mutex // Mutex to prevent multiple access to xml.Decoder nextMutex sync.Mutex // Mutex to prevent multiple access to xml.Decoder
shutdown bool // Variable signalling that the stream will be closed
p *xml.Decoder p *xml.Decoder
stanzaWriter io.Writer stanzaWriter io.Writer
LimitMaxBytes int // Maximum stanza size (XEP-0478: Stream Limits Advertisement) LimitMaxBytes int // Maximum stanza size (XEP-0478: Stream Limits Advertisement)
@ -338,6 +339,7 @@ func NewClientNoTLS(host, user, passwd string, debug bool) (*Client, error) {
// Close closes the XMPP connection // Close closes the XMPP connection
func (c *Client) Close() error { func (c *Client) Close() error {
if c.conn != (*tls.Conn)(nil) { if c.conn != (*tls.Conn)(nil) {
c.shutdown = true
fmt.Fprintf(c.stanzaWriter, "</stream:stream>\n") fmt.Fprintf(c.stanzaWriter, "</stream:stream>\n")
go func() { go func() {
<-time.After(10 * time.Second) <-time.After(10 * time.Second)
@ -1526,6 +1528,11 @@ type rosterItem struct {
// Scan XML token stream to find next StartElement. // Scan XML token stream to find next StartElement.
func (c *Client) nextStart() (xml.StartElement, error) { func (c *Client) nextStart() (xml.StartElement, error) {
for { for {
// Do not read from the stream if it's
// going to be closed.
if c.shutdown {
return xml.StartElement{}, io.EOF
}
c.nextMutex.Lock() c.nextMutex.Lock()
to, err := c.p.Token() to, err := c.p.Token()
if err != nil || to == nil { if err != nil || to == nil {