StreamObserver.onError() call on different instance when client gets SIGKILL (CANCELLED: client cancelled)
- Dominant language
- Java
- Stars
- 12.1k
- Forks
- 4k
- Avg merge
- 2d 17h
- Merged PRs (30d)
- 37
Description
I have an ongoing bidirectional stream between my client and my server. I then terminate my client with SIGKILL. This leads to a `io.grpc.StatusRuntimeException: CANCELLED: client cancelled` and a call to my `onError()` implementation of the `StreamObserver` on the server-side. My issue is that the call is not executed on the same `StreamObserver` instance as `onNext()` earlier. This leads to the problem that some attributes in my StreamObserver implementation are `null` during the `onError()` call instead of the values I set them to earlier. This does not happen when for example an exception is thrown during the `onNext()` execution. In this case the `onError()` is executed on the same instance. I'm using the `@Scope(scopeName = "grpcRequest", proxyMode = ScopedProxyMode.TARGET_CLASS)` notation above my class. While debugging I noticed that the instance gets destroyed right before the `onError()` call and the application logging the `StatusRuntimeException: CANCELLED: client cancelled`. I raised the issue already with the grpc-spring-boot-starter project as yidongnan/grpc-spring-boot-starter#870, as I was not sure if this is the right place or if this behaviour is intended, but it seemed rather strange to me. The kind maintainer of the project deducted that this should be an issue with the request scope, that should be valid at this point, but is no longer. Therefore I'm opening this issue.
For my needs I found a way to circumvent the issue with the Javax annotation `@PreDestroy` in my `StreamObserver`, in case anybody else is having the same issue.
### What version of gRPC-Java are you using?
grpc-java: 1.51.0
### What is your environment?
* Spring (boot): 2.6.13
* grpc-spring-boot-starter: 2.14.0.RELEASE
* java: 18.0.2 64 Bit
### What did you expect to see?
That the `onError()` call is executed on the same instance
### What did you see instead?
It was executed on a different instance.
### Steps to reproduce the bug
Ongoing bidirectional stream between server and client. Send SIGKILL to the client and log the instance on the server-side like below.
To make my issue more clear if I log the instance (with `System.out.print("SOME STRING" + this)`) my methods are called during the bidirectional stream would look like something like this:
```
onNext() call instance: ClassName@AAAA
onNext() call instance: ClassName@AAAA
[the clients gets SIGKILL in the meatime]
onError() call instance: ClassName@BBBB
```
Contributor guide
Assessment
This issue has not been assessed yet.