Skip to content

ClusterAdapter.broadcast() silently drops events to local sockets when publishAndReturnOffset throws error #5456

@vishnukumarya-kore

Description

@vishnukumarya-kore

Description

In ClusterAdapter.broadcast() (cluster-adapter.js, line 242–261), when
publishAndReturnOffset() rejects (e.g. due to a serialization error in the
adapter layer), the catch block does a return debug(...) which
prevents super.broadcast(packet, opts) from ever being called.

This means sockets connected to the same server instance that emitted the
event also never receive it — a completely silent failure.

Affected code

// cluster-adapter.js lines 242–261
async broadcast(packet, opts) {
    const onlyLocal = opts?.flags?.local;
    if (!onlyLocal) {
        try {
            const offset = await this.publishAndReturnOffset({
                type: MessageType.BROADCAST,
                data: { packet, opts: encodeOptions(opts) },
            });
            this.addOffsetIfNecessary(packet, opts, offset);
        } catch (e) {
            return debug("[%s] error while broadcasting message: %s", this.uid, e.message);
            // ^^^ `return` here means super.broadcast() below is NEVER reached
        }
    }
    super.broadcast(packet, opts);  // ← never called when catch fires
}

Steps to reproduce

  1. Use createShardedAdapter (Redis 7+ sharded pub/sub) with an ioredis Cluster.
  2. Emit an event where the payload causes encode() inside doPublish() to throw
    (e.g. a Mongoose document — see related issue in @socket.io/redis-adapter).
  3. Observe that no connected socket receives the event, including sockets on
    the same Node.js process.

Expected behaviour

When remote publishing fails, the error should be logged but super.broadcast() should still be called so that local sockets on the same server instance receive the event.

Environment

  • socket.io-adapter: 2.x (ClusterAdapter / ClusterAdapterWithHeartbeat)
  • @socket.io/redis-adapter: 8.3.x (ShardedRedisAdapter)
  • Redis: 7.x cluster with sharded pub/sub (SPUBLISH/SSUBSCRIBE)
  • Node.js: 18.x

Metadata

Metadata

Assignees

No one assigned

    Labels

    to triageWaiting to be triaged by a member of the team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions