Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fatal error: concurrent map writes #60

Open
dayadev opened this issue Jul 22, 2019 · 1 comment
Open

fatal error: concurrent map writes #60

dayadev opened this issue Jul 22, 2019 · 1 comment

Comments

@dayadev
Copy link

dayadev commented Jul 22, 2019

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
@icholy
Copy link

icholy commented Nov 4, 2019

Message should have a Clone() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants