Cleanup pending background requests

When we shut down, cleanup pending background requests that haven't been
executed yet.
Fix a number of warnings exposed by http://clang-analyzer.llvm.org

This fixes a number of issues that are either actual issues or trivial
to fix / change.
Only allocate space when it's actually needed

Allocating 0 bytes is undefined behavior and we don't need it anyway in
those cases.