From 8832bfb4583b779be0501b4cf70160d5417d0e68 Mon Sep 17 00:00:00 2001 From: Alex Rousskov Date: Fri, 16 Jul 2021 13:15:49 -0400 Subject: [PATCH] Enforce more "safe job callbacks" invariants, albeit at runtime --- src/base/AsyncJob.cc | 11 ++++++++++- src/base/AsyncJob.h | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/base/AsyncJob.cc b/src/base/AsyncJob.cc index bf924eb4b1..16d444e017 100644 --- a/src/base/AsyncJob.cc +++ b/src/base/AsyncJob.cc @@ -24,6 +24,7 @@ AsyncJob::Pointer AsyncJob::Start(AsyncJob *j) { AsyncJob::Pointer job(j); CallJobHere(93, 5, job, AsyncJob, start); + job->started_ = true; // it is the attempt that counts return job; } @@ -38,6 +39,7 @@ AsyncJob::~AsyncJob() { debugs(93,5, "AsyncJob destructed, this=" << this << " type=" << typeName << " [" << id << ']'); + assert(!started_ || swanSang_); } void AsyncJob::start() @@ -141,9 +143,16 @@ void AsyncJob::callEnd() AsyncCall::Pointer inCallSaved = inCall; void *thisSaved = this; + // TODO: Swallow swanSong() exceptions to reduce memory leaks. + + // Job callback invariant: swanSong() is (only) called for started jobs. + // Here to detect violations in kids that forgot to call our swanSong(). + assert(started_); + + swanSang_ = true; // it is the attempt that counts swanSong(); - delete this; // this is the only place where the object is deleted + delete this; // this is the only place where a started job is deleted // careful: this object does not exist any more debugs(93, 6, HERE << *inCallSaved << " ended " << thisSaved); diff --git a/src/base/AsyncJob.h b/src/base/AsyncJob.h index db17297f3d..fe3e26d74f 100644 --- a/src/base/AsyncJob.h +++ b/src/base/AsyncJob.h @@ -74,6 +74,9 @@ protected: const char *stopReason; ///< reason for forcing done() to be true const char *typeName; ///< kid (leaf) class name, for debugging AsyncCall::Pointer inCall; ///< the asynchronous call being handled, if any + + bool started_ = false; ///< Start() has finished successfully + bool swanSang_ = false; ///< swanSong() was called }; #endif /* SQUID_ASYNC_JOB_H */ -- 2.47.2