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

Improve network error reporting on Windows #867

Merged
merged 1 commit into from
Apr 19, 2018

Conversation

nurupo
Copy link
Member

@nurupo nurupo commented Apr 8, 2018

Windows doesn't report network errors though errno, it has its own facilities.

Not sure if the new and kill are the correct words here, toxcore seems to use them for alloc and free, so I'm just following the pattern.

This should fix the Unexpected error reading from socket: 0, No error error on Windows, which someone on IRC supposedly got 60000 times in 10 seconds. The issue was in

int fail_or_len = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
if (fail_or_len < 0 && errno != EWOULDBLOCK) {
    LOGGER_ERROR(log, "Unexpected error reading from socket: %u, %s", errno, strerror(errno));
}

Because Windows doesn't use errno for error reporting, Windows was reporting EWOULDBLOCK through WSAGetLastError() instead of errno, resulting in the logger being called when it shouldn't had been.


This change is Reviewable

@nurupo nurupo requested a review from iphydf April 8, 2018 02:19
@CLAassistant
Copy link

CLAassistant commented Apr 8, 2018

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@nurupo nurupo force-pushed the windows-net-errno branch 7 times, most recently from 06cb656 to f25496f Compare April 8, 2018 03:44
@nurupo
Copy link
Member Author

nurupo commented Apr 8, 2018

Don't merge it yet, can't get logs out of Travis, it seems to be having some issue

There was an error while trying to fetch the log.

@nurupo
Copy link
Member Author

nurupo commented Apr 8, 2018

Removed the commit that enabled all Travis-CI jobs. Here is the the build.

Alright, now we get actual error codes in the logs rather than 0 Sucess, which might help us debug the failures. For example, onion_test has Failed to bind socket: 10022, (null) IP: [] port_from: 34567 port_to: 34667, 10022 means "invalid argument" or "socket in invalid state".

The reported Unexpected error reading from socket: 0, Success errors have disappeared, so that's fixed. Getting 60000 of those errors in 10 seconds sounds CPU intense, we might want include this fix into 0.2.2. Not sure which log level is turned on by default though, it might be that the person set a specific log level to get those and it's not a default?

Btw, you might notice that the logger on Travis CI Windows builds doesn't print text explanations of errors, it prints code and (null), as in Failed to activate local multicast membership. (10045, (null)). That's because most (all?) of Windows Sockets error codes are missing the corresponding text explanations in Wine, even in the most recent development version of Wine, so the net_new_strerror() returns NULL, which logger prints as (null). It should print the actual error messages on a real Windows system, since its dlls have texts for all error codes -- it's just Wine missing those.

@nurupo
Copy link
Member Author

nurupo commented Apr 8, 2018

Feel free to merge if it passes the review, I think I'm done with the PR.

@robinlinden
Copy link
Member

Reviewed 6 of 6 files at r1, 1 of 1 files at r2.
Review status: all files reviewed at latest revision, all discussions resolved, some commit checks failed.


toxcore/network.h, line 407 at r2 (raw file):

int bind_to_port(Socket sock, int family, uint16_t port);

/* Get networking error code.

Get the last networking error code?


toxcore/network.h, line 409 at r2 (raw file):

/* Get networking error code.
 *
 * Similar to Unix's errno, but cross-patform, as not all platforms use errno

"cross-platform"


toxcore/network.h, line 413 at r2 (raw file):

 *
 * Note that different platforms may return different codes for the same error,
 * so you are likely shouldn't be checking the value returned by this function

"you likely shouldn't"


toxcore/network.h, line 414 at r2 (raw file):

 * Note that different platforms may return different codes for the same error,
 * so you are likely shouldn't be checking the value returned by this function
 * unless you know what you are doing, you are likely just want to use it in

"you likely just"


toxcore/network.h, line 415 at r2 (raw file):

 * so you are likely shouldn't be checking the value returned by this function
 * unless you know what you are doing, you are likely just want to use it in
 * the combination with net_new_strerror() to print the error.

Remove leading "the"


toxcore/network.h, line 425 at r2 (raw file):

 * return NULL on failure.
 * return pointer to a NULL-terminated string describing the error code on
 * success. The returned string must be freed using net_new_strerror()

"must be freed using net_kill_strerror." (Remove "function" and fix function name.)


toxcore/network.h, line 430 at r2 (raw file):

char *net_new_strerror(int error);

/* Frees the string returned by the net_new_strerror().

"returned by net_new_strerror()"


Comments from Reviewable

@nurupo
Copy link
Member Author

nurupo commented Apr 9, 2018

Should be fixed now.

@nurupo
Copy link
Member Author

nurupo commented Apr 15, 2018

So, how does the PR look, @robinlinden?

@robinlinden
Copy link
Member

Reviewed 1 of 1 files at r3.
Review status: all files reviewed at latest revision, all discussions resolved, some commit checks failed.


Comments from Reviewable

@robinlinden
Copy link
Member

:lgtm_strong:


Review status: all files reviewed at latest revision, all discussions resolved, some commit checks failed.


Comments from Reviewable

@nurupo
Copy link
Member Author

nurupo commented Apr 15, 2018

Btw, I did

int error = net_error();

in one cases but

int neterror = net_error();

in other because there already was error variable defined, so I simply couldn't use that name there.

@iphydf
Copy link
Member

iphydf commented Apr 15, 2018

Reviewed 1 of 1 files at r2.
Review status: all files reviewed at latest revision, all discussions resolved, some commit checks failed.


toxcore/network.h, line 421 at r3 (raw file):

int net_error(void);

/* Get a text explanation for the error code from net_error().

Add a note that the string returned should not be mutated, despite the type saying it's allowed. Alternatively, strdup+free.


Comments from Reviewable

@nurupo
Copy link
Member Author

nurupo commented Apr 16, 2018

Review status: all files reviewed at latest revision, 1 unresolved discussion, some commit checks failed.


toxcore/network.h, line 421 at r3 (raw file):

Previously, iphydf wrote…

Add a note that the string returned should not be mutated, despite the type saying it's allowed. Alternatively, strdup+free.

Nice catch. You are right, while on Windows a new error string would be allocated for every call, so it can be modified, on Unix-like it would return a pointer to a static string, which shouldn't be modified.

Your change suggestions are kind of weird though. Is there something wrong with changing the return type to const char *?


Comments from Reviewable

@iphydf
Copy link
Member

iphydf commented Apr 16, 2018

Review status: all files reviewed at latest revision, 1 unresolved discussion, some commit checks failed.


toxcore/network.h, line 421 at r3 (raw file):

Previously, nurupo wrote…

Nice catch. You are right, while on Windows a new error string would be allocated for every call, so it can be modified, on Unix-like it would return a pointer to a static string, which shouldn't be modified.

Your change suggestions are kind of weird though. Is there something wrong with changing the return type to const char *?

The problem with making it const char * is that it requires a cast-to-non-const in the free function. To avoid that, you would wrap the result in a struct, return an allocated pointer to that, and make an accessor to get the actual value. I.e.:

// network.h
typedef struct Net_Error Net_Error;

Net_Error *net_error_new(void);
void net_error_kill(Net_Error *err);

int net_error_code(const Net_Error *err);
const char *net_error_message(const Net_Error *err);

// network.c
struct Net_Error {
  const int code;
  char *const message;
};

// net_error_new allocates Net_Error and calls strerror or the windows function to populate message
// net_error_message returns the message (no allocation or other calls here)

Comments from Reviewable

@nurupo
Copy link
Member Author

nurupo commented Apr 17, 2018

toxcore/network.h, line 421 at r3 (raw file):

Previously, iphydf wrote…

The problem with making it const char * is that it requires a cast-to-non-const in the free function. To avoid that, you would wrap the result in a struct, return an allocated pointer to that, and make an accessor to get the actual value. I.e.:

// network.h
typedef struct Net_Error Net_Error;

Net_Error *net_error_new(void);
void net_error_kill(Net_Error *err);

int net_error_code(const Net_Error *err);
const char *net_error_message(const Net_Error *err);

// network.c
struct Net_Error {
  const int code;
  char *const message;
};

// net_error_new allocates Net_Error and calls strerror or the windows function to populate message
// net_error_message returns the message (no allocation or other calls here)

What about now?


Comments from Reviewable

@iphydf
Copy link
Member

iphydf commented Apr 17, 2018

:lgtm_strong:


Review status: 1 of 7 files reviewed at latest revision, 1 unresolved discussion, some commit checks failed.


Comments from Reviewable

@robinlinden
Copy link
Member

Reviewed 5 of 6 files at r4, 1 of 1 files at r5.
Review status: all files reviewed at latest revision, 1 unresolved discussion, some commit checks failed.


Comments from Reviewable

@nurupo nurupo force-pushed the windows-net-errno branch 3 times, most recently from d2c928c to a35fa18 Compare April 17, 2018 22:33
Windows doesn't report network errors though errno, it has its own facilities.
@iphydf iphydf merged commit 7d399ce into TokTok:master Apr 19, 2018
@iphydf iphydf added this to the v0.2.3 milestone Jun 25, 2018
iphydf added a commit to iphydf/c-toxcore that referenced this pull request Sep 5, 2018
v0.2.3
**Merged PRs:**

- [TokTok#951] Only run astyle if the astyle binary exists.
- [TokTok#950] Remove utils.c and utils.h from toxencryptsave build.
- [TokTok#949] Fixes to the imported sodium sources to compile without warnings.
- [TokTok#948] Add a MAX_HOSTNAME_LENGTH constant.
- [TokTok#947] Remove the format test.
- [TokTok#937] Add new Circle CI configuration.
- [TokTok#935] Add a test for double conference invite.
- [TokTok#933] Add Logger to various net_crypto functions, and add `const` to Logger where possible.
- [TokTok#931] Avoid conditional-uninitialised warning for tcp test.
- [TokTok#930] Disable UDP when proxy is enabled.
- [TokTok#928] Use clang-format for C++ code.
- [TokTok#927] Add assertions to bootstrap tests for correct connection type.
- [TokTok#926] Make NULL options behave the same as default options.
- [TokTok#925] Add tests for what happens when passing an invalid proxy host.
- [TokTok#924] Make the net_crypto connection state an enum.
- [TokTok#922] Clarify/Improve test_some test
- [TokTok#921] Beginnings of a TCP_test.c overhaul
- [TokTok#920] Add test for creating multiple conferences in one tox.
- [TokTok#918] Merge irungentoo/master into toktok
- [TokTok#917] Add random testing program.
- [TokTok#916] Fix linking with address sanitizer.
- [TokTok#915] Remove resource_leak_test.
- [TokTok#914] Make dht_test more stable.
- [TokTok#913] Minor cleanup: return early on error condition.
- [TokTok#906] Sort bazel build file according to buildifier standard.
- [TokTok#905] In DEBUG mode, make toxcore crash on signed integer overflow.
- [TokTok#902] Log only the filename, not the full path in LOGGER.
- [TokTok#899] Fix macOS macro because of GNU Mach
- [TokTok#898] Fix enumeration of Crypto_Connection instances
- [TokTok#897] Fix ipport_isset: port 0 is not a valid port.
- [TokTok#894] Fix logging related crash in bootstrap node
- [TokTok#893] Fix bootstrap crashes, still
- [TokTok#892] Add empty logger to DHT bootstrap daemons.
- [TokTok#887] Fix FreeBSD build on Travis
- [TokTok#884] Fix the often call of event tox_friend_connection_status
- [TokTok#883] Make toxcore compile on BSD
- [TokTok#878] fix DHT_bootstrap key loading
- [TokTok#877] Add minitox to under "Other resources" section in the README
- [TokTok#875] Make bootstrap daemon use toxcore's version
- [TokTok#867] Improve network error reporting on Windows
- [TokTok#841] Only check full rtp offset if RTP_LARGE_FRAME is set
- [TokTok#823] Finish @Diadlo's network Family abstraction.
- [TokTok#822] Move system header includes from network.h to network.c
iphydf added a commit to iphydf/c-toxcore that referenced this pull request Sep 5, 2018
v0.2.3
**Merged PRs:**

- [TokTok#951] Only run astyle if the astyle binary exists.
- [TokTok#950] Remove utils.c and utils.h from toxencryptsave build.
- [TokTok#949] Fixes to the imported sodium sources to compile without warnings.
- [TokTok#948] Add a MAX_HOSTNAME_LENGTH constant.
- [TokTok#947] Remove the format test.
- [TokTok#937] Add new Circle CI configuration.
- [TokTok#935] Add a test for double conference invite.
- [TokTok#933] Add Logger to various net_crypto functions, and add `const` to Logger where possible.
- [TokTok#931] Avoid conditional-uninitialised warning for tcp test.
- [TokTok#930] Disable UDP when proxy is enabled.
- [TokTok#928] Use clang-format for C++ code.
- [TokTok#927] Add assertions to bootstrap tests for correct connection type.
- [TokTok#926] Make NULL options behave the same as default options.
- [TokTok#925] Add tests for what happens when passing an invalid proxy host.
- [TokTok#924] Make the net_crypto connection state an enum.
- [TokTok#922] Clarify/Improve test_some test
- [TokTok#921] Beginnings of a TCP_test.c overhaul
- [TokTok#920] Add test for creating multiple conferences in one tox.
- [TokTok#918] Merge irungentoo/master into toktok
- [TokTok#917] Add random testing program.
- [TokTok#916] Fix linking with address sanitizer.
- [TokTok#915] Remove resource_leak_test.
- [TokTok#914] Make dht_test more stable.
- [TokTok#913] Minor cleanup: return early on error condition.
- [TokTok#906] Sort bazel build file according to buildifier standard.
- [TokTok#905] In DEBUG mode, make toxcore crash on signed integer overflow.
- [TokTok#902] Log only the filename, not the full path in LOGGER.
- [TokTok#899] Fix macOS macro because of GNU Mach
- [TokTok#898] Fix enumeration of Crypto_Connection instances
- [TokTok#897] Fix ipport_isset: port 0 is not a valid port.
- [TokTok#894] Fix logging related crash in bootstrap node
- [TokTok#893] Fix bootstrap crashes, still
- [TokTok#892] Add empty logger to DHT bootstrap daemons.
- [TokTok#887] Fix FreeBSD build on Travis
- [TokTok#884] Fix the often call of event tox_friend_connection_status
- [TokTok#883] Make toxcore compile on BSD
- [TokTok#878] fix DHT_bootstrap key loading
- [TokTok#877] Add minitox to under "Other resources" section in the README
- [TokTok#875] Make bootstrap daemon use toxcore's version
- [TokTok#867] Improve network error reporting on Windows
- [TokTok#841] Only check full rtp offset if RTP_LARGE_FRAME is set
- [TokTok#823] Finish @Diadlo's network Family abstraction.
- [TokTok#822] Move system header includes from network.h to network.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants