go-gomail / gomail

The best way to send emails in Go.
MIT License
4.39k stars 579 forks source link

fatal error: concurrent map writes #140

Open dayadev opened 5 years ago

dayadev commented 5 years ago

I use gomail in one of my projects and when I try to send emails concurrently I get a fatal error : concurrent map writes

Below is my code to send an email

func (s *Service) sendEmail(notification *entities.Notification, m gomail.Message, errorNotificationIDs *[]int64, processedNotificationIDs *[]int64, wg *sync.WaitGroup) {
    m.SetHeader("To", notification.Recipient)
    if err := s.emailer.DialAndSend(&m); err != nil {
        *errorNotificationIDs = append(*errorNotificationIDs, notification.ID)
    } else {
        *processedNotificationIDs = append(*processedNotificationIDs, notification.ID)
    }
    wg.Done()
}
//Usage of the above function

for i := 0; i < len(notifications); i += 10 {
        batch := notifications[i:helper.Min(i+10, len(notifications))]
        for _, notification := range batch {
            wg.Add(1)
            go s.sendEmail(notification, *m, &errorNotificationIDs, &processedNotificationIDs, &wg)
        }
    }
    wg.Wait()

Panics like below

fatal error: concurrent map writes

goroutine 153 [running]:
runtime.throw(0x1c8d213, 0x15)
    /usr/local/go/src/runtime/panic.go:616 +0x81 fp=0xc4203fb550 sp=0xc4203fb530 pc=0x1031551
runtime.mapassign_faststr(0x1b80be0, 0xc42041d0b0, 0x1c7fb31, 0x2, 0x1)
    /usr/local/go/src/runtime/hashmap_fast.go:779 +0x4bd fp=0xc4203fb5d8 sp=0xc4203fb550 pc=0x100f9cd
gopkg.in/gomail%2ev2.(*Message).SetHeader(0xc4202e7340, 0x1c7fb31, 0x2, 0xc420254880, 0x1, 0x1)
    /Users/abcd/go/src/gopkg.in/gomail.v2/message.go:103 +0x80 fp=0xc4203fb618 sp=0xc4203fb5d8 pc=0x1409570
email.(*Service).sendEmail(0xc4204b2d90, 0xc420343540, 0xc42041d0b0, 0xc42000e028, 0x1, 0x1, 0xc42000e040, 0x1, 0x1, 0xc42000e030, ...)
    /Users/abcd/go/src/email/email.go:327 +0x134 fp=0xc4203fb6d8 sp=0xc4203fb618 pc=0x1a81b94
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2361 +0x1 fp=0xc4203fb6e0 sp=0xc4203fb6d8 pc=0x105eac1
created by email.(*Service).handleEmailMessage
    /Users/abcd/go/src/email/email.go:166 +0x15e2
pedromorgan commented 5 years ago

@dayadev see #104 and #108

dayadev commented 5 years ago

@dayadev see #104 and #108

I get a 500 error on both the links

dayadev commented 5 years ago

@dayadev see #104 and #108

@pedromorgan Is this an existing issue?

pedromorgan commented 5 years ago

sorry.. no idea.. but this project is unmaintained for years, so you are unlikely to get answer there..