Skip to content

SseEmitter is not thread safe [SPR-13224] #17815

@spring-projects-issues

Description

@spring-projects-issues

Tomasz Nurkiewicz opened SPR-13224 and commented

SseEmitter misbehaves when multiple threads call send() at the same time. Sometimes container fails, but most of the time different messages get interleaved, e.g.:

emitter.send("foo");  //thread A
emitter.send("bar");  //thread B

sometimes results in:

data:data:AB

It happens because emitter.send("A") actually sends three independent messages: data:, A and \n\n. These three messages can be interleaved with messages from different threads. Please either synchronize properly or document clearly that SseEmitter is not thread safe, so that if clients want to use from multiple threads (I think it's quite useful), it must be synchronized manually:

synchronized(sseEmitter) {
  sseEmitter.send("foo");
}

Affects: 4.2 RC2

Issue Links:

Referenced from: commits bdb6348

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions