Consider the following code:
#include <cassert>
#include "uv.h"
void async() {
auto *loop = uv_default_loop();
sockaddr_in saddr;
uv_getnameinfo_t *nameinfo = new uv_getnameinfo_t;
auto err = uv_ip4_addr("", 6667, &saddr);
assert(err);
uv_getnameinfo(loop, nameinfo, +[](uv_getnameinfo_t *, int status, const char *, const char *) {
// error detected
assert(status);
}, reinterpret_cast<const sockaddr *>(&saddr), 0);
}
void sync() {
auto *loop = uv_default_loop();
sockaddr_in saddr;
uv_getnameinfo_t *nameinfo = new uv_getnameinfo_t;
auto err = uv_ip4_addr("", 6667, &saddr);
assert(err);
auto status = uv_getnameinfo(loop, nameinfo, nullptr, reinterpret_cast<const sockaddr *>(&saddr), 0);
// no error detected ?
assert(status); // (1)
}
int main() {
async();
sync();
}
If you compile and run it, assert (1) fails.
The (maybe wrong?) expectation was that if I get an error as an invalid status when used in async mode, I should get an error as an invalid return value when used in sync mode.
One could argue that I should stop the function as soon as I detect the error using uv_ip4_addr and that makes perfectly sense. Anyway the behavior of uv_getnameinfo seems somehow inconsistent.
Note:
async function, the issue was related to the sync one. Honestly it looks like someone just closed the issue without read it. I hope this time to have a better explanation of why it is not a mistake if it is not. That being said, the _problem_ is still there even with an uv_getnameinfo_t that isn't located on the stack, as one could have easily guessed actually.What is the error you get? Also, the async version doesn't run (I think) since you are not running the loop.
The callback never runs because your program doesn't call uv_run()...
edit: hah, cross-post!
Nevermind. Just realized I'm stupid. I should stop working on this project late night. :-)
@skypjack No problem! Out of curiosity, what was the problem?
@saghul
The error was in a test of uvw. I have a listener introduced to detect errors. It should have been attached to the error event. In the async test I attached it to the name info event instead. Therefore an error was present in the async version and no errors were detected in the sync version. I don't know why (I'm not accustomed to using this functions actually), it made perfectly sense to me to have an error for that test. Because of that I was looking only into the sync test to understand what was wrong there.
Yesterday I double checked all the test for the DNS and that's it, I recognized my mistake immediately.
Honestly I wrote uvw out of a necessity, but I have not a great experience with libuv and its underlying functions (getnameinfo is an example). I'm doing my best to get behind libuv and not to bother you when I have a problem. This time I was short in time and I worked on it late night and... Well, I failed miserably.
It happens, I'm so sorry. Thank you anyway for your help.
Don'rt sweat it, it happens to everyone :-) Also, let me use this opportunity to thank you for thelp you provide people on StackOverflow, I really appreciate that!
Oh, replying on SO is the best opportunity for me to dig into the code of libuv and figure out how it works internally. It's quite interesting indeed, even though I'm keen on templates and I really miss them when dealing with C code. :-)
That's pretty nice. If you ever have questions, don't hesitate to ask.
I will do. Thank you very much, really appreciated.
Actually I would haven't written uvw without your help here and in the chat. I hope it can bring even more users to libuv.
Most helpful comment
That's pretty nice. If you ever have questions, don't hesitate to ask.