check and log errors more often in deferred cleanup calls, and log remote-induced errors at lower priority

We normally check errors for all operations. But for some cleanup calls, eg
"defer file.Close()", we didn't. Now we also check and log most of those.
Partially because those errors can point to some mishandling or unexpected code
paths (eg file unexpected already closed). And in part to make it easier to use
"errcheck" to find the real missing error checks, there is too much noise now.

The log.Check function can now be used unconditionally for checking and logging
about errors. It adjusts the log level if the error is caused by a network
connection being closed, or a context is canceled or its deadline reached, or a
socket deadline is reached.
This commit is contained in:
Mechiel Lukkien
2025-03-24 13:46:08 +01:00
parent 15a8ce8c0b
commit a2c79e25c1
38 changed files with 337 additions and 161 deletions

View File

@ -1119,9 +1119,11 @@ func OpenAccountDB(log mlog.Log, accountDir, accountName string) (a *Account, re
defer func() {
if rerr != nil {
db.Close()
err := db.Close()
log.Check(err, "closing database file after error")
if isNew {
os.Remove(dbpath)
err := os.Remove(dbpath)
log.Check(err, "removing new database file after error")
}
}
}()
@ -1820,17 +1822,26 @@ func (a *Account) CheckConsistency() error {
conf, _ := a.Conf()
if conf.JunkFilter != nil {
random := make([]byte, 16)
cryptorand.Read(random)
if _, err := cryptorand.Read(random); err != nil {
return fmt.Errorf("reading random: %v", err)
}
dbpath := filepath.Join(mox.DataDirPath("tmp"), fmt.Sprintf("junkfilter-check-%x.db", random))
bloompath := filepath.Join(mox.DataDirPath("tmp"), fmt.Sprintf("junkfilter-check-%x.bloom", random))
os.MkdirAll(filepath.Dir(dbpath), 0700)
defer os.Remove(dbpath)
defer os.Remove(bloompath)
defer func() {
err := os.Remove(bloompath)
log.Check(err, "removing temp bloom file")
err = os.Remove(dbpath)
log.Check(err, "removing temp junk filter database file")
}()
jf, err = junk.NewFilter(ctx, log, conf.JunkFilter.Params, dbpath, bloompath)
if err != nil {
return fmt.Errorf("new junk filter: %v", err)
}
defer jf.Close()
defer func() {
err := jf.Close()
log.Check(err, "closing junk filter")
}()
}
var ntrained int
@ -1987,7 +1998,10 @@ func (a *Account) CheckConsistency() error {
if err != nil {
return fmt.Errorf("open account junk filter: %v", err)
}
defer ajf.Close()
defer func() {
err := ajf.Close()
log.Check(err, "closing junk filter")
}()
wordsGot, err := load(ajf)
if err != nil {
return fmt.Errorf("read account junk filter: %v", err)

View File

@ -423,7 +423,10 @@ func (a *Account) AssignThreads(ctx context.Context, log mlog.Log, txOpt *bstore
buf := headerbuf[:int(size)]
err := func() error {
mr := a.MessageReader(m)
defer mr.Close()
defer func() {
err := mr.Close()
log.Check(err, "closing message reader")
}()
// ReadAt returns whole buffer or error. Single read should be fast.
n, err := mr.ReadAt(buf, partialPart.HeaderOffset)