#1248·centrifugo

[bug] Broadcast with tags crashes the server when using the redisnats broker

Author: darakanoitCreated Sep 29, 2026Updated Sep 29, 2026

Describe the bug. NatsBroker.PublishWithStreamPosition writes the internal epoch tag directly into opts.Tags, a map that belongs to the caller:

go
tags := opts.Tags
if tags == nil {
	tags = map[string]string{}
}
tags[epochTagsKey] = sp.Epoch

Executor.Broadcast publishes to each channel in its own goroutine and passes the same cmd.Tags map to all of them (centrifuge.WithTags does not copy it). With the redisnats broker, every publish that uses history or an idempotency key goes through PublishWithStreamPosition. So several goroutines write to one map while others read or serialize it in RedisBroker.Publish. The process dies with a fatal error that cannot be recovered: concurrent map writes, concurrent map iteration and map write or concurrent map read and map write.

Async consumers are affected too: Dispatcher.DispatchPublication calls Broadcast when a message targets more than one channel.

Even a single-channel publish leaves __centrifugo_epoch in the caller's map.

Versions

Centrifugo version is master (a2e08963). The code dates back to v5.2.0 (#740), so v5.2.0 through v6.9.7 are affected. Client library used is not relevant (server API only). Operating system is macOS (arm64); the integration test also reproduces it in a Linux golang:1.26 container.

Steps to Reproduce How can the bug be triggered?

  1. Start Redis and Nats (docker compose up -d redis nats) and run Centrifugo with:
    json
    {
      "enable_unreleased_features": true,
      "broker": { "enabled": true, "type": "redisnats" },
      "channel": { "without_namespace": { "history_size": 10, "history_ttl": "60s" } },
      "http_api": { "key": "test-api-key" }
    }
  2. Send a broadcast with tags to several channels, repeating it a few times:
    bash
    curl -X POST http://localhost:8000/api/broadcast \
      -H "X-API-Key: test-api-key" \
      -d '{"channels": ["ch1", "ch2", "ch3", "ch4"], "data": {}, "tags": {"k": "v"}}'

In 6 runs the server crashed every time, after 1 to 26 sequential requests:

fatal error: concurrent map writes

goroutine 163 [running]:
internal/runtime/maps.fatal(...)
github.com/centrifugal/centrifugo/v6/internal/natsbroker.(*NatsBroker).PublishWithStreamPosition(...)
	internal/natsbroker/broker.go:212 +0xd0
github.com/centrifugal/centrifugo/v6/internal/redisnatsbroker.(*Broker).Publish(...)
	internal/redisnatsbroker/broker.go:41 +0x1b4
github.com/centrifugal/centrifuge.(*Node).publish(...)
	github.com/centrifugal/[email protected]/node.go:925 +0x150
github.com/centrifugal/centrifuge.(*Node).Publish(...)
	github.com/centrifugal/[email protected]/node.go:964
github.com/centrifugal/centrifugo/v6/internal/api.(*Executor).Broadcast.func1(...)
	internal/api/api.go:371 +0x498
created by github.com/centrifugal/centrifugo/v6/internal/api.(*Executor).Broadcast in goroutine 284
	internal/api/api.go:322 +0x394

Expected behavior What output or behaviour were you expecting instead?

Publishing must not modify the caller's tags, and broadcast with tags must work with any broker.