go-imap
go-imap copied to clipboard
"use of closed network connection" when running package tests
When running the package tests using go test -v ./...
the following errors occur, seemingly at random:
imap/client: 2020/06/29 01:35:27 error reading response: read tcp 127.0.0.1:37868->127.0.0.1:41003: use of closed network connection
...
imap/server: 2020/06/29 01:35:27 cannot read command: read tcp 127.0.0.1:34511->127.0.0.1:52710: use of closed network connection
The tests do not fail. I have no idea why these errors are produced.
@ThreeFx You had fixed yet. Can you show me an example code?
Closing because this is an issue about go-imap v1. I'm now focusing on go-imap v2.
Please re-open if you can reproduce with go-imap v2.
we met the same error on go-imap v2.
import (
"github.com/emersion/go-imap/v2"
"github.com/emersion/go-imap/v2/imapclient"
)
type Watcher struct {
cli *imapclient.Client
}
func NewWatcher() (*Watcher, error) {
cli, err := imapclient.DialTLS(c.Server, nil)
if err != nil {
return nil, err
}
err = cli.Login(c.Username, c.Password).Wait()
if err != nil {
return nil, err
}
_, err = cli.Select(c.BoxName, nil).Wait()
if err != nil {
return nil, err
}
go w.keepWatch()
return &Watcher{
cli: cli
}
}
func (w *Watcher) keepWatch() {
tk := time.NewTicker(10 * time.Second)
for {
select {
case <-w.exit:
return
case <-tk.C:
res, err := w.cli.Search(cond, nil).Wait() // from since to now
if err != nil {
w.logger.Error("email search failed: ", err)
return
}
ids := res.AllNums()
// do some thing with email content
}
}
}
{"@timestamp":"2023-05-11T18:47:11.180+08:00","caller":"email/email.go:352","content":"email search failed: use of closed network connection","level":"error"}
This code is an example show how I use,In general, I make client and store it in a long running watcher, and every 10s I polling email server to get new email.
This code can normally run 1-2 day, then this "use of closed network connection" will appear, and I have to restart my code which means construct a new watcher, which means a new client.
thx for help 🙇
This is unrelated and just means the TCP connection to your server broke, which is a normal thing to happen.
Yeah, I think so, I'm going to construct a new client before every new round of requests to email server.
thx