Skip to content

Conversation

john-moffett
Copy link
Contributor

Only raw errno int values are logged if FileCommit fails. These values are implementation-specific, so it makes it harder to debug based on user reports. For instance, #26455 (comment) and another.

Instead, use SysErrorString (or the refactored Windows equivalent Win32ErrorString) to display both the raw int value and the descriptive message. All other instances in the code I could find where errno or (Windows-only) GetLastError()/WSAGetLastError() are logged use the full descriptive string. For example:

errmsg = NetworkErrorString(err);

throw std::runtime_error(strprintf("send(): %s", NetworkErrorString(err)));

bitcoin/src/netbase.cpp

Lines 515 to 516 in 7e1007a

LogPrintf("Error setting SO_NOSIGPIPE on socket: %s, continuing anyway\n",
NetworkErrorString(WSAGetLastError()));

return InitError(strprintf(_("Unable to create the PID file '%s': %s"), fs::PathToString(GetPidFile(args)), SysErrorString(errno)));

I refactored the Windows formatting code to put it in syserror.cpp, as it's applicable to all Win32 API system errors, not just networking errors. To be clear, the Windows API functions WSAGetLastError() and GetLastError() are currently equivalent.

@DrahtBot
Copy link
Contributor

DrahtBot commented Dec 7, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK MarcoFalke
Concept ACK hebasto

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #28075 (util: Remove DirIsWritable, GetUniquePath by MarcoFalke)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Member

@fanquake fanquake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhat unrelated, but after this change, how often are we using the

#ifdef WIN32
    if (strerror_s(...

case in SysErrorString? Wondering if we can (in general) further consolidate this error logging.

@@ -419,18 +419,7 @@ void Sock::Close()
#ifdef WIN32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could turn this into:

Suggested change
#ifdef WIN32
std::string NetworkErrorString(int err)
{
#ifdef WIN32
return Win32ErrorString(err);
#else
// On BSD sockets implementations, NetworkErrorString is the same as SysErrorString.
return SysErrorString(err);
#endif
}

@john-moffett
Copy link
Contributor Author

@fanquake Great suggestions, thanks. Will incorporate them all shortly.

Somewhat unrelated, but after this change, how often are we using the

#ifdef WIN32
    if (strerror_s(...

case in SysErrorString? Wondering if we can (in general) further consolidate this error logging.

I don't think in many places. In addition to the potential use of it for fflush in this PR, there's this:

return InitError(strprintf(_("Unable to create the PID file '%s': %s"), fs::PathToString(GetPidFile(args)), SysErrorString(errno)));

If I artificially trigger that using the current code (by giving it an impossible path), it shows:

image

If I substitute it with Win32ErrorString(GetLastError()), I get:

image

Since the error codes are unrelated (errno and GetLastError() return different actual values), I think it'd be difficult to consolidate effectively.

Potentially, we could have a std::string GetSystemErrorReason() function that called SysErrorString(errno) on non-Win32 and Win32ErrorString(GetLastError()) on Win32, but I'm not certain that (on Win32) every time errno is set that GetLastError() is also set.

@hebasto
Copy link
Member

hebasto commented Dec 8, 2022

Concept ACK.

@glozow
Copy link
Member

glozow commented Apr 25, 2023

@john-moffett please rebase?

@john-moffett
Copy link
Contributor Author

Thanks for reminding me! Will rebase tomorrow.

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice. lgtm, but I didn't look at the windows stuff

return false;
}
#else
if (fsync(fileno(file)) != 0 && errno != EINVAL) {
LogPrintf("%s: fsync failed: %d\n", __func__, errno);
LogPrintf("%s: fsync failed: %d\n", __func__, SysErrorString(errno));
Copy link
Member

@maflcko maflcko Jun 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LogPrintf("%s: fsync failed: %d\n", __func__, SysErrorString(errno));
LogPrintf("fsync failed: %s\n", SysErrorString(errno));

nits:

  • Can remove the manual func logging, which isn't needed, because all strings in this function are already unique to the codebase. Also, users can enable the setting which enables function logging, if they want this.
  • Can change %d to %s, since it is a string. (No behavior difference though)

Only raw errno codes are logged if FileCommit fails. These are
implementation-specific, so it makes it harder to debug based on
user reports. Instead, use SysErrorString to display both the
raw int value and the descriptive message.
GetErrorReason()'s Win32 implementation does the same thing as
Win32ErrorString(int err) from syserror.cpp, so call the latter.

Also remove now-unnecessary headers from sock.cpp and less verbose
handling of #ifdefs.
@maflcko
Copy link
Member

maflcko commented Jul 20, 2023

CI can be ignored.

lgtm ACK 5408a55 💡

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: lgtm ACK 5408a55fc87350baeae3a32f1003f956d5533a79 💡
Maic+76d/0s9p3mR11fkUMJOSi4Wt//8xLPyWD1i2HpZZ4TuNxB3ZRZR3Aq7+Kk0GonkwwTMVO/8XT4Qm8qLDQ==

@fanquake fanquake merged commit ac7c177 into bitcoin:master Jul 20, 2023
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jul 21, 2023
@bitcoin bitcoin locked and limited conversation to collaborators Jul 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants