Skip to content

Commit 4f8fe18

Browse files
authored
fix issue with concurrent map writes (#373)
1 parent 9f13df4 commit 4f8fe18

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* FEATURE: upgrade Go builder from Go1.24.2 to Go1.25. See [Go1.25 release notes](https://tip.golang.org/doc/go1.25).
66

77
* BUGFIX: fix log level coloring when no custom rules are configured. See [#347](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/347).
8-
98
* BUGFIX: respect adhoc filters variables. See [#361](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/361)
9+
* BUGFIX: fix issue with concurrent map writes when performing multiple requests to the datasource. See [this issue](https://github.com/VictoriaMetrics/victoriametrics-datasource/issues/363)
1010

1111
## v0.19.3
1212

pkg/plugin/datasource.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ func (d *Datasource) QueryData(ctx context.Context, req *backend.QueryDataReques
269269
}
270270

271271
var wg sync.WaitGroup
272+
var mu sync.Mutex
272273
for _, q := range req.Queries {
273274
rawQuery, err := getQueryFromRaw(q.JSON, forAlerting)
274275
if err != nil {
@@ -279,7 +280,10 @@ func (d *Datasource) QueryData(ctx context.Context, req *backend.QueryDataReques
279280
wg.Add(1)
280281
go func(rawQuery *Query) {
281282
defer wg.Done()
282-
response.Responses[rawQuery.RefID] = di.query(ctx, rawQuery)
283+
resp := di.query(ctx, rawQuery)
284+
mu.Lock()
285+
response.Responses[rawQuery.RefID] = resp
286+
mu.Unlock()
283287
}(rawQuery)
284288
}
285289
wg.Wait()

pkg/plugin/datasource_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"fmt"
78
"net/http"
89
"net/http/httptest"
910
"strings"
@@ -973,3 +974,31 @@ func TestDatasource_checkAlertingRequest(t *testing.T) {
973974
}
974975
f(o)
975976
}
977+
978+
func TestDatasourceQueryDataRace(t *testing.T) {
979+
ctx := context.Background()
980+
ds := NewDatasource()
981+
pluginCtx := backend.PluginContext{
982+
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{
983+
URL: "http://localhost", // Use a valid test server if needed
984+
JSONData: []byte(`{"httpMethod":"POST","customQueryParameters":""}`),
985+
},
986+
}
987+
988+
var queries []backend.DataQuery
989+
for i := 0; i < 20; i++ {
990+
queries = append(queries, backend.DataQuery{
991+
RefID: fmt.Sprintf("A%d", i),
992+
QueryType: instantQueryPath,
993+
JSON: []byte(`{"refId":"A","instant":true,"range":false,"expr":"sum(vm_http_request_total)"}`),
994+
})
995+
}
996+
997+
_, err := ds.QueryData(ctx, &backend.QueryDataRequest{
998+
PluginContext: pluginCtx,
999+
Queries: queries,
1000+
})
1001+
if err != nil {
1002+
t.Fatalf("unexpected error: %v", err)
1003+
}
1004+
}

0 commit comments

Comments
 (0)