diff options
author | eldritch horrors <pennae@lix.systems> | 2024-04-05 23:17:18 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@lix> | 2024-04-05 23:17:18 +0000 |
commit | e9e1b6963c8ee2a0a382487e1346677e77bc1e9e (patch) | |
tree | 4d63978643543657db594327a8f70e1b5c3931d2 /src/libutil | |
parent | 405e41e288768ed99002f7b1d7d68f41c6152c12 (diff) | |
parent | 38dc6f5b69da81dfd21780ef16efaa297f9c2231 (diff) |
Merge changes I1fa30114,I3ca208b6,Ide4c6e00,I74c46b9f,I05fa6a9d, ... into main
* changes:
Revert "libutil: drop Pool resources on exceptional free"
Revert "libutil: remove Pool::Handle::bad"
Revert "libstore: remove one Resource::good flag"
Revert "libstore: using throwing finally in withFramedSink"
Revert "libutil: allow graceful dropping of Pool::Handle"
Revert "libutil: drop Fs{Source,Sink}::good"
libutil: guard Finally against invalid exception throws
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/finally.hh | 25 | ||||
-rw-r--r-- | src/libutil/pool.hh | 39 | ||||
-rw-r--r-- | src/libutil/serialise.cc | 23 | ||||
-rw-r--r-- | src/libutil/serialise.hh | 11 |
4 files changed, 67 insertions, 31 deletions
diff --git a/src/libutil/finally.hh b/src/libutil/finally.hh index f2293e5d4..cbfd6195b 100644 --- a/src/libutil/finally.hh +++ b/src/libutil/finally.hh @@ -1,6 +1,9 @@ #pragma once ///@file +#include <cassert> +#include <exception> + /** * A trivial class to run a function at the end of a scope. */ @@ -19,5 +22,25 @@ public: Finally(Finally &&other) : fun(std::move(other.fun)) { other.movedFrom = true; } - ~Finally() noexcept(false) { if (!movedFrom) fun(); } + ~Finally() noexcept(false) + { + try { + if (!movedFrom) + fun(); + } catch (...) { + // finally may only throw an exception if exception handling is not already + // in progress. if handling *is* in progress we have to return cleanly here + // but are still prohibited from doing so since eating the exception would, + // in almost all cases, mess up error handling even more. the only good way + // to handle this is to abort entirely and leave a message, so we'll assert + // (and rethrow anyway, just as a defense against possible NASSERT builds.) + if (std::uncaught_exceptions()) { + assert(false && + "Finally function threw an exception during exception handling. " + "this is not what you want, please use some other methods (like " + "std::promise or async) instead."); + } + throw; + } + } }; diff --git a/src/libutil/pool.hh b/src/libutil/pool.hh index bc71083e9..1cece71ec 100644 --- a/src/libutil/pool.hh +++ b/src/libutil/pool.hh @@ -1,7 +1,6 @@ #pragma once ///@file -#include <exception> #include <functional> #include <limits> #include <list> @@ -103,24 +102,12 @@ public: private: Pool & pool; std::shared_ptr<R> r; + bool bad = false; friend Pool; Handle(Pool & pool, std::shared_ptr<R> r) : pool(pool), r(r) { } - void drop(bool stillValid) - { - { - auto state_(pool.state.lock()); - if (stillValid) - state_->idle.emplace_back(std::move(r)); - assert(state_->inUse); - state_->inUse--; - } - pool.wakeup.notify_one(); - r = nullptr; - } - public: Handle(Handle && h) : pool(h.pool), r(h.r) { h.r.reset(); } @@ -128,27 +115,25 @@ public: ~Handle() { - if (r) - drop(std::uncaught_exceptions() == 0); - } - - void release() - { - drop(true); + if (!r) return; + { + auto state_(pool.state.lock()); + if (!bad) + state_->idle.push_back(ref<R>(r)); + assert(state_->inUse); + state_->inUse--; + } + pool.wakeup.notify_one(); } R * operator -> () { return &*r; } R & operator * () { return *r; } + + void markBad() { bad = true; } }; Handle get() { - // we do not want to handle the complexity that comes with allocating - // resources during stack unwinding. it would be possible to do this, - // but doing so requires more per-handle bookkeeping to properly free - // resources allocated during unwinding. that effort is not worth it. - assert(std::uncaught_exceptions() == 0); - { auto state_(state.lock()); diff --git a/src/libutil/serialise.cc b/src/libutil/serialise.cc index 6450a9651..692144b75 100644 --- a/src/libutil/serialise.cc +++ b/src/libutil/serialise.cc @@ -52,7 +52,18 @@ FdSink::~FdSink() void FdSink::writeUnbuffered(std::string_view data) { written += data.size(); - writeFull(fd, data); + try { + writeFull(fd, data); + } catch (SysError & e) { + _good = false; + throw; + } +} + + +bool FdSink::good() +{ + return _good; } @@ -117,13 +128,19 @@ size_t FdSource::readUnbuffered(char * data, size_t len) checkInterrupt(); n = ::read(fd, data, len); } while (n == -1 && errno == EINTR); - if (n == -1) { throw SysError("reading from file"); } - if (n == 0) { throw EndOfFile(std::string(*endOfFileError)); } + if (n == -1) { _good = false; throw SysError("reading from file"); } + if (n == 0) { _good = false; throw EndOfFile(std::string(*endOfFileError)); } read += n; return n; } +bool FdSource::good() +{ + return _good; +} + + size_t StringSource::read(char * data, size_t len) { if (pos == s.size()) throw EndOfFile("end of string reached"); diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh index e6290a652..d1c791823 100644 --- a/src/libutil/serialise.hh +++ b/src/libutil/serialise.hh @@ -18,6 +18,7 @@ struct Sink { virtual ~Sink() { } virtual void operator () (std::string_view data) = 0; + virtual bool good() { return true; } }; /** @@ -79,6 +80,8 @@ struct Source */ virtual size_t read(char * data, size_t len) = 0; + virtual bool good() { return true; } + void drainInto(Sink & sink); std::string drain(); @@ -133,6 +136,11 @@ struct FdSink : BufferedSink ~FdSink(); void writeUnbuffered(std::string_view data) override; + + bool good() override; + +private: + bool _good = true; }; @@ -157,8 +165,11 @@ struct FdSource : BufferedSource return *this; } + bool good() override; protected: size_t readUnbuffered(char * data, size_t len) override; +private: + bool _good = true; }; |