sentry-java icon indicating copy to clipboard operation
sentry-java copied to clipboard

SentrySpringRequestListener not working as expected when using streamed responses

Open lehoffma opened this issue 5 years ago • 3 comments

Platform:

  • [X] Java -> 11 (both source and target)
  • [X] Spring -> Spring Boot 2.4.0, Spring 5.3.1

IDE:

  • [X] IntelliJ

IntelliJ IDEA 2020.3 (Ultimate Edition) Build #IU-203.5981.155, built on November 30, 2020 Runtime version: 11.0.9+11-b1145.21 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Windows 10 10.0

Build system:

  • [X] Maven -> Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T20:41:47+02:00)

Android Gradle Plugin:

  • [X] No

Sentry Android Gradle Plugin:

  • [X] No

Proguard/R8:

  • [X] Disabled

Platform installed with:

  • [X] Maven Central

The version of the SDK: 3.2.0 (didn't work with 3.1.3 either)


I have the following issue:

In our REST application we are using Spring's StreamingResponseBody to stream big chunks of data in a memory efficient way. For example:

@RestController
@RequestMapping("api/resources")
public class ResourcesController {
    @GetMapping(path = "test.zip", produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE})
    public ResponseEntity<StreamingResponseBody> getZip(
        return ResponseEntity.ok()
            .body(outputStream -> {
                //here we write something into the outputstream
           };
    }
}

If an error happens before we enter the streaming part of the response, everything works as expected. However, if an exception is thrown inside that outputStream -> {} lambda, we don't receive the usual request specific tags and breadcrumbs.

We tried to figure out the exact reason behind that and it seems to be connected to how Sentry associates its data with a Thread. We put breakpoints throughout the library code to follow along and found that the SentrySpringRequestListener' applies the event processors to the "main" request thread (Thread A) scope, but the exception resolver is called in a different thread, the one Spring created to handle the streaming (Thread B). The listener's requestDestroyed method is also called from Thread B, which means it tries to clean up a scope of the wrong thread and Thread A's added event processor is kept.

We worked around the missing request data tags by using a custom HandlerExceptionResolver, but that is 1) very much a hack in our eyes and 2) doesn't even fix our problem of the lost breadcrumbs that are not related to the request itself.

Steps to reproduce:

  1. Create Spring Boot web project with sentry-spring-boot-starter maven dependency and a valid dsn
  2. Create a Controller like this:
@RestController
@RequestMapping("api")
public class ResourcesController {
    @GetMapping(path = "throwErrorInStream", produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE})
    public ResponseEntity<StreamingResponseBody> getZip(
        return ResponseEntity.ok()
            .body(outputStream -> {
                throw new RuntimeException("test");
           };
    }
}
  1. Call HTTP endpoint with a tool of your choice
  2. Check request data of SentryEvent either 4.1. on the website or 4.2. by creating a BeforeSendCallback handler and setting a breakpoint in the execute method like this:
@Component
public class WebSentryEventHandler implements SentryOptions.BeforeSendCallback {
	@Override
	public SentryEvent execute(SentryEvent event, Object hint) {
		return event; //breakpoint here
	}
}

Actual result:

The created Sentry event doesn't contain any request specific data (url, user, browser, etc).

Expected result:

The created Sentry event contains request specific data, as it is thrown inside a request.

Let me know if there's any more info you need!

lehoffma avatar Dec 11 '20 12:12 lehoffma

I'd say it's not surprising given that once you return from the controller, the framework sends out the headers and starts writing out the response body as per that callback. I assume you'd need to have some error handling inside the call back (i.e: try catch) and log any exception to Sentry in there.

Unless some trailing header of sorts exist, your user is unaware of your error either, since 200 or whatever was already sent out in the request.

bruno-garcia avatar Dec 15 '20 02:12 bruno-garcia

not sure if I tag this as an enhancement or bug, seems a limitation for streaming responses, but not generally a bug IMO.

marandaneto avatar Jan 08 '21 08:01 marandaneto

@maciejwalkowiak can we use the same concept for kotlin coroutines here?

marandaneto avatar May 27 '21 08:05 marandaneto