Make Borrowable's descructor warn / crash if destructed with outstanding borrows
Opened this issue · 0 comments
Borrowable's destructor currently hangs until all borrows of the object have been relinquished:
stout/include/stout/borrowed_ptr.h
Lines 26 to 36 in 8dce905
I'd call this surprising behavior: it's not mentioned in documentation that I can see (e.g. not in the old borrowed_ptr README), and it's sharply different behavior from other standard c++ smart pointers (namely std::unique_ptr
, std::shared_ptr
, and std::weak_ptr
) and boost smart pointers.
Let's print a warning warns if a borrrowable destructor is called without outstanding non-relinquished borrows. A warning would have saved me several days of debugging: I had written buggy code where a borrowable was destructed before its uses were relinquished. I didn't expect incorrect Borrowable use to cause deadlocks, and I wasn't able to diagnose the issue without attaching a debugger.
Better yet, let's make this default behavior and remove the default of atomic backoff. (Atomic backoff feels like an orthogonal feature to a safe alternative to raw pointers: if it's something needed by some use cases, perhaps there could be a WaitingBorrowedPointer or similar that adds this extra backoff functionality in an opt-in fashion that most callers would only use if they have shared / non-simple ownership).