nest icon indicating copy to clipboard operation
nest copied to clipboard

refactor(microservices): prevent grpc write promise from throwing

Open ssilve1989 opened this issue 1 year ago • 1 comments
trafficstars

PR Checklist

Please check if your PR fulfills the following requirements:

  • [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
  • [x] Tests for the changes have been added (for bug fixes / features)
  • [x] Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • [ ] Bugfix
  • [ ] Feature
  • [ ] Code style update (formatting, local variables)
  • [x] Refactoring (no functional changes, no api changes)
  • [ ] Build related changes
  • [ ] CI related changes
  • [ ] Other... Please describe:

What is the current behavior?

currently the Observable that wraps ServerStreaming calls to write data out to the gRPC call object can reject which needs to be caught, even though the call.emit in the catch handler won't do anything because call.end() happens before that catch block can run, thanks to the unsubscribe call in the Subscription cleanup functions used inside writeObservableToGrpc

Issue Number: N/A

What is the new behavior?

This promise should just never reject in the first place. The Promise is just used to signal when writing has completed and the call has ended. The error signal is handled exclusively by emitting error on the Call.

Does this PR introduce a breaking change?

  • [ ] Yes
  • [x] No

Other information

A series of changes recently outlined here https://github.com/nestjs/nest/issues/13360 resulted in a bug where streaming server calls that replied with errors, would crash the application with an unhandledRejection. This PR also adds a test case in the integration test for asserting that that doesn't happen again.

I renamed the integration test from sum because it no longer only tests sum-related endpoints, but the Math grpc service, so it should be called that instead (the other integration files probably should also). I piggybacked off the existing Divide function to showcase this behavior by providing a request that will result in an error on the streaming response.

ssilve1989 avatar Mar 27 '24 20:03 ssilve1989

Pull Request Test Coverage Report for Build f34ed1b5-fe52-40ae-986d-c84b98dc9403

Details

  • 5 of 5 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.06%) to 92.187%

Totals Coverage Status
Change from base Build eacd3e56-4bed-4f5e-9bab-412a10335aab: 0.06%
Covered Lines: 6737
Relevant Lines: 7308

💛 - Coveralls

coveralls avatar Mar 27 '24 20:03 coveralls

lgtm

kamilmysliwiec avatar Jun 03 '24 11:06 kamilmysliwiec