fix data race in code for logging login attempts

logging of login attempts happens in the background, because we don't want to
block regular operation with disk since for such logging. however, when a line
is logged, we evaluate some attributes of a connection, notably the username.
but about when we do authentication, we change the username on a connection. so
we were reading and writing at the same time. this is now fixed by evaluating
the attributes before we pass off the logger to the goroutine.

found by the go race detector.
This commit is contained in:
Mechiel Lukkien
2025-02-19 15:23:19 +01:00
parent de6262b90a
commit cbe5bb235c
8 changed files with 89 additions and 48 deletions

View File

@ -17,12 +17,19 @@ func TestLoginAttempt(t *testing.T) {
mox.MustLoadConfig(true, false)
xctx, xcancel := context.WithCancel(ctxbg)
defer xcancel() // Stop clearing of LoginAttempts.
err := Init(xctx)
tcheck(t, err, "store init")
// Stop the background LoginAttempt writer for synchronous tests.
xcancel()
<-writeLoginAttemptStopped
stopc := make(chan struct{})
writeLoginAttemptStop <- stopc
<-stopc
defer func() {
// Ensure Close() below finishes
go func() {
c := <-writeLoginAttemptStop
c <- struct{}{}
}()
err := Close()
tcheck(t, err, "store close")
}()