Client calls are escaping the request domain
- Dominant language
- TypeScript
- Stars
- 4.8k
- Forks
- 716
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 10
Description
Client calls are escaping the domain making some of our apps to crash.
Similar to this problem https://github.com/mongodb/node-mongodb-native/pull/1184
I modified this [example](https://github.com/grpc/grpc/blob/master/examples/node/static_codegen/greeter_client.js) for demonstration:
```js
const domain = require('domain');
const d = domain.create();
const messages = require('./helloworld_pb');
const services = require('./helloworld_grpc_pb');
const grpc = require('grpc');
function main() {
const client = new services.GreeterClient('localhost:50051', grpc.credentials.createInsecure());
const request = new messages.HelloRequest();
client.sayHello(request, (err, response) => {
console.log('Greeting:', response.getMessage());
throw new Error('Break!'); // <-- This makes the app crash --
});
setInterval(() => {
console.log('app is running.');
}, 2000);
}
d.run(() => {
main();
});
// This should catch the error and recover
d.on('error', (error) => {
console.log('Something wrong happened, but I caught it here and recovered:', error);
main();
});
```
A workaround is to wrap the callback with `domain.bind`
```js
client.sayHello(request, process.domain.bind((err, response) => {
console.log('Greeting:', response.getMessage());
throw new Error('Break!');
}));
```
Contributor guide
Assessment
This issue has not been assessed yet.