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

Add a couple of throws for bad states #2132

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion folly/experimental/io/EpollBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ void SignalRegistry::notify(int sig) {
int fd = notifyFd_.load();
if (fd >= 0) {
uint8_t sigNum = static_cast<uint8_t>(sig);
::write(fd, &sigNum, 1);
if (::write(fd, &sigNum, 1) != 1) {
throw std::runtime_error("Failed to write all the byes.");
}
Comment on lines +97 to +99
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like the type of write that is infallible, i.e., cannot fail (as long as the inputs are correct), and total, i.e. cannot be a partial write.

And if this write wre to fail, there is nothing that would know how to clean this up.

Failure would be an invariant violation. If we are looking for invariant violations, such as we might do in debug builds, we would use something in the family of assert (eg DCHECK, PCHECK, FOLLY_SAFE_DCHECK, etc).

Partial writes may occur if the backend has insufficient resources to accept the full write, and if the write is non-blocking or is interrupted by a signal while blocking. But this is not the sort of write where the backend could have insufficient resources.

}
}

Expand Down
4 changes: 4 additions & 0 deletions folly/experimental/settings/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <folly/experimental/settings/Types.h>

#include <string_view>

namespace folly {
namespace settings {

Expand All @@ -27,6 +29,8 @@ std::string_view toString(SetErrorCode code) {
return "rejected";
case SetErrorCode::FrozenImmutable:
return "frozen immutable";
default:
throw std::invalid_argument("Code '" + std::to_string(static_cast<int>(code)) + "' had no string representation!");
Comment on lines +32 to +33
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO this should not be an error - it's for diagnostics.

We should like to return "(unknown)" in case of an unrecognized input. But we should also like the compiler to warn us when the cases in the switch are non-exhaustive.

}
}

Expand Down
Loading