Repository: prometheus/prometheus (language: go)

Issue:
Dataraces in package `tsdb`
While testing a new linting rule for [revive](https://github.com/mgechev/revive) I found subtle potential dataraces in the package `tsdb`.
Both dataraces are caused by capturing the named function result `err` in anonymous functions that are executed as goroutines.
First occurrence:
https://github.com/prometheus/prometheus/blob/6555cc68caf8d8f323056e497ae7bb1e32a81667/tsdb/head_wal.go#L134-L141
and following references to `err` in the function literal 

Second:
https://github.com/prometheus/prometheus/blob/6555cc68caf8d8f323056e497ae7bb1e32a81667/tsdb/agent/db.go#L411-L418
and following references to `err` in the function literal 


Discussion / hints:

Fix patch:
diff --git a/tsdb/agent/db.go b/tsdb/agent/db.go
index 7d42d14feba..b0fe1f9627e 100644
--- a/tsdb/agent/db.go
+++ b/tsdb/agent/db.go
@@ -410,6 +410,7 @@ func (db *DB) loadWAL(r *wal.Reader, multiRef map[chunks.HeadSeriesRef]chunks.He
 
 	go func() {
 		defer close(decoded)
+		var err error
 		for r.Next() {
 			rec := r.Record()
 			switch dec.Type(rec) {
diff --git a/tsdb/head_wal.go b/tsdb/head_wal.go
index d0fe685ffb1..02f4aeadb0e 100644
--- a/tsdb/head_wal.go
+++ b/tsdb/head_wal.go
@@ -133,6 +133,7 @@ func (h *Head) loadWAL(r *wal.Reader, multiRef map[chunks.HeadSeriesRef]chunks.H
 
 	go func() {
 		defer close(decoded)
+		var err error
 		for r.Next() {
 			rec := r.Record()
 			switch dec.Type(rec) {


Test patch: