Skip to content
This repository was archived by the owner on Nov 28, 2024. It is now read-only.
This repository was archived by the owner on Nov 28, 2024. It is now read-only.

fatal error: concurrent map writes  #60

@dayadev

Description

@dayadev

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions