Skip to content

Duplicate messages emitted after error in onmessage #563

@jcheng5

Description

@jcheng5

Using xhr-streaming, xhr-polling, or jsonp-polling, if an error is raised in the socket.onmessage event handler, then the next time a message is received SockJS will replay the message that caused the error.

For example, message A is received and the client onmessage event handler throws an uncaught error; later, message B is received, and onmessage is called again with message A.

Repro repo: https://github.com/jcheng5/sockjs-error-test

The problem appears to be here:

for (var idx = -1; ; this.bufferPosition += idx + 1) {
var buf = text.slice(this.bufferPosition);
idx = buf.indexOf('\n');
if (idx === -1) {
break;
}
var msg = buf.slice(0, idx);
if (msg) {
debug('message', msg);
this.emit('message', msg);
}
}
};

When this.emit('message', msg); throws an exception, this.bufferPosition doesn't get updated (third clause of the for-loop). If you update this.bufferPosition before emitting the message, the problem goes away.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions