From 466a7b8cab7847bcee35aff5c35ed79361cde427 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Thu, 22 Apr 2021 15:48:41 +0300 Subject: [PATCH] Fixed possible data race --- doc/news.ru.texi | 10 ++++++++++ doc/news.texi | 9 +++++++++ ports/nncp/Makefile | 2 +- src/nncp.go | 2 +- src/sp.go | 16 +++++++++++++--- 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/doc/news.ru.texi b/doc/news.ru.texi index ea1c700..aa4f62f 100644 --- a/doc/news.ru.texi +++ b/doc/news.ru.texi @@ -1,6 +1,16 @@ @node Новости @section Новости +@node Релиз 6.4.0 +@subsection Релиз 6.4.0 +@itemize + +@item +Исправлена возможная гонка в online протоколе, приводящая к падению +программы. + +@end itemize + @node Релиз 6.3.0 @subsection Релиз 6.3.0 @itemize diff --git a/doc/news.texi b/doc/news.texi index 87be652..67340e2 100644 --- a/doc/news.texi +++ b/doc/news.texi @@ -3,6 +3,15 @@ See also this page @ref{Новости, on russian}. +@node Release 6.4.0 +@section Release 6.4.0 +@itemize + +@item +Fixed possible race in online protocol, that lead to panic. + +@end itemize + @node Release 6.3.0 @section Release 6.3.0 @itemize diff --git a/ports/nncp/Makefile b/ports/nncp/Makefile index bb053c3..c398830 100644 --- a/ports/nncp/Makefile +++ b/ports/nncp/Makefile @@ -1,5 +1,5 @@ PORTNAME= nncp -DISTVERSION= 6.3.0 +DISTVERSION= 6.4.0 CATEGORIES= net MASTER_SITES= http://www.nncpgo.org/download/ diff --git a/src/nncp.go b/src/nncp.go index 12edaa4..7c1b7c5 100644 --- a/src/nncp.go +++ b/src/nncp.go @@ -40,7 +40,7 @@ along with this program. If not, see .` const Base32Encoded32Len = 52 var ( - Version string = "6.3.0" + Version string = "6.4.0" Base32Codec *base32.Encoding = base32.StdEncoding.WithPadding(base32.NoPadding) ) diff --git a/src/sp.go b/src/sp.go index e6e4900..6d31375 100644 --- a/src/sp.go +++ b/src/sp.go @@ -233,6 +233,7 @@ type SPState struct { onlyPkts map[[32]byte]bool writeSPBuf bytes.Buffer fds map[string]FdAndFullSize + fdsLock sync.RWMutex fileHashers map[string]*HasherAndOffset checkerQueues SPCheckerQueues sync.RWMutex @@ -669,11 +670,12 @@ func (state *SPState) StartR(conn ConnDeadlined) error { } func (state *SPState) closeFd(pth string) { - s, exists := state.fds[pth] - delete(state.fds, pth) - if exists { + state.fdsLock.Lock() + if s, exists := state.fds[pth]; exists { + delete(state.fds, pth) s.fd.Close() } + state.fdsLock.Unlock() } func (state *SPState) StartWorkers( @@ -914,7 +916,9 @@ func (state *SPState) StartWorkers( string(TTx), Base32Codec.EncodeToString(freq.Hash[:]), ) + state.fdsLock.RLock() fdAndFullSize, exists := state.fds[pth] + state.fdsLock.RUnlock() if !exists { fd, err := os.Open(pth) if err != nil { @@ -931,7 +935,9 @@ func (state *SPState) StartWorkers( return } fdAndFullSize = FdAndFullSize{fd: fd, fullSize: fi.Size()} + state.fdsLock.Lock() state.fds[pth] = fdAndFullSize + state.fdsLock.Unlock() } fd := fdAndFullSize.fd fullSize := fdAndFullSize.fullSize @@ -1352,7 +1358,9 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { state.Ctx.LogD("sp-file-open", lesp, func(les LEs) string { return logMsg(les) + ": opening part" }) + state.fdsLock.RLock() fdAndFullSize, exists := state.fds[filePathPart] + state.fdsLock.RUnlock() var fd *os.File if exists { fd = fdAndFullSize.fd @@ -1368,7 +1376,9 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { }) return nil, err } + state.fdsLock.Lock() state.fds[filePathPart] = FdAndFullSize{fd: fd} + state.fdsLock.Unlock() if file.Offset == 0 { h, err := blake2b.New256(nil) if err != nil { -- 2.44.0