fix(adapter): do not skip local broadcast when publishAndReturnOffset throws (#5457)

Remove the `return` in the catch block of ClusterAdapter.broadcast() so
that super.broadcast() is still called when remote publishing fails.

This ensures local sockets receive the event even if the cluster publish
errors out (e.g. due to a serialization error in the adapter layer).

Related: https://github.com/socketio/socket.io/issues/5456
This commit is contained in:
Sarthak Shah
2026-03-12 16:08:29 +05:30
committed by GitHub
parent 37aad11417
commit f6301588ca
2 changed files with 18 additions and 5 deletions

View File

@@ -438,11 +438,7 @@ export abstract class ClusterAdapter extends Adapter {
});
this.addOffsetIfNecessary(packet, opts, offset);
} catch (e) {
return debug(
"[%s] error while broadcasting message: %s",
this.uid,
e.message,
);
debug("[%s] error while broadcasting message: %s", this.uid, e.message);
}
}

View File

@@ -15,6 +15,7 @@ const NODES_COUNT = 3;
class EventEmitterAdapter extends ClusterAdapterWithHeartbeat {
private offset = 1;
public shouldFailPublish = false;
constructor(
nsp,
@@ -27,6 +28,9 @@ class EventEmitterAdapter extends ClusterAdapterWithHeartbeat {
}
protected doPublish(message: ClusterMessage): Promise<string> {
if (this.shouldFailPublish) {
return Promise.reject(new Error("publish failed"));
}
this.eventBus.emit("message", message);
return Promise.resolve(String(++this.offset));
}
@@ -152,6 +156,19 @@ describe("cluster adapter", () => {
servers[0].local.emit("test");
});
it("broadcasts to local clients even when publishAndReturnOffset throws", (done) => {
const adapter = servers[0].of("/").adapter as EventEmitterAdapter;
adapter.shouldFailPublish = true;
clientSockets[0].on("test", (arg1) => {
expect(arg1).to.eql(1);
adapter.shouldFailPublish = false;
done();
});
servers[0].emit("test", 1);
});
it("broadcasts with multiple acknowledgements", (done) => {
clientSockets[0].on("test", (cb) => cb(1));
clientSockets[1].on("test", (cb) => cb(2));