lock: fix race in background thread when shard_num changes

shard_num can be unset and set to another value if we don't hold
self.lock while depending on its value. This causes a crash if the
log.error() triggers and tries to format None as an int.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>