From c0326ecfab268ef8d231bfd5080103169148c350 Mon Sep 17 00:00:00 2001 From: xiaofan Date: Mon, 28 Nov 2016 13:32:11 +0800 Subject: [PATCH] fix concurrent map write panic when init --- cronexpr_parse.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cronexpr_parse.go b/cronexpr_parse.go index aeb8296..64c9a84 100644 --- a/cronexpr_parse.go +++ b/cronexpr_parse.go @@ -19,6 +19,7 @@ import ( "regexp" "sort" "strings" + "sync" ) /******************************************************************************/ @@ -193,6 +194,7 @@ var ( layoutDowOfSpecificWeek = `^(%value%)#([1-5])$` fieldFinder = regexp.MustCompile(`\S+`) entryFinder = regexp.MustCompile(`[^,]+`) + layoutRegexpLock = sync.Mutex{} layoutRegexp = make(map[string]*regexp.Regexp) ) @@ -482,8 +484,14 @@ func makeLayoutRegexp(layout, value string) *regexp.Regexp { layout = strings.Replace(layout, `%value%`, value, -1) re := layoutRegexp[layout] if re == nil { - re = regexp.MustCompile(layout) - layoutRegexp[layout] = re + // double check locking to fix issue: https://github.com/gorhill/cronexpr/issues/19 + layoutRegexpLock.Lock() + defer layoutRegexpLock.Unlock() + re = layoutRegexp[layout] + if re == nil { + re = regexp.MustCompile(layout) + layoutRegexp[layout] = re + } } return re }