Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled Promise Rejection On Failed createProducer #119

Closed
savearray2 opened this issue Sep 11, 2020 · 3 comments
Closed

Unhandled Promise Rejection On Failed createProducer #119

savearray2 opened this issue Sep 11, 2020 · 3 comments

Comments

@savearray2
Copy link
Contributor

When creating a producer using a client URL that is intentionally incorrect via the following:

const client = new Pulsar.Client({
	serviceUrl: 'pulsar://this-will-fail:6650',
	ioThreads: 6
})
client.createProducer({
	topic: 'persistent://test/a/b',
}).then((producer) => {
	// ...
}).catch((err) => {
	// ...
})

I get the following unhandled promise rejection warning:

[ClientConnection:469] [<none> -> pulsar://this-will-fail:6650] Resolve error: asio.netdb:1 : Host not found (authoritative)
(node:15) UnhandledPromiseRejectionWarning: Error: Failed to create producer: ConnectError
(node:15) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:15) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Upon further inspection of the code the error originates from the following line:

SetError(std::string("Failed to create consumer: ") + pulsar_result_str(result));

Where ConsumerNewInstanceWorker is a Napi::AsyncWorker:

class ConsumerNewInstanceWorker : public Napi::AsyncWorker {
public:
ConsumerNewInstanceWorker(const Napi::Promise::Deferred &deferred, pulsar_client_t *cClient,
ConsumerConfig *consumerConfig, std::shared_ptr<CConsumerWrapper> consumerWrapper)
: AsyncWorker(Napi::Function::New(deferred.Promise().Env(), [](const Napi::CallbackInfo &info) {})),
deferred(deferred),
cClient(cClient),
consumerConfig(consumerConfig),
consumerWrapper(consumerWrapper) {}

I did some quick checking into using Promises with AsyncWorker and there is a proposed solution mentioned here on the NAPI repository: nodejs/node-addon-api#231 (comment)

I'd be willing to incorporate this solution and create a pull request, if it would be welcome, but I wanted to discuss it here first before making any changes.

I'd also be willing to help out with some of the other requests here, as well.
I'll be using this in production going forward.

@savearray2
Copy link
Contributor Author

Well, this is strange.
I cloned the project and created a simple js file to reproduce the error:

const Pulsar = require('.')
const client = new Pulsar.Client({
	serviceUrl: 'pulsar://this-will-fail:6650'
})
client.createProducer({
	topic: 'persistent://a/b/c',
}).then(() => {
	console.log('success')
}).catch((err) => {
	console.log('normal err')
	console.log(err)
})

This leads to the following output:

# node test_me.js
2020-09-11 16:39:03.046 INFO  [0x700008f87000] ConnectionPool:85 | Created connection for pulsar://this-will-fail:6650
2020-09-11 16:39:03.058 ERROR [0x70000a813000] ClientConnection:469 | [<none> -> pulsar://this-will-fail:6650] Resolve error: asio.netdb:1 : Host not found (authoritative)
2020-09-11 16:39:03.058 INFO  [0x70000a813000] ClientConnection:1387 | [<none> -> pulsar://this-will-fail:6650] Connection closed
2020-09-11 16:39:03.058 ERROR [0x70000a813000] ClientImpl:181 | Error Checking/Getting Partition Metadata while creating producer on persistent://a/b/c -- ConnectError
2020-09-11 16:39:03.058 INFO  [0x70000a813000] ClientConnection:238 | [<none> -> pulsar://this-will-fail:6650] Destroyed connection
normal err
[Error: Failed to create producer: ConnectError]
[1]    45767 segmentation fault  node test_me.js

This still seems to be related to the comment in the NAPI repository mentioned above. According to the comment, a change needed to be made so that exceptions do not occur on exit.

I'd like to get some input from you guys on how to proceed.

@savearray2
Copy link
Contributor Author

Okay, I probably should have done some more research (and gotten more sleep) before posting this. Sorry about this mess.
The problem with the unhandled promise rejection is not with the pulsar library. I had a typo in my testing framework code.

The second problem with the segmentation fault happens when the client is not closed properly before exit. It may be desirable to include code in the library to attach to a process exit to automatically close out the client if it hasn't been already, but this might add unneeded complexity.

The following code fixes the test:

const Pulsar = require('.')
const client = new Pulsar.Client({
	serviceUrl: 'pulsar://this-will-fail:6650'
})
client.createProducer({
	topic: 'persistent://a/b/c',
}).then(() => {
	console.log('success')
	client.close()
}).catch((err) => {
	console.log('normal err')
	console.log(err)
	client.close()
})

Please note the inclusion of client.close().

This issue may be closed at the maintainers' leisure.
Again, I apologize for the trouble.

@sijie
Copy link
Member

sijie commented Sep 15, 2020

@savearray2 thank you for your explanation! Close this issue since it seems that the problem has been resolved.

@sijie sijie closed this as completed Sep 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants